I have smoke blowing outta my ears and a red head! ...I have this BIG problem in getting realloc to give me more memory! ...I have looked everywhere and tried everything, but my output is still the same!
Here we go: this is a small test program i have written just to get my head around how realloc works! ...basically, when you run the program, I want it to collect arguments from the command line, and read them into memory putting a '\n' between each arg! Simple i think (5 hours ago!) now its just doing my head in! ...well, here is the code....
Code:
/* test.c - test program to see how realloc works *
* requires: at least 1 arg, of any type, or length */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char **argv){
char *ptr; /* pointer to allocated memory */
int i = 0; /* loop counter */
unsigned int j; /* loop counter */
int chars = 0; /* counts number of chars in memory */
/* make sure there are args */
if (argc == 0)
return 1;
/* read the first argv element into memory using malloc */
ptr = malloc(sizeof(char) * strlen(argv[i]) + sizeof(char));
/* put args into memory, putting a '\n' between each arg */
for (i = 0; i < argc; i++){
/* allocate new memory to hold the arg */
ptr = realloc(ptr, ((sizeof(char) * strlen(argv[i]) + sizeof(char))));
/* occupy the allocated memory with arg data */
for (j = 0; j < strlen(argv[i]); j++){
*ptr = argv[i][j];
chars++;
ptr++;
}
/* add '\n' char to memory */
*ptr = '\n';
}
/* rewind ptr to begining of data and print */
ptr -= chars;
for (i = 0; i < chars; i++){
printf("%c",*ptr);
ptr++;
}
printf("\n");
/* finished! */
return 0;
} /* main */
and here is the output:
Code:
[root@localhost shell]# ./test
./test
so it works for one argument!
but then! if you use more than one! BANG!!!
Code:
[root@localhost shell]# ./test one
*** glibc detected *** ./test: realloc(): invalid pointer: 0x090a000e ***
======= Backtrace: =========
/lib/libc.so.6(__libc_realloc+0x2e6)[0x407f15]
./test[0x804848e]
/lib/libc.so.6(__libc_start_main+0xc6)[0x3b6de6]
./test[0x8048365]
======= Memory map: ========
00384000-0039e000 r-xp 00000000 fd:00 1327730 /lib/ld-2.3.5.so
0039e000-0039f000 r-xp 00019000 fd:00 1327730 /lib/ld-2.3.5.so
0039f000-003a0000 rwxp 0001a000 fd:00 1327730 /lib/ld-2.3.5.so
003a2000-004c6000 r-xp 00000000 fd:00 1327799 /lib/libc-2.3.5.so
004c6000-004c8000 r-xp 00124000 fd:00 1327799 /lib/libc-2.3.5.so
004c8000-004ca000 rwxp 00126000 fd:00 1327799 /lib/libc-2.3.5.so
004ca000-004cc000 rwxp 004ca000 00:00 0
00707000-00708000 r-xp 00707000 00:00 0
007db000-007e4000 r-xp 00000000 fd:00 1327803 /lib/libgcc_s-4.0.0-20050520.so.1
007e4000-007e5000 rwxp 00009000 fd:00 1327803 /lib/libgcc_s-4.0.0-20050520.so.1
08048000-08049000 r-xp 00000000 fd:00 1294304 /home/mconway/work/shell/test
08049000-0804a000 rw-p 00000000 fd:00 1294304 /home/mconway/work/shell/test
090a0000-090c1000 rw-p 090a0000 00:00 0 [heap]
b7f96000-b7f97000 rw-p b7f96000 00:00 0
b7faa000-b7fab000 rw-p b7faa000 00:00 0
bff95000-bffab000 rw-p bff95000 00:00 0 [stack]
Aborted
It is probably something really stupid that i have missed out or something! but any help is much appriciated! ...I want to use realloc in a bigger program i am working on, but at the moment, i am just counting all the chars in all the args and doing one big malloc in one go (because it works!) but i want to use realloc, so i thought i would find out how it works! BIG MISTAKE!!!!
Thanks for your help people!