You are not logged in.

#1 2010-11-24 19:11:49

ShadowKyogre
Member
From: Hell! XP No... I'm not telling
Registered: 2008-12-19
Posts: 476
Website

[C/Glib] Segfault because of array declaration? [SLVD, read last post]

I'm having trouble with making character arrays that I'm using to store the cached files in compiz-deskmenu.

Code: http://pastebin.com/FNrvpTKS

When I try to execute that function, the program segfaults before it enters the if/else statement in the cache management. It may have to do with how I declared the array, but I'm pretty much lost at that sad.

Compile errors I get related to this:

deskmenu-menu.c:941:19: warning: 'cache_name' may be used uninitialized in this function
deskmenu-menu.c:904:30: warning: 'cache_file' may be used uninitialized in this function

Changing the original declarations to:

gchar **cache_name = NULL;
gchar **cache_file = NULL;

does not fix the problem, even though it gets rid of that compile error.

Last edited by ShadowKyogre (2010-11-27 05:06:56)


For every problem, there is a solution that is:
Clean
Simple and most of all...wrong!
Github page

Offline

#2 2010-11-24 22:15:56

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: [C/Glib] Segfault because of array declaration? [SLVD, read last post]

There's nothing better than valgrind for diagnosing segfaults.  Anyway, &cache_file[0] is undefined behaviour since you're dereferencing a NULL pointer ("&cache_file[0]" is equivalent to "&(*(cache_file + 0))", and "cache_file + 0" is NULL).

I suppose you want cache_file to be an array of pointers-to-gchar?  What size should this array have?  If that size is known at compile time, declare cache_file as "gchar *cache_file[NCACHEFILES]"; if the size is only known at run time, do "gchar **cache_files = malloc(ncachefiles * sizeof(gchar *))", and don't forget to free() it later.

Offline

#3 2010-11-24 23:56:06

ShadowKyogre
Member
From: Hell! XP No... I'm not telling
Registered: 2008-12-19
Posts: 476
Website

Re: [C/Glib] Segfault because of array declaration? [SLVD, read last post]

tavianator wrote:

There's nothing better than valgrind for diagnosing segfaults.  Anyway, &cache_file[0] is undefined behaviour since you're dereferencing a NULL pointer ("&cache_file[0]" is equivalent to "&(*(cache_file + 0))", and "cache_file + 0" is NULL).

I suppose you want cache_file to be an array of pointers-to-gchar?  What size should this array have?  If that size is known at compile time, declare cache_file as "gchar *cache_file[NCACHEFILES]"; if the size is only known at run time, do "gchar **cache_files = malloc(ncachefiles * sizeof(gchar *))", and don't forget to free() it later.

Yes, it should normally have a flexible size, but I think 1000 would be a sane size limit for the array. I changed the &cache_file[0] to a temporary pointer for transfer, but now the only problem is that the arrays aren't being written back to (which probably makes the input & array element comparisons fail).

Code: http://pastebin.com/M6LRdWW6

[EDIT] Decided to use a GHashTable since that gets rid of these errors, allows a dynamic cache, and makes it easier to look up items in the cache since the cache structure is just a cached filename to a cached file content.

Last edited by ShadowKyogre (2010-11-27 05:05:58)


For every problem, there is a solution that is:
Clean
Simple and most of all...wrong!
Github page

Offline

Board footer

Powered by FluxBB