![]() |
| ||
| Weird segfault involving popen Hi guys It has been quite a long time since my last post, but I am having some very weird behaviour on a program I am writing and I was hoping to get a little community feedback ;) I am writing a class that basically opens a pipe to ksh runs a script gets the results and passes it back to the main program, the weird thing is that as the program terminates it segfaults I know it is related to the class that includes popen but I can't figure out why it is happening: This is the popen class: cpp #include <iostream> h #ifndef SHELL_INTERP_H And this is the calling code in the main function (sorry it is very dirty at the moment it is just test code Shell_Interp my_shell; As it stand the returned string is exactly as expected, it runs everything before this point and everything after this point without a problem, but when it exits I get a segfault, if I remove the popen code no more segfaults so it is within this code here. Any advice, help, pointers, questions, gratefully recieved. Thanks Ben |
| ||
| Re: Weird segfault involving popen files_to_open has no space reserved for it. You declared it as a char* but didn't 'new' it. So do one of these two things: char* files_to_open = new char[ kMaxFileSize ]; // don't forget to delete it! -or- char files_to_open[ kMaxFileSize ]; |
| ||
| Re: Weird segfault involving popen Quote:
char files_to_open[ kMaxFileSize ]; Causes failure to compile: shell_interp.cpp: In member function `int Shell_Interp::runshellscript(char*, std::string&)': shell_interp.cpp:31: error: cannot convert `char**' to `char*' for argument `1' to `int sprintf(char*, const char*, ...)' shell_interp.cpp:33: error: cannot convert `char**' to `const char*' for argument `1' to `FILE* popen(const char*, const char*)' However, char* files_to_open = new char[ kMaxFileSize ]; is perfect gets rid of the segfault completely. I hate to impose further but any chance you could give me an indication as to why this happened? I would like to learn why this works for future reference instead of just that it does :D Thanks Ben |
| ||
| Re: Weird segfault involving popen It sounds like you changed sprintf(files_to_open, "/usr/bin/ksh %s", filename); to sprintf(&files_to_open, "/usr/bin/ksh %s", filename); because that would be a char**, not a char*. Possible? |
| ||
| Re: Weird segfault involving popen Quote:
Ben |
| All times are GMT -4. The time now is 7:30 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC