GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Oct 23, 2014 2:24 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: g_hash_table_insert() and local string array
PostPosted: Thu Dec 20, 2012 10:37 pm 
Offline
Familiar Face

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

According to the manual:
Quote:
Note that neither keys nor values are copied when inserted into the GHashTable, so they must exist for the lifetime of the GHashTable. This means that the use of static strings is OK, but temporary strings (i.e. those created in buffers and those returned by GTK+ widgets) should be copied with g_strdup() before being inserted.


I am confused why the following code works? After atable_new() exist, shouldn't keys[] and values[] on stack also gone?

Code:

//tmp1.h
#ifndef _TMP1_
#define _TMP1_
#include <glib.h>
#include <gtk/gtk.h>
typedef void atable;
atable *atable_new();
void atable_free(atable *table);
gchar *atable_lookup(atable* table, gpointer key);
#endif


Code:

//tmp1.c
#include "tmp1.h"
atable *atable_new(){
  gchar *keys[]={"sam", "jack"};
  gchar *values[]={"happy", "sad"};
  GHashTable *table;
  table = g_hash_table_new(g_str_hash, g_str_equal);
  int i;
  for (i=0;i<2;i++)
    g_hash_table_insert(table, (gpointer)keys[i], (gpointer)values[i]);
  return (atable *)table;
}
void atable_free(atable *table){
g_hash_table_destroy((GHashTable *)table);
}
gchar *atable_lookup(atable* table, gpointer key){
return (gchar *) g_hash_table_lookup((GHashTable *)table, (gpointer)key);
}


Code:

//tmpmain.c
#include <stdio.h>
#include "tmp1.h"
int main(){
  atable *table;
  table = atable_new();
  printf("%s\n", atable_lookup(table, "sam"));
  printf("%s\n", atable_lookup(table, "jack"));
  atable_free(table);
}


I compiled tmpmain and the output is:
happy
sad

I thought this code should not work!


Top
 Profile  
 
 Post subject: Re: g_hash_table_insert() and local string array
PostPosted: Fri Dec 21, 2012 7:29 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
I don't think the data values are actually on the stack.
Your c compiler will generally perform a number of optimisations. When it sees that the data in the atable_new function is not going to change and is identical for every atable_new call, it will leave it in static memory as that is faster and more memory efficient.


Top
 Profile  
 
 Post subject: Re: g_hash_table_insert() and local string array
PostPosted: Sat Dec 22, 2012 3:34 am 
Offline
Familiar Face

Joined: Sun Dec 16, 2012 9:09 pm
Posts: 12
Yes, you are right! I knew the code were problemtic; just confused why it could possibly work. I actually used flag -O0. It seems gcc still did something behind the scene.

A slightly modified code did not work as expected:

./tmpmain
Segmentation fault (core dumped)


Code:
#include "tmp1.h"
atable *atable_new(char *key1, char *value1, char* key2, char *value2){

  GHashTable *table;
  table = g_hash_table_new(g_str_hash, g_str_equal);
  int i;
  g_hash_table_insert(table, (gpointer)key1, (gpointer)value1);
  g_hash_table_insert(table, (gpointer)key2, (gpointer)value2);
  return (atable *)table;
}
void atable_free(atable *table){
  g_hash_table_destroy((GHashTable *)table);
}
gchar *atable_lookup(atable* table, gpointer key){
  return (gchar *) g_hash_table_lookup((GHashTable *)table, (gpointer)key);
}


Code:
//tmp1.h
#ifndef _TMP1_
#define _TMP1_
#include <glib.h>
#include <gtk/gtk.h>
typedef void atable;
atable *atable_new();
void atable_free(atable *table);
gchar *atable_lookup(atable* table, gpointer key);
#endif


Code:
#include <stdio.h>
#include "tmp1.h"
int main(int argc, char **argv){
  atable *table;
  table = atable_new(argv[1], argv[2], argv[3], argv[4]);
  printf("%s\n", atable_lookup(table, "sam"));
  printf("%s\n", atable_lookup(table, "jack"));
  atable_free(table);
}


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 1 guest


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