ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
dest=(char*) malloc(20);
dest="data1 data1 data1 da"; --> now it is 20 bytes now also i am getting segmentation fault
strcpy(dest,in);
printf("%s",dest);
free(dest);
return 0;
}
i have doubt in your answers can you please explain
Quote:
Originally Posted by eerok
Well, the problem is here:
Code:
dest=(char*) malloc(20);
dest="data1";
The buffer addr value dest gets from malloc() is trashed by the string assignment, so you might as well not have used malloc() at all.
i accept your point malloc address has been changed due to "data1" string assignment why i have to remove the "data1" initilize to dest variable. i have some reason by assigning some string to pointer the pointer address is pointed to where the string kept in memory. why the strcpy is not copying to this address from source to destination address.
This is the kind of thing you need to do:
Code:
#include<stdio.h>
#include<string.h>
#include<malloc.h>
int main (void) /* don't use empty main() as per standard */
{
char *dest;
char *in = "String1";
dest = malloc (20);
/* don't cast malloc */ MALLOC WILL RETURN VOID* i think its better to have (char*) typecasting. what do you say.
strcpy (dest,"data1");
strcpy (dest,in);
printf ("%s\n", dest);
free (dest);
return 0;
}
Tiil now, all is correct. You've just made pointer dest to point to location where the string "data1 data1 data1 da" is (statically) stored.
Code:
strcpy(dest,in);
Now, you made the mistake. You're trying to modify the string "data1 data1 data1 da", by using a pointer. This is WRONG! To alter the string content, dest must be an array.
i accept your point malloc address has been changed due to "data1" string assignment why i have to remove the "data1" initilize to dest variable. i have some reason by assigning some string to pointer the pointer address is pointed to where the string kept in memory. why the strcpy is not copying to this address from source to destination address.
You don't initialize a buffer you've created with malloc(), you write to it, for the simple reason that only writing to it will work. (Sorry, mistake here: 'Your first try at assignment overwrote the pointer to your buffer because you needed to try '*dest' instead of 'dest' ... but even using the pointer properly in this case will still fail. Try it and see.')
Also you should try turning on warnings (gcc -Wall -Wextra). You will get a warning if you try the assignment above.
Quote:
/* don't cast malloc */ MALLOC WILL RETURN VOID* i think its better to have (char*) typecasting. what do you say.
All that matters with malloc() is the size of the block. Of course it returns a void pointer ... that means you don't have to cast it. The practice of casting malloc() is from the time before 'void' was implemented.
Put another way, once you assign the pointer from malloc to your variable that's been declared as a char pointer, what's left to worry about? Your char pointer is no less a char pointer.
More to the point, pigsa already gave you the answer to your original question, in the very first response:
Quote:
By assignment "data1" to dest, dest points to a buffer with 6 bytes, not 20 bytes.
That's not really the answer. The answer is that the assignment in question creates a string literal, which will cause a segfault when you try to overwrite it no matter how many bytes are involved. (String literals are stored in protected memory.)
I misexplained what was wrong in my remarks above, as I corrected.
Of course, you also get a segfault from 'free (dest);' after dest has been overwritten.
The result of a buffer overrun as mentioned would be "undefined behavior" ... it might work, crash, or segfault. I'm pretty sure the code didn't execute to that point, though.
When there's more than one thing wrong, it's sometimes hard to nail the pertinent one.
Distribution: Slackware 12 Kernel 2.6.24 - probably upgraded by now
Posts: 1,054
Rep:
Quote:
dest="data1";
Therein lies the nub of the error.
use :
Quote:
strcpy(dest,"data1");
This I think, should work fine. Following that , the free will also work correctly.
Some teacher taught me to always use strcpy , use something like what you have done only when variable has been declared.
Someone posted dest should be an array. Thats not true IMHO; cos when you did malloc you did as much.
Sorry , my english is not that good and I dont have access to GCC right now, so all this is from top of my head.
Seriously though, why can't I write into src?
from what I read, static is something which doesn't lose value between function calls..
hai duryodhan,
you are correct static is only preserve the values for the variable. but one thing i just want to remember you
" Someone posted dest should be an array. Thats not true IMHO; cos when you did malloc you did as much "
in the case of array the same code will goes through without segmentation fault. i have reason for you because when compiler does convert to object file that time the array is becomes read and write permissions but in the case of pointer i am assigning "data1" constant data this might has read permission but not for write so it causes segmentation fault occur. But still i need more information why pointer gaves me segmentation fault when i assinged "data1".
you want me to give program for the array implemented code?
...the compiler makes a static string "hello world" in the object file. This is in a read-only data segment (called .data). The variable named c, of type pointer to a char, is set to be the address of the first character of the string in the .data segment.
If you try to modify the address of the static string in the data segment, you get a segfault because that page of memory is marked as read-only.
So, in answer to the question "why does this make a segfault?", see comments here:
Code:
dest=(char*) malloc(20); /* After this, dest points to newly allocated
memory from the heap (or NULL on failure) */
dest="data1"; /* oops, you've discarded the address of your
malloc'd memory, and instead set dest to
point at the static string "data1" in the
.data segment (readonly)! */
strcpy(dest,in); /* strcpy tries to write to the address of
"data1". Segfault! */
...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.