0
char * a = (char *) malloc(10); strcpy(a,"string1"); char * x = "string2"; strcat(a,x); printf("\n%s",a); 

Here, I allocated only 10B to a, but still after concatenating a and x (combined size is 16B), C prints the answer without any problem.

But if I do this:

 char * a = "string1"; char * x = "string2"; strcat(a,x); printf("\n%s",a); 

Then I get a segfault. Why is this? Why does the first one work despite lower memory allocation? Does strcat reallocate memory for me? If yes, why does the second one not work? Is it because a & x declared that way are unmodifiable string literals?

1
  • Undefined behaviour is undefined. You should never do, or depend on the result of, either of these. Commented Mar 20, 2014 at 11:26

4 Answers 4

2

In your first example, a is allocated in the heap. So when you're concatenating the other string, something in the heap will be overwritten, but there is no write-protection.

In your second example, a points to a region of the memory that contains constants, and is readonly. Hence the seg fault.

Sign up to request clarification or add additional context in comments.

2 Comments

Right, so what's the usual practice in such a case? Should I realloc(a,strlen(a)+strlen(x)) before strcat(a,x)?
Sounds like a good idea, but it would be strlen(a)+strlen(x)+1 (because of the null terminator)
0

The first one doesn't always work, it already caused an overflow. The second one, a is a pointer to the constant string which is stored in the data section, in a read-only page.

Comments

0

In the 2nd case what you have is a pointer to unmodifiable string literals,

In 1st case, you are printing out a heap memory location and in that case its undefined, you cannot guarantee that it will work every time.

(may be write it in a very large loop, yo may see this undefined behavior)

Comments

0

Your code is writing beyond the buffer that it's permitted, which causes undefined behavior. This can work and it can fail, and worse: it can look like it worked but cause seemingly unrelated failures later. The language allows you to do things like this because you're supposed to know what you're doing, but it's not recommended practice.

In your first case, of having used malloc to allocate buffers, you're actually being helped but not in a manner you should ever rely on. The malloc function allocates at least as much space as you've requested, but in practice it typically rounds up to a multiple of 16... so your malloc(10); probably got a 16 byte buffer. This is implementation specific and it's never a good idea to rely on something like that.

In your second case, it's likely that the memory pointed to by your a (and x) variable(s) is non-writable, which is why you've encountered a segfault.

3 Comments

Right, so what's the usual practice in such a case? Should I realloc(a,strlen(a)+strlen(x)) before strcat(a,x)?
Yes, calling realloc() is a reasonable option. Leave room for the NULL terminating byte in this case though, since the space is for concatenated strings.
@user1265125: Always allocate one char more for C-"strings" as they need to be 0-terminated: realloc(a, strlen(a) + strlen(x) + 1)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.