Find the answer to your Linux question:
Results 1 to 10 of 10
Hi, I'm stuck here with a segmentation violation error during execution. I got no error when compiling. I'm quite new to c++ but I beleive it has something to do ...
  1. #1
    Just Joined! SingleFemaleLawyer's Avatar
    Join Date
    Jul 2010
    Posts
    28

    Segmentation violation

    Hi, I'm stuck here with a segmentation violation error during execution. I got no error when compiling. I'm quite new to c++ but I beleive it has something to do with memory space ... related to arrays and pointers perhaps ? But I can't think of a solution ... my mind is still with thinking in "java"

    Code:
    string mensajeHTTP(char* opcion, char* url, int nseq)
    {
    
    char mensaje[256]; //I want to store the request here
    
    printf("Control 9");
    printf("Control 10");
    
    sprintf(mensaje, "%s %s HTTP/1.0\r\nCSeq: %s\r\n", opcion, url, nseq); //
    
    // Some more stuff here
    
    }
    PHP Code:
    Jess@linux:~/workspace/httpg++ request.cpp coding -o request.-lcustomxlib

    (Program works fine until here)
    Control 9
    Segmentation violation 

  2. #2
    RDU
    RDU is offline
    Just Joined!
    Join Date
    Aug 2010
    Posts
    89
    Problem is in sprintf (even if you don't see Controle 10 on the terminal, it seg fault before the display buffer is flushed).
    1.Are you sure the total length of the msg is less than 256 char ?
    2.Your third arg in sprintf is INT (nseq) but you used %s in format !

  3. #3
    Just Joined! SingleFemaleLawyer's Avatar
    Join Date
    Jul 2010
    Posts
    28
    Yes, you are right, the problem is with sprintf.

    It should be enough with 256, a normal request would look like :

    PHP Code:
    GET http://myserver.com/path/file HTTP/1.0
    CSeq
    which is about 50 char. And I also tried with this but same thing happened:

    Code:
    string mensajeHTTP(char* opcion, char* url, char nseq)
    {
    
    char* mensaje; //I want to store the request here
    
    printf("Control 9");
    The int is a typo sorry, it should be char but i was thinking about int when i posted

  4. #4
    RDU
    RDU is offline
    Just Joined!
    Join Date
    Aug 2010
    Posts
    89
    Are you sure the 3 string are null terminated correctly ?
    can you printf strlen of all 3 ?

  5. #5
    Just Joined! SingleFemaleLawyer's Avatar
    Join Date
    Jul 2010
    Posts
    28
    I did that and it seems normal to me :

    mensaje length is: 13

    opcion length is: 7

    url length is: 56

    cseq length is: 2

    But they are char* type not string.

  6. #6
    RDU
    RDU is offline
    Just Joined!
    Join Date
    Aug 2010
    Posts
    89
    So are you sure the probem is in the sprintf and not later when you convert the result back to string?

  7. #7
    Just Joined! SingleFemaleLawyer's Avatar
    Join Date
    Jul 2010
    Posts
    28
    Yes, I think the problem is there because after sprintd I added mor printfs and I got the violation error before even seeing Control 11 and control 12 on screen.

    Code:
    printf("Control 9");
    printf("Control 10");
    
    sprintf(mensaje, "%s %s HTTP/1.0\r\nCSeq: %s\r\n", opcion, url, nseq); //
    
    printf("Control 11");
    printf("Control 12");
    
    // Some more stuff here
    [OUTPUT]

    (...)
    Control 9
    Segmentation violation


    To convert mensaje to a string I use this I saw here in the forum :
    Code:
            std::stringstream ss;
            ss << mensaje;
            ss >> mensajeRTSP;

  8. #8
    Just Joined!
    Join Date
    Feb 2009
    Location
    Southport, England
    Posts
    31
    I've had nothing but hell with sprintf(). For me, it was all to do with the way I declared the destination char* string. I think I ended up dynamically allocating it using malloc() and it was fine.
    Last edited by lemons; 08-11-2010 at 06:55 PM. Reason: Added more description

  9. #9
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    First, please change your printfs to end in newlines:
    Code:
    printf("Control 9"); ==> printf("Control 9\n");
    This will ensure that your output gets flushed and written to the screen. In your output, you should be seeing:
    Code:
    Control 9
    Control 10
    Segmentation fault
    if the error is the sprintf(), so I think that your output is not getting flushed.

    Also, I'm getting confused about exactly what the function looks like, as you've posted several versions. Can you please copy and paste exactly what the code looks like currently?

    Your sprintf() looks fine, so more context should help.

    Thank you.
    DISTRO=Arch
    Registered Linux User #388732

  10. #10
    Just Joined! SingleFemaleLawyer's Avatar
    Join Date
    Jul 2010
    Posts
    28
    Hi, thank you all for your replys. I kept digging and I solved it buy deleting the executable file generated previously and building the program again from the start, so I guess that the problem came.

    When the variable nseq was changed from int to char it also needed modifying %s in sprintf . Or using int and %d. Somehow the compiler got stuck there

    Now both options are correct :

    Code:
    string mensajeHTTP(char* opcion, char* url, int nseq)
    {
    
    char mensaje[256]; //I want to store the request here
    
    printf("Control 9\n");
    printf("Control 10\n");
    
    sprintf(mensaje, "%s %s HTTP/1.0\r\nCSeq: %d\r\n", opcion, url, nseq); //
    
    // Some more stuff here
    
    }
    or

    Code:
    string mensajeHTTP(char* opcion, char* url, char nseq)
    {
    
    char mensaje[256]; //I want to store the request here
    
    printf("Control 9\n");
    printf("Control 10\n");
    
    sprintf(mensaje, "%s %s HTTP/1.0\r\nCSeq: %s\r\n", opcion, url, nseq); //
    
    // Some more stuff here
    
    }
    I always end the printfs with \n but I couldnt copy&paste so I ommited then when copying it by hand hehehe

    thank you guys, problem solved

    xxx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...