|
From: Bart F. <ba...@fr...> - 2004-03-27 17:51: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.
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");
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, 0 otherwise
*/
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;
}
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 0 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.
|
|
From: Henrik N. <hn...@ma...> - 2004-03-27 22:17:18
|
On Sat, 27 Mar 2004, Bart Friederichs wrote: > 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. If valgrind detecte memory errors then there is a high likelyhood your application will run differently as valgrind manages freed memory differently to better detect memory misuse. Regards Henrik |