realloc(): invalid next size

  • Thread starter solanki.chandrakant
  • Start date
S

solanki.chandrakant

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
 
R

Richard Heathfield

(e-mail address removed) said:
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.

Please take the time to write readable English, with proper case
distinctions and spellings and so on. It is so much easier to read. Thank
you.

The answer to your problem is that you have forgotten to null-terminate the
string in your strcats() function.

There are several other minor problems which I will go into if you wish, but
at present you are likely to see them as a pointless distraction. But I
will just say this: given your willingness to hurl string literals at
strcats(), I recommend that you make its parameter const char *.
 
H

Herbert Rosenau

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top