Re: realloc(): invalid next size



On Tue, 31 Oct 2006 07:06:51 UTC, "solanki.chandrakant@xxxxxxxxx"
<Solanki.Chandrakant@xxxxxxxxx> wrote:

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';

Is syntactical ok but irritating as '\0' is not a pointer value.
Change to
char *source = NULL;

int strlens( char *name ){ /* find the length of given string

You invokes the namespace of the implementation here. Any name
starting with str followed by a lower case letter is reserved for the
implementation and not useable by you. By that strlen() does exactly
what you tries to do here.

*/
int k = 0;
for( ; name[k] != '\0'; k++ ) ;
return k;
}

int strcats( char *name, int start ) { /* concatenate the string */

You invokes the namespace of the implementation here. Any name
starting with str followed by a lower case letter is reserved for the
implementation and not useable by you.

int l = strlens( name );
int i = start, j = 0, z;

It is bad style (even as it is legal) to define more than one variable
in a single line.

if( NULL == ( source = realloc ( source, l * sizeof( char ) + 1 )) )

Here are 2 errors:
1. you overwrites the content of the pointer to the source string.
This will result in loosing it. realloc() returns NULL when
it is unable to serve you with memory of the new lengh. That
results in a memory leak.
Create a helper pointer to catch the result of realloc()
and copy it to source only if realloc returns not NULL.
You can, when realloc() returns NULL use the old data or
free() it when you have no need for.
2. you allocates only space enough to hold the string in name.
You means realloc(source, l + strlen(source) - start + 1)
l contains the lenth of name
as your loop below shows that you will overwrite some content
of source start is the lenth of the remaining string
strlen(source) - start gives you the remaining lengh of source
You must add 2:
- 1 for the '!' you adds when the loop below is finished
- 1 for the '\0' that you forgot to append after
you append '!' below.
sizeof(char) gives always 1 so it is superflous.


fprintf( stderr, "error: realloc failed.\n" );

l += start;

l contains at begin of this expression the lengh of the string in
name, not the length of source you assuemes here.

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



--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
.



Relevant Pages

  • Re: realloc() implicit free() ?
    ... >> another] what happens to the input block when realloc() finds it ... > you must instead use the output pointer value. ... The realloc function changes the size of the object pointed to by ... new, deallocate the old, and return a pointer to the new -- but I see ...
    (comp.lang.c)
  • Re: problem with freeing data
    ... I use realloc to allocate memory and grow my string until I get to ... I NULL the pointer and reseting the length of string back to zero, ... Stack struct has pointer "root" which points to the top of my stack. ... I again read how works realloc and I can see that I should be using it ...
    (comp.lang.c)
  • Re: Dynamic buffer library
    ... We do it this way instead of using a struct so that ... /* We now need to get the address of the buffer, because realloc() ... The address of the _pointer_. ... from and to all data pointer types, but void** canNOT validly point to ...
    (comp.lang.c)
  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... If your compiler ... it works with a cast. ... Pointer arithmetic, as you probably know, is scaled by ... not sure about concerning realloc(). ...
    (comp.lang.c)
  • Aliases in C
    ... we came into the realloc problem. ... At this point p is *invalid*, and also ALL its aliases. ... the call to DoSomeWork2uses an invalid pointer. ... if there is an alias for our ...
    (comp.lang.c)