Reputation: 15
There is a tiny part of my code in C.
read(fd,&bufferSize,sizeof(bufferSize);
buffer = malloc(bufferSize);
read(fd,&buffer,bufferSize);
printf("%d",buffer);
fflush(stdout);
printf("%s",buffer);
fflush(stdout);
When I print buffer with %d
format, it's working, but when I try to consider the buffer as a string, I get segmentation fault. SIGSEGV occurs even when I use strcmp
or other functions like that.
Upvotes: 0
Views: 162
Reputation: 666
read(fd,&buffer,bufferSize);
Here, buffer
is the address of the start of your buffer. So, when you call the above function, you as passing in the address of your address.
Thus, you can see why it segfaults, because the address passed in is not the actual address of the buffer. Replace that line with
read(fd,buffer,bufferSize);
Upvotes: 3