|
-
February 27th, 2012, 08:43 PM
#8
Re: Segmentation fault and can't figure out why
 Originally Posted by jedipenguin
segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
cout << "Segment id is : " << segment_id << "\n";
It gave me: 4456489
Did you make those checks an integral part of your program, and not just "cout" some value? What if the value is -1, what do you do? It may not be -1 now, but run it on another machine, and what if it is -1? Do you halt the program?
Error checking and handling are supposed to be integrated into your program.
I had to change my printf()s to couts to make it stop the segmentation fault.
Code:
printf("*%s\n", segment_id);
What variable type is segment_id? Isn't it an int? So why are you fooling printf() into believing that segment_id is a NULL terminated string? That's why printf() crashes -- your variable type does not match the format specfier. The reason why cout works is that cout doesn't use this technique of format strings to print variables, i.e. cout is type-safe. With printf(), if your variable doesn't match the format specifier, the results are unpredictable.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; February 27th, 2012 at 08:47 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|