1

Why this below code gives segmentation fault?

int main()
{
        char *t = "Working on RedHat Linux";
        char *s;

        s = malloc (8000 * sizeof(char));
        memcpy(s,t,7000);  
        printf("s = %s\nt = %s\n",s,t);
        free(s);
}

I have allocated 8000bytes for 's'. And copying only 't' to s untill 7000bytes. Though I have allocated 8000 bytes for 's', why its giving segmentation fault?

1
  • You can also look at strcpy or strncpy for string copying. Commented Jan 2, 2012 at 18:31

4 Answers 4

11

The segmentation fault is because t points to a region smaller than 7000 bytes. You are probably trying to read into an area when no readable page is mapped (after the end of string literal "Working on RedHat Linux"). You should limit your memcpy to sizeof("Working on RedHat Linux") bytes.

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

Comments

4

Your program exhibits undefined behavior: for memcpy() to work, both the source and the destination must be addressable for the number of bytes you've specified.

You've satisfied the destination, but not the source part.

Also, you can remove sizeof(char) as it is defined by the standard to always be 1.

6 Comments

Hey Employed Russian, thanks for the reply. I dint get the statement "You've satisfied the destination, but not the source part". Why do I need to satisfy source part?
@NikhilTej: Because t is less then 7000 bytes long.
@NikhilTej - you have 24 bytes and you're telling it to copy 7000. Once it reaches the end of the 24 bytes you actually have it is then off into uncharted (undefined) territory. You have no idea what is in memory beyond the 24 bytes of your string literal (t).
@NikhilTej You need to satisfy the source part because memcpy requires that you do (else you get undefined behavior).
@Brian Roach - moreover you have no idea if memory is accessible beyond those 24 bytes. Segmentation fault occurs because memcpy tries to access nonaccessible memory.
|
2

Use:

memcpy(s, t, strlen(t) + 1);

to avoid memcpy reading past the string literal array.

The C standard says regarding string functions (memcpy is a string.h function) (C99, 7.21.1p1).

"If an array is accessed beyond the end of an object, the behavior is undefined."

Comments

1

t points to a String buffer of length 24, but in memcpy you are trying to copy more than that (7000) which do not exist.

You are trying to access memory beyond what is allocated. So it is giving a segmentation fault

8 Comments

The length of the string is 23. The size of the string literal array is 24.
yes correct, i meant the total length of the buffer, not the strlen
Hey Sumit Jain, I have allocated memory of 8000 bytes for s. But copying only till 7000 bytes. Then why it should give segmentation fault
@NikhilTej: Because you are reading way past the end of the buffer
yes but that is destination, your source is only 24 bytes, both of them must be greater than equal to the number of bytes to be copied
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.