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 ...
- 08-11-2010 #1
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/http> g++ request.cpp coding -o request.o -lcustomxlib
(Program works fine until here)
Control 9
Segmentation violation
- 08-11-2010 #2Just 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 !
- 08-11-2010 #3
Yes, you are right, the problem is with sprintf.
It should be enough with 256, a normal request would look like :
which is about 50 char. And I also tried with this but same thing happened:PHP Code:GET http://myserver.com/path/file HTTP/1.0
CSeq: 2
The int is a typoCode:string mensajeHTTP(char* opcion, char* url, char nseq) { char* mensaje; //I want to store the request here printf("Control 9");
sorry, it should be char but i was thinking about int when i posted
- 08-11-2010 #4Just Joined!
- Join Date
- Aug 2010
- Posts
- 89
Are you sure the 3 string are null terminated correctly ?
can you printf strlen of all 3 ?
- 08-11-2010 #5
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.
- 08-11-2010 #6Just 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?
- 08-11-2010 #7
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.
[OUTPUT]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
(...)
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;
- 08-11-2010 #8Just 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
- 08-12-2010 #9
First, please change your printfs to end in newlines:
This will ensure that your output gets flushed and written to the screen. In your output, you should be seeing:Code:printf("Control 9"); ==> printf("Control 9\n");
if the error is the sprintf(), so I think that your output is not getting flushed.Code:Control 9 Control 10 Segmentation fault
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
- 08-12-2010 #10
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 :
orCode: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 }
I always end the printfs with \n but I couldnt copy&paste so I ommited then when copying it by hand heheheCode: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 }
thank you guys, problem solved
xxx


Reply With Quote