Memory freeing after exec call?

This is a discussion on Memory freeing after exec call? within the C Programming forums, part of the General Programming Boards category; This is for a school project, and I am looking for guidence on going about freeing memory of a parameter ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    8

    Memory freeing after exec call?

    This is for a school project, and I am looking for guidence on going about freeing memory of a parameter to an exec call. This isn't the point of the project, just a detail I am stuck on planning out. Its a license manager, only letting a specified number of process run at a time.

    It gets a line of text (char*) that I parse into an argv type array, char* arr[]. arr[0] is the command, and the rest of it are parameters.

    The array is made using malloc and calloc, so its memory that needs to be free'ed at some point. Code written for previous project, what we have to use.

    After I get this array arr, I call fork, then have the child call execvp(arr[0], arr). At that point, execution passes to the command in arr[0].

    I am wondering how I could go about making sure the memory is free'ed when the command is finished, short of a whole other control structure just tracking these arrays, unless I'm missing an STL that will do what I need.

    Only thing I have found so far is atexit(), but will it still work after the exec? I'm not at a point to test this myself yet, wondering if others have played with it.

    Anyone have pointers on how I could handle this, if atexit isn't the way?

    Seth

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    hmm, cant you free the memory right after the execvp() call? Im pretty sure the command line arguments get copied for the new process, so you dont have to worry about it finishing before you free the memory.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    8
    No, execvp overwrites the program on the stack with the command it was given.

    execvp(cmd, args); // This runs.
    fprintf(stderr, "Error, execvp failed."); // This line never runs, unless the previous command fails.

    Seth

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Yes, I was talking about the parent process.

    Something like this:

    Code:
    // malloc some memory here
    if(!fork())
      execvp();  // child process
    else
      // free memory here in parent process

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    8
    Yeah, but no guarentee that you free it after the child is all done with it, unless I'm jsut not thinking straight due to lack of sleep. But real close to what I came up with. It really helped to write the problem down.

    Parent forks child, then continues doing whatever.

    Child forks again. New child execvp's, New Parent does a wait, then free's memory. Thats my story and I'm sticking to it.

    Seth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Freeing memory
    By C_ntua in forum C Programming
    Replies: 17
    Last Post: 06-29-2008, 05:42 AM
  2. a noob freeing memory...
    By panos in forum C Programming
    Replies: 18
    Last Post: 11-01-2006, 10:15 AM
  3. Multiple types in lists, vectors or arrays.
    By megatron09 in forum C++ Programming
    Replies: 20
    Last Post: 08-31-2006, 02:54 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Pls help me to do this project in C I need source code
    By sureshmenon74 in forum C Programming
    Replies: 4
    Last Post: 10-04-2001, 07:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21