Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6636311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:10:12+00:00 2026-05-25T23:10:12+00:00

So here is the bit of my code that’s giving me problems: void childProcessHandler(string

  • 0

So here is the bit of my code that’s giving me problems:

void childProcessHandler(string command){


int argCounter = 0;
for(int i=0; i!=command.size(); i++)
    argCounter+=( command.at(i) == ' ');

char * temp, *token;
char *childArgs[argCounter];

argCounter = 1;

temp = new char [command.size()+1];
strcpy (temp, command.c_str());

token = strtok (temp," ");
childArgs[0] = token;

while (token!=NULL)
{
    token = strtok(NULL," ");
    childArgs[argCounter] = token;
    argCounter++;
}
    
//delete[] temp; //Should remove token as well?

execvp(childArgs[0], childArgs);

cout<<"PROBLEM!"<<endl;
exit(-1);

}

In the main() method my code gets to a point where it forks() (the parent process then waits for the child to exit.) then the child process (process ID == 0 yes?) calls the method childProcessHandler with the user input (command to run + args) as it’s argument. Then I tokenize the user input and call execvp on it.

Everything compiles and executes. The line after execvp is never reached because execvp only returns when there is an error yes?

The project is to simulate a unix terminal however when I give it the command "date" nothing gets printed like it should… The child exits and the parent process resumes just fine however nothing is sent back up to the terminal window…

What am I doing wrong?

(Also we were "recommended" to use strtok to tokenize it but if anyone has anything simpler i’m open to opinions.)

THANKS!

EDIT

The above code works, for example, if I type in "date " instead of "date". I think there might be something fishy with the "tokenizer" not putting a null character at the end of the childArgs[] array. I’ll play around with that and thanks for the quick responses!

(Ninja edit, also commented out delete[] temp for the time being)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:10:12+00:00Added an answer on May 25, 2026 at 11:10 pm

    Your childargs vector of pointers point into the bytes allocated in the block of memory “temp”. When you free temp, you are removing the memory pointed to by the childargs pointers, possibly corrupting some of the values within your vector.

    Remove the call to delete[] to stop freeing the memory pointed to by the childargs pointers. You will not be leaking memory. Once you call exec_() your entire process image is replaced anyway. The only thing that survives a call to exec_() (for the most part) are your file descriptors.

    As a test, try something a bit more simple: After your call to fork() in the child, just call exec with the path to “date”. Make that work before fiddling with the parameter list vector.

    As another test, remove your call to exec, and print out your entire vector of pointers to make sure that your tokenizing is working the way you think it should. Remember that your final entry must be NULL so that you know where the end of the vector is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a little bit of Javascript that almost works correctly. Here's the code:
Here's the relevant bit of the source code: class Dice { String name ;
Here's a bit of code that is uploading a file: file_size = os.path.getsize('Tea.rdf') f
I'm trying to do an Outer Join in Symfony. Here's the bit of code
It seems like I had to inline quite a bit of code here. I'm
First, to make my job explaining a bit easier, here's some of my code:
I'm a bit confused here. Microsoft as far as I can tell claims that
I've got a bit of code that I started to manage, and it's begun
I found a bit of code that gets me access to the raw pixel
I have (well had) a bit of code that would get me my lovely

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.