realloc(): invalid next size



hi

i have fedora linux 4 and i have simple realloc program which i am
included here... plz help me to overcome the realloc to pointer to
character variable. The program working 2 times but more than 2 times
it gives error : realloc() - invalid next size.

#include <stdio.h>
#include <stdlib.h>

char *source = '\0';

int strlens( char *name ){ /* find the length of given string */
int k = 0;
for( ; name[k] != '\0'; k++ ) ;
return k;
}

int strcats( char *name, int start ) { /* concatenate the string */
int l = strlens( name );
int i = start, j = 0, z;

if( NULL == ( source = realloc ( source, l * sizeof( char ) + 1 )) )
fprintf( stderr, "error: realloc failed.\n" );

l += start;
while( i != l )
source[i++] = name[j++];
source[i++] = '!';
return i;
}

int main( void ) { /* main function */
int length = 0, k;
char *string = "abcdefghijklmnopqrstuvwxyz";
for( k = 0; k < 5; k++ ) {
length = strcats( string, length );
fprintf( stderr, "Length : %d\tString : %s\n", length, source );
}
return 0;
}


Chandrakant

.



Relevant Pages

  • Re: struggling to use calloc and realloc
    ... calloc() is not a useful as it seems. ... particularly true for floating point and pointer types. ... return type of int. ... This is not a safe way of using realloc. ...
    (comp.lang.c)
  • Re: Problems with realloc()
    ... void quicksort(int a, int lo, int hi) ... the allocation routines use to keep track of dynamic allocations. ... And when realloc tries to use this corrupted data, ...
    (comp.lang.c)
  • Re: realloc(): invalid next size
    ... i have fedora linux 4 and i have simple realloc program which i am ... Is syntactical ok but irritating as '\0' is not a pointer value. ... you overwrites the content of the pointer to the source string. ...
    (comp.lang.c)
  • Re: Whats wrong?
    ... function and want to see the effect in the caller you need to ... use a triple pointer. ... int* to a function when you want to change an int variable owned by ... in FillI am using that realloc(), ...
    (comp.os.linux.development.apps)
  • Re: realloc() question
    ... The return-type of mainshould be "int". ... (Using void here will ... If realloc() returns NULL, the previously-allocated memory is ... Reading email is like searching for food in the garbage, ...
    (comp.lang.c)