segmentation fault in strcpy
#include<stdio.h>
#include<string.h> #include<malloc.h> int main() { char *dest; char *in="String1"; dest=(char*) malloc(20); dest="data1"; strcpy(dest,in); printf("%s",dest); free(dest); return 0; } output: Segmentation fault thanks & regards dayalan |
By assignment "data1" to dest, dest points to a buffer with 6 bytes, not 20 bytes.
|
Well, the problem is here:
Code:
dest=(char*) malloc(20); This is the kind of thing you need to do: Code:
#include<stdio.h> |
Quote:
#include<string.h> #include<malloc.h> int main() { char *dest; char *in="String1"; 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; } output: Segmentation fault please give me some more information. |
i have doubt in your answers can you please explain
Quote:
|
Here's my explanation:
Code:
dest="data1 data1 data1 da"; Code:
strcpy(dest,in); Code:
char dest[n]; |
:-/ The OP doesn't contain any questions.. just a piece of code, the output and
Quote:
|
Quote:
Also you should try turning on warnings (gcc -Wall -Wextra). You will get a warning if you try the assignment above. Quote:
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:
Code:
#include <stdio.h> |
Quote:
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. |
Quote:
use : Quote:
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. |
Quote:
Seriously though, why can't I write into src? from what I read, static is something which doesn't lose value between function calls.. |
Quote:
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? Thanks & Regards dayalan |
Quote:
Code:
char dest[]="this is just a test"; PS: I really don't understand why some members keep saying stupidities, when I already explained what was the real problem. |
When you do something like this:
Code:
char* c = "hello world"; 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 |
All times are GMT -5. The time now is 01:43 AM. |