GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Tue Nov 25, 2014 8:37 pm

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: is it in stack or fixed memory
PostPosted: Thu Dec 20, 2012 7:33 pm 
Offline
Familiar Face

Joined: Sun Dec 16, 2012 9:09 pm
Posts: 12
Dear there,

In the following code, I think the array gchar *sym_names[] is in the fixed memory until the program exit. I'm thinking about moving it inside the function symtable_alloc() as a local variable, thus it will be allocated on the entry and will be released when the function symtable_alloc() returns.
The built hash table has all strings anyway, hence I think it is a bit waste to keep the array gchar *sym_names[] in the fixed memory all the time.
What do you think?

Code:
#include <glib.h>
#include "mathsym-data.h" /* GdkPixbuf RGBA C-Source image dump 1-byte-run-length-encoded */
#include "mathsym.h"

const guint8 *sym_data[]= {
  sym_alpha, //in the mathsym-data.h
  sym_beta,  sym_chi,   sym_delta,   sym_Delta,   sym_epsilon,
  sym_eta,   sym_gamma,   sym_Gamma,   sym_iota,   sym_kappa,
  sym_lambda,   sym_Lambda,   sym_mu,   sym_nu,   sym_omega,
  sym_Omega,   sym_phi,   sym_Phi,   sym_pi,   sym_Pi,   sym_psi,
  sym_Psi,   sym_rho,   sym_sigma,   sym_Sigma,   sym_tau,
  sym_theta,   sym_Theta,  sym_upsilon,  sym_Upsilon,  sym_varepsilon,
  sym_varphi,  sym_varpi,  sym_varrho,  sym_varsigma,  sym_vartheta,
  sym_xi,   sym_Xi,   sym_zeta
};

const gchar *sym_names[]= {
  "\\alpha",  "\\beta",  "\\chi",  "\\delta",  "\\Delta",
  "\\epsilon",  "\\eta",  "\\gamma",  "\\Gamma",  "\\iota",
  "\\kappa",  "\\lambda",  "\\Lambda",  "\\mu",  "\\nu",
  "\\omega",  "\\Omega",  "\\phi",  "\\Phi",  "\\pi",
  "\\Pi",  "\\psi",  "\\Psi",  "\\rho",  "\\sigma",
  "\\Sigma",  "\\tau",  "\\theta",  "\\Theta",  "\\upsilon",
  "\\Upsilon",  "\\varepsilon",  "\\varphi",  "\\varpi",  "\\varrho",
  "\\varsigma",  "\\vartheta",  "\\xi",  "\\Xi",  "\\zeta"
};

symtable *symtable_alloc(){
//I propose to move the declaration and  definition of gchar *sym_names[] here
//to save memory. Am I thinking in the right direction? 
 
  GHashTable *table;
  table = g_hash_table_new(g_str_hash, g_str_equal);
  int i;
  for(i=0;i<sizeof(sym_names)/sizeof(gchar *); i++)
  {
    g_hash_table_insert(table, (gpointer)sym_names[i], (gpointer)sym_data[i]);
  }
  return (symtable *)table;
}

void symtable_free(symtable *table){
  g_hash_table_destroy((GHashTable *)table);
}

guint8 *symtable_lookup(symtable *table, const gchar* latex)
{
  return (guint8 *) g_hash_table_lookup((GHashTable *)table, (gpointer)latex);
}


Top
 Profile  
 
 Post subject: Re: is it in stack or fixed memory
PostPosted: Thu Dec 20, 2012 10:28 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
You don't want the data/names to be freed on return as your hash table has pointers to them stored inside. You'd likely get segfaults when doing the lookup. If you did move it I don't think it would free it anyway, just make it local in scope.

At some point in your compiled binary you must have the sym_data etc. somewhere. Having a const global is the most efficient way of doing this.
Concerning freeing the space: it really is a small chunk of memory and you would be limited in what could make use of that space once freed. If you are feeling resource constrained to that extent then using GTK is probably out of the question.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group