Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Memory problems when copying and uppercase a string

Status
Not open for further replies.

cadbilbao

Programmer
Apr 9, 2001
233
ES

Hello

I'm trying to copy into 'vble2' some of the chars of 'name1',
and afterwards, set 'vble2' to uppercase:

char *vble2 = NULL;
vble2 = (char*)malloc(strlen(name1)-5);
strncpy(vble2, name1, strlen(name1)-5);
strupr(vble2);
vble2[strlen(name1)-5] = '\0';
printf("\n vble2 = %s", vble2);
free(vble2);

I do not have any problem to compile it, but when printing
'vble2' I get a memory error in the program.

What am I doing wrong?

Thank you very much...
 
vble2[strlen(name1)-5] = '\0';

Is the culprit you are nulling the string and free has nothing to free.

Even then the output is full of garbage. You should consider rewriting this.


William
Software Engineer
ICQ No. 56047340
 
You are setting a value to null that does not exist. the malloc should be -4 not -5.

Matt

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top