Re: realloc(): invalid next size
- From: "Herbert Rosenau" <os2guy@xxxxxxxxxxxxx>
- Date: Tue, 31 Oct 2006 10:29:21 +0000 (UTC)
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!
.
- References:
- realloc(): invalid next size
- From: solanki.chandrakant@xxxxxxxxx
- realloc(): invalid next size
- Prev by Date: Re: finding how much the file system is full, from a C program?
- Next by Date: Re: need for an extra variable.
- Previous by thread: Re: realloc(): invalid next size
- Next by thread: need for an extra variable.
- Index(es):
Relevant Pages
|