By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 888 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Memory Issue with strcat

P: 2
Say i have to character pointers

char *localbuf = NULL;
char *malbuf = NULL;

I now allocate memory for malbuf

malbuf = (char*)malloc(30);

I write a string into malbuf which is of length 29 bytes (so leaving room for the NULL termination.

I now concatenate this string with localbuf

strcat(localbuf, malbuf);

My Questions are?
What memory does localbuf use to store the string.....new memory malloced by the OS or is it that the pointer to malbuf is moved to localbuf?

If it is the former scenario, does freeing malbuf using the command free(malbuf) and again allocating memory for malbuf cause any corruption in localbuf?
Jun 6 '07 #1
Share this Question
Share on Google+
3 Replies


100+
P: 208
Say i have to character pointers

char *localbuf = NULL;
char *malbuf = NULL;

I now allocate memory for malbuf

malbuf = (char*)malloc(30);

I write a string into malbuf which is of length 29 bytes (so leaving room for the NULL termination.

I now concatenate this string with localbuf

strcat(localbuf, malbuf);

My Questions are?
What memory does localbuf use to store the string.....new memory malloced by the OS or is it that the pointer to malbuf is moved to localbuf?

If it is the former scenario, does freeing malbuf using the command free(malbuf) and again allocating memory for malbuf cause any corruption in localbuf?
Strcat just puts whatever is in malbuf into localbuf. I believe you still need to malloc size to store this string.

Freeing malbuf and then allocating memory to it won't cause any corruption in localbuf because you've already assigned it. (What I mean by this is no change in malbuff will have any effect on localbuf)

Correct me if I'm wrong :P
Jun 6 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Yes, Silent1Mezzo is correct.

You need to allocate localbuf as:

Expand|Select|Wrap|Line Numbers
  1. localbuf = (char*)malloc(sizeof(char) * (strlen(malbuf) + 1));
  2. localbuf[0] = '\0';
  3.  
I believe you need the \0 in localbuf for strcat to work. Otherwise, you do a strcpy() before you start the strcats.
Jun 6 '07 #3

100+
P: 208
Yes, Silent1Mezzo is correct.

You need to allocate localbuf as:

Expand|Select|Wrap|Line Numbers
  1. localbuf = (char*)malloc(sizeof(char) * (strlen(malbuf) + 1));
  2. localbuf[0] = '\0';
  3.  
I believe you need the \0 in localbuf for strcat to work. Otherwise, you do a strcpy() before you start the strcats.
Both strings should have \0 at the end of them. But it this specific case since theres nothing in localbuf to start off with it doesn't need a \0 (Doesn't hurt to have one though)

PS. My 100th post :D
Jun 6 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.