Segmentation fault before return
Posted
by donalmg
on Stack Overflow
See other posts from Stack Overflow
or by donalmg
Published on 2010-03-19T13:05:57Z
Indexed on
2010/03/19
13:11 UTC
Read the original article
Hit count: 216
c++
|segmentation-fault
Hi,
Why does the following code seg fault before returning:
int main()
{
char iD[20];
memset (iD, 0, 20);
char* prefix;
srand (time(NULL) );
int iPrefix = rand()%1000000;
sprintf(prefix, "%i", iPrefix);
int len = strlen(prefix);
char* staticChar = "123456789";
//set prefix into ID
memcpy(iD, prefix, len);
// append static value
memcpy(iD+len, staticChar, 20-len);
cout << "END " << endl;
return 0;
}
At the minute, the cout will display, but I get a segmentation fault.
© Stack Overflow or respective owner