Re: Segment fault ../sysdeps/i386/i686/multiarch/strcpy-sse2.S
Hi,
Dan Richard wrote
> char *str; strcpy(str, "hello");
You are writing to a random memory location.
The pointer str is not initialized to point to valid
memory, which you would have to allocate previously.
Try instead
char str[100];
strcpy(str, "hello");
This is good for strings up to 99 characters plus
trailing 0 as terminator.
If your string length is only predictable at run time
then use dynamic memory allocation
...
#include <stdlib.h>
...
char *str;
int lstr;
lstr= strlen(argv[1]);
/* >>> maybe check for absurd size */
str= calloc(1, lstr + 1);
if(str == NULL) {
/* >>> bail out with message about lack of memory */;
}
strcpy(str, argv[1]);
(Warning: Written from wetware memory. Not actually tested.
Take this only as sketch for the real code.)
Further reading:
Classic: Kernighan, Ritchie, The C Programming Language
Online: google "C language tutorial"
Recreational: http://crashworks.org/if_programming_languages_were_vehicles/
Have a nice day :)
Thomas
Reply to: