Hi Daniweb,

I'm making a shell-ish console for my Operating Systems class. Trying to get the shell to Terminate a process given a process name. Ever since I made the following addition to my code, this error is coming:

Program received signal SIGSEGV, Segmentation fault.In __gnu_cxx::__exchange_and_add(int volatile*, int) () ()

The code is here:

 //...branches for other input commands

 //branch for "terminate xxx" command
 else if(cmdword.compare(commands[9]) == 0){
                    string id = " ";

                    //seperates and returns the id or name of the process
                    id = parseString(command);

                    //to check whether we have an id or a name
                    bool allnumb = true;
                    for(int i = 0; i<id.length(); i++){
                        if(id.at(i) < 48 || id.at(i) > 57){
                            allnumb = false;

                    //if it is apparently an id
                        //function terminates the process with this id
                        //throws error if no such process. Is in working condition.
                    //if it is apparently a name
                        int pindex = -1;
                        //find the first such pid that corresponds to the name given
                        for(int i = 0; i<Processes.size(); i++){
                            if(Processes.at(i).pname.compare(id) == 0){
                                pindex = i;

                        //if no such pid, then just send the id/name substring. termprocess will see if theres an id, and throw an error otherwise
                        if(pindex < 0){
                        //if there is such a pid, fetch it, convert it from DWORD to string
                        //end send it to termprocess
                            char tpid[] = " ";
                            itoa(Processes.at(pindex).pid, tpid, 10);
                            string p_id(tpid);
//error handling and execution-loop footer

Googling has told me that there is some pointer referencing issue going on here. I have tried to pinpoint it but am lost. The thing is, the code runs, the process terminates, the "cout<<hellow" also executes. However, as soon as it crosses the following brace, the error pops up, ie "helllllllo" will not print.

Please help me find the source of this issue, thanks

UPDATE: The issue has been fixed. For anyone with a similar issue, this is the article that helped me: http://stackoverflow.com/questions/7038124/weird-sigsegv-segmentation-fault-in-stdstringassign-method-from-libstdc

To the best of my (incomplete)understanding, what is happening is that because I declared and initialized the tpid c string on line 43 with " ", it was fixed with a length of 2, and process names are often longer than that. This resulted in the overflowing of the contents of the array, and then the program crashed when some other variable tried to write itself on top of overflowing elements of tpid.

To solve this, one can either declare the c-string with ample length, or simply declare a pointer to char. Both worked for me

If there is a flaw in my interpretation, PLEASE do let me know to better my understanding of computing. Thanks :)

First, itoa() is a non-standard C/C++ language extension. Don't use it. Normally for C++, you would use something like sprintf() instead, like this:

char tpid[33];
int numchars = 0;
numchars = sprintf(tpid, "%d", (int)Processes.at(pindex).pid);
if (numchars > 32)
    // sprintf() returns number of chars output, not
    // including the trailing null byte, so check for
    // more than 32 for a 33 char buffer.