dynamic memory deletion via function

This is a discussion on dynamic memory deletion via function within the C++ Programming forums, part of the General Programming Boards category; Folks, Wondering if you could help out with the below segment of code. Basically I've written a little function that ...

  1. #1
    Registered User starkhorn's Avatar
    Join Date
    Sep 2003
    Posts
    21

    dynamic memory deletion via function

    Folks,

    Wondering if you could help out with the below segment of code. Basically I've written a little function that takes a string and converts it into a char array so that it can be used with the system command.

    In the function, I've used the new command to allocated memory for the char array which is enough to hold the length of the string.
    the function returns the character back to main where it gets allocated to a char pointer.

    Now what's missing is that I haven't deleted the memory that I've allocated using new and I'm bit of a loss of where to do it.

    Will calling the below in main, delete the memory that I allocated in the function ?

    Code:
     
    delete ptr[]
    As ptr should contain the same memory address as commands, then I'm thinking yes but I'm not 100% sure and I don't really want to take a chance of a memory leak.

    Is there a more efficient way to do this ?

    Cheers
    Brendan

    ps here is the segment of code


    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    char* convert_sz_char_array(string comm)
    {
    
    	char *commands = new char[(comm.length()+1)];
    	comm.copy(commands,comm.length());
    	commands[comm.length()]= NULL;
    	return commands;
    }
    
    
    					
    int main(int argc, char *argv[])
    {
    	string xpdf_exec_comm ="";
    	char *ptr;
    
    	ptr= (convert_sz_char_array(xpdf_exec_comm));
    	system(ptr);
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Call "delete[] ptr;" at the end of main and it will be deallocated. An alternative to using char pointers as strings (since you're using C++ and all) is the std::string which handles all memory allocation and deallocation by itself.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Just use the c_str() member function like this:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        string xpdf_exec_comm ="dir";
    
        system(xpdf_exec_comm.c_str());
    
        return 0;
    }
    BTW you would delete the memory like this:

    Code:
    if( ptr ) delete [] ptr;
    And yes you could put that in main if you wanted.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User starkhorn's Avatar
    Join Date
    Sep 2003
    Posts
    21
    Thanks guys. I had no idea about the c_str command which did exactly what I wanted....instead I went off and tried to write an inefficient function to do it.....my bad.

    Thanks a mill for your answer on the memory (and correcting my syntax on the delete command) and also for the c_str recommendation.
    Cheers
    Starkhorn

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Code:
    if( ptr ) delete [] ptr;
    No need for the if statement

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. POSIX Threads and dynamic memory allocation
    By PING in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2009, 10:28 AM
  4. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  5. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM

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