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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strcat and memory

Status
Not open for further replies.

cmancre

Programmer
Jan 30, 2005
35
PT
code:
char *command=(char*)malloc(1024*sizeof(char));
char *tmp=(char*)malloc(10*sizeof(char));
memset(tmp,0,10);
tmp = strcat(tmp,"123456789");
.
.
.
fprintf(stdout,"\nbefore %s",tmp);
command=strcat(strcat(strcat(var1,var2),var3),var4);
fprintf(stdout,"\n%s",command);
fprintf(stdout,"\nafter %s",tmp);

problem:
tmp points that string numbers, and is never used till the other part of the code, the first printf prints correct the string, the variable command is also good to, bue the second time showing tmp it shows part of the command string, why is this???

 
I'm guessing it has to be:
Code:
command=strcat(strcat(strcat(var1,var2),var3),var4);
Try commenting out that line and see what happens.
If that solves the tmp corruption problem you'll have to look closer at var1, 2, 3 & 4...
 
> tmp it shows part of the command string, why is this???
Because you overflowed your string.

Without seeing how you declared your var1 to var4 variables, your post makes no sense.

> command=strcat(strcat(strcat(var1,var2),var3),var4);
Since you didn't strcat to command in the innermost call, you've just lost the memory you allocated.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top