S
Sheldon
Hi,
This program works when tested with gdb ( see results 1) but when used
in a larger program where 12 becomes 1500 there exists a problem when
freeing the memory ( see results 2). Can anyone give any advise here?
--------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
int main(void) {
int i;
char** arr;
arr = malloc(sizeof(char *)*12);
for (i = 0; i < 12; i++) {
arr = malloc(sizeof(char)*5);
}
if (!(arr)) {
printf("Failed to allocate memory\n");
return 0;
}
for (i = 0; i < 12; i++) {
strcpy(arr,"Thi");
printf("Arr[%d]\t%s\n",i, arr);
}
for (i = 0; i < 12; i++) {
free(arr);
}
free(arr);
return 1;
}
---------------------------------------------
Result 1:
% gdb a.out
GNU gdb 6.0-2mdk (Mandrake Linux)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "i586-mandrake-linux-gnu"...Using host
libthread_db library "/lib/tls/libthread_db.so.1".
(gdb) run
Starting program: a.out
Arr[0] Thi
Arr[1] Thi
Arr[2] Thi
Arr[3] Thi
Arr[4] Thi
Arr[5] Thi
Arr[6] Thi
Arr[7] Thi
Arr[8] Thi
Arr[9] Thi
Arr[10] Thi
Arr[11] Thi
Program exited with code 01.
(gdb)
Results 2:
free(): invalid pointer 0x40426814!
free(): invalid pointer 0x40442c74!
free(): invalid pointer 0x40426874!
free(): invalid pointer 0x40442c54!
free(): invalid pointer 0x404268d4!
free(): invalid pointer 0x40442cb4!
This problem eludes me. Any help is appreciated.
/S
This program works when tested with gdb ( see results 1) but when used
in a larger program where 12 becomes 1500 there exists a problem when
freeing the memory ( see results 2). Can anyone give any advise here?
--------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
int main(void) {
int i;
char** arr;
arr = malloc(sizeof(char *)*12);
for (i = 0; i < 12; i++) {
arr = malloc(sizeof(char)*5);
}
if (!(arr)) {
printf("Failed to allocate memory\n");
return 0;
}
for (i = 0; i < 12; i++) {
strcpy(arr,"Thi");
printf("Arr[%d]\t%s\n",i, arr);
}
for (i = 0; i < 12; i++) {
free(arr);
}
free(arr);
return 1;
}
---------------------------------------------
Result 1:
% gdb a.out
GNU gdb 6.0-2mdk (Mandrake Linux)
Copyright 2003 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and
you are
welcome to change it and/or distribute copies of it under certain
conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for
details.
This GDB was configured as "i586-mandrake-linux-gnu"...Using host
libthread_db library "/lib/tls/libthread_db.so.1".
(gdb) run
Starting program: a.out
Arr[0] Thi
Arr[1] Thi
Arr[2] Thi
Arr[3] Thi
Arr[4] Thi
Arr[5] Thi
Arr[6] Thi
Arr[7] Thi
Arr[8] Thi
Arr[9] Thi
Arr[10] Thi
Arr[11] Thi
Program exited with code 01.
(gdb)
Results 2:
free(): invalid pointer 0x40426814!
free(): invalid pointer 0x40442c74!
free(): invalid pointer 0x40426874!
free(): invalid pointer 0x40442c54!
free(): invalid pointer 0x404268d4!
free(): invalid pointer 0x40442cb4!
This problem eludes me. Any help is appreciated.
/S