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);
}