|
From: Jeroen N. W. <jn...@xs...> - 2004-03-27 19:28:21
|
> Hi,
>
> I am working on some code creating and updating a dynamic allocated char
> * array.
>
> When I run my program, it runs correctly. However, when I run it through
> valgrind, it not only detects memory errors, but also runs differently.
> Could that be a valgrind bug, or am I missing something here.
The error detected by valgrind is accurate; there is a bug in your
program. See my comments below.
>
> TIA
> Bart Friederichs
>
> PS
> I am not subscribed to the list, so a cc to my own address would be very
> much appreciated.
>
> code in main.c:
> /******* TEST *******/
> char **t; int s=0;
> t = setup_string_array();
> printf("Adding line 1: \n");
In the calls to string_array_add() below, you must pass the address of the
pointer to the array:
string_array_add(&t, "first line");
> printf("Adding line 2: \n");
string_array_add(&t, "second line");
>
> print_string_array(t);
> free_string_array(t);
> oc_exit(0);
> /******* ENDTEST *****/
>
> functions:
> /* create an empty string array, with only one element: NULL */
> char **setup_string_array() {
> char **ret = malloc(sizeof(char *));
> ret[0] = NULL;
>
> return ret;
> }
>
> /* add a string to the array, a copy will be made of the string (NULL
> terminated)
> * return 1 on success, otherwise
> */
You must pass the address of the pointer to the array. Otherwise, you only
modify the copy of the pointer to the array on the stack.
int string_array_add(char ***array, char *string) {
> int ret = 0, size = -1;
> char *new, **t;
>
> /* make a copy of the string */
> new = malloc(strlen(string)+1);
> strcpy(new, string);
>
> printf("nul: %s\n", array[0]);
while ((*array)[++size]) {
printf("element %d: %s\n", size, (*array)[size]);
> }
>
> printf("Size of string array before: %d\n", size);
t = realloc((*array), (size+2)*sizeof(char *));
> if (t) {
(*array) = t;
(*array)[size] = new;
(*array)[size+1] = NULL;
> ret = 1;
> }
>
> return ret;
> }
>
My guess is that without valgrind, realloc() always returns the same
pointer. Apparently the valgrind replacement for realloc() does not.
Regards,
Jeroen.
> Run without valgrind:
> bart@thorax src $ ./oculusd -v -c oculusd.conf
> Adding line 1:
> nul: (null)
> Size of string array before: 0
> Adding line 2:
> nul: first line
> element 0: first line
> Size of string array before: 1
> first line
> second line
>
> Run with valgrind:
> bart@thorax src $ valgrind ./oculusd -v -c oculusd.conf
> ==2765== Memcheck, a.k.a. Valgrind, a memory error detector for x86-linux.
> ==2765== Copyright (C) 2002-2003, and GNU GPL'd, by Julian Seward.
> ==2765== Using valgrind-2.0.0, a program supervision framework for
> x86-linux.
> ==2765== Copyright (C) 2000-2003, and GNU GPL'd, by Julian Seward.
> ==2765== Estimated CPU clock rate is 702 MHz
> ==2765== For more details, rerun with: -v
> ==2765==
> Adding line 1:
> nul: (null)
> Size of string array before: 0
> Adding line 2:
> ==2765== Invalid read of size 4
> ==2765== at 0x8049B30: string_array_add (generic.c:108)
> ==2765== by 0x8049450: main (main.c:34)
> ==2765== by 0x403EC90B: __libc_start_main (in /lib/libc-2.3.2.so)
> ==2765== by 0x8049370: (within /home/bart/Projects/oculusd/src/oculusd)
> ==2765== Address 0x4150F024 is bytes inside a block of size 4 free'd
>
> ==2765== at 0x40029079: realloc (vg_replace_malloc.c:310)
> ==2765== by 0x8049B98: string_array_add (generic.c:114)
> ==2765== by 0x8049434: main (main.c:32)
> ==2765== by 0x403EC90B: __libc_start_main (in /lib/libc-2.3.2.so)
> nul: (null)
>
> This is where the weird stuff is. This read 'nul: first line' in the
> valgrind-less run.
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by: IBM Linux Tutorials
> Free Linux tutorial presented by Daniel Robbins, President and CEO of
> GenToo technologies. Learn everything from fundamentals to system
> administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
> _______________________________________________
> Valgrind-users mailing list
> Val...@li...
> https://lists.sourceforge.net/lists/listinfo/valgrind-users
|