[RESOLVED] Segmentation fault and can't figure out why
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: [RESOLVED] Segmentation fault and can't figure out why

  1. #1

    [RESOLVED] Segmentation fault and can't figure out why

    It seems to be going null. I can't get a null value, I want it to accept input till the user hits enter with no text typed. I've tried checking to see if the input is NULL, "", and "\n" all to no avail.

    Code:
    #include <stdio.h>
    #include <sys/shm.h>
    #include <sys/stat.h>
    #include <iostream>
    #include <fstream>
    #include <string.h>
    #include <cstring>
    
    using namespace std;
    int main()
    {
      string info = "";
      cout << "Enter some info. \n";
     
      int counter;
      string newInfo = "";
      counter = 0;
      cout << counter;
      bool noMore = false;
      while(noMore == false)
        { // begin while
      
          if (counter == 0)
    	{ // begin if
    	getline(cin, info);
         
            cout << info.c_str();
            
    	  if (info.c_str() == "")
    	    { // begin if
    	    cout << "Went here.";
    	      noMore = true;
    	    } // end if
    
    	  else
    	    counter++;
    	    } // end if
    	  else
    	    { // begin else
    	      getline(cin, newInfo);
    	      
    	      cout << newInfo.c_str();
    	      
    	      if (newInfo.c_str() == "")
    		{ // begin if
    		 noMore = true;
    		} // end if
    
    	      else
    		{ // begin else
                  info = info + "\n" + newInfo;
    	      counter++;
    		} // end else
    	    } // end else
    	  
    	} // end while
        
        if (info.c_str() == "\n")
        {
        cout << "Abnormal termination.";
        return 1;
        }
      
      /* the identifier for the shared memory segment */
      int segment_id;
      /* a pointer to the shared memory segment */
      char *shared_memory;
      /* the size (in bytes) of the shared memory segment */
      const int size = 4096;
    
      /* allocate a shared memory segment */
      segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
      /* attach the shared memory segment */
      shared_memory = (char *) shmat(segment_id, NULL, 0);
      /* write a message to the shared memory segment */
    
      sprintf(shared_memory, info.c_str());
      
      /* now print out the string from shared memory */
      printf("*%s\n", shared_memory);
      
      printf("*%s\n", segment_id);
      
      /* now detach the shared memory segment */
      shmdt(shared_memory);
      
      return 0;
    
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Segmentation fault and can't figure out why

    Code:
    info.c_str() == ""
    This will not do what you think it does. Try:

    Code:
    info == ""
    or just
    Code:
    info.empty()
    The c_str() call is also unnecessary for cout.

  3. #3

    Re: Segmentation fault and can't figure out why

    I did and it's still giving a segmentation fault.

    What is wrong with this line:

    printf("*&#37;s\n", shared_memory);
    Last edited by jedipenguin; February 27th, 2012 at 06:01 PM.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Segmentation fault and can't figure out why

    I'm not very familiar with shared memory operations. Just to confirm, though, you did make those changes I suggested throughout the code, not just at that one line, right?

  5. #5

    Re: Segmentation fault and can't figure out why

    Quote Originally Posted by Lindley View Post
    I'm not very familiar with shared memory operations. Just to confirm, though, you did make those changes I suggested throughout the code, not just at that one line, right?
    Yes, I think so. It's getting down to that line I just pointed out. My couts are outputting till it reaches that point.

    I'm wondering, though the book has a printf() in it's example, if I should change it to a cout or something.

    I'm going to see if shared_memory is somehow NULL.

    Why is it going to the if block in the while loop and assuming that I entered nothing. It's outputting, "Went here." even when the first time I enter something other than a newline.

    Never mind on that. It's going through the loop I think. It's because of my spacing. I didn't see that it said "I went here." However, I don't know why it's going NULL or whatever is going wrong.

    Is it something with my shared_memory variable?
    Last edited by jedipenguin; February 27th, 2012 at 06:15 PM.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Segmentation fault and can't figure out why

    Quote Originally Posted by jedipenguin View Post
    Yes, I think so. It's getting down to that line I just pointed out. My couts are outputting till it reaches that point.
    You don't check any of those functions to see if they returned a success code. You just assume they work, which is not correct.

    http://pubs.opengroup.org/onlinepubs...ns/shmget.html

    From the link above:
    RETURN VALUE

    Upon successful completion, shmget() shall return a non-negative integer, namely a shared memory identifier; otherwise, it shall return -1 and set errno to indicate the error.
    Where is your check to see what shmget() returns? Not only this function, but all the other functions such as shmat -- where do you check for error values?

    http://linux.die.net/man/2/shmat

    Please read the documentation for your functions, and especially the section that describes error codes if a failure occurs.

    The reason why your program crashes is that you're attempting to dereference an invalid address. You can't call printf() on a pointer that is pointing to garbage -- the results will be unpredicatable.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; February 27th, 2012 at 06:26 PM.

  7. #7

    Re: Segmentation fault and can't figure out why

    Quote Originally Posted by Paul McKenzie View Post
    You don't check any of those functions to see if they returned a success code. You just assume they work, which is not correct.

    http://pubs.opengroup.org/onlinepubs...ns/shmget.html

    From the link above:
    Where is your check to see what shmget() returns? Not only this function, but all the other functions such as shmat -- where do you check for error values?

    http://linux.die.net/man/2/shmat

    Please read the documentation for your functions, and especially the section that describes error codes if a failure occurs.

    The reason why your program crashes is that you're attempting to dereference an invalid address. You can't call printf() on a pointer that is pointing to garbage -- the results will be unpredicatable.

    Regards,

    Paul McKenzie
    segment_id = shmget(IPC_PRIVATE, size, S_IRUSR | S_IWUSR);
    cout << "Segment id is : " << segment_id << "\n";

    It gave me: 4456489

    I had to change my printf()s to couts to make it stop the segmentation fault.

    It successfully wrote to the shared memory because when I had another program read from it, it got all the data.

    However, why was my printf going wrong? Was it a bad formatting for the first argument to the method?

  8. #8
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Segmentation fault and can't figure out why

    Quote Originally Posted by jedipenguin View Post
    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("*&#37;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 07: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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center