Jump to content


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.

Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.


C++ Dealing With Ram

  • Please log in to reply
No replies to this topic

#1 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer

  • Malware Response Team
  • 12,304 posts
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:10:14 PM

Posted 11 May 2008 - 12:02 AM

Hello. I am trying to return data from a function which is an array of arbitrary length, without causing a memory leak. I am writing a function which allows one to pass it an argument of any line you would normally execute in a batch file, run it in the background in an invisible window, and then return the console output of the program. What I am currently doing is allocating a new hunk of ram each time I execute this process. When the function returns the pointer, I have no way of knowing if I need to delete the pointer or not.

Is there some fundimential thing about C++'s argument behavior I'm missing here?

Here's what I'm currently doing:
[codebox]char* getTextOfCommand(const char command[]){
//adds "> tempfile.txt" to the command string, executes it
//reads the file to a string, deletes the file, and returns the string.
char *holder;
char *TempCharPTR;
streamsize length;
DWORD result;
holder = new char[strlen(command)+40];
strcat(holder," > tempfile.txt");
ofstream tempfile("tempBat.bat",ios::trunc);
tempfile << holder;
delete [] holder;
si.wShowWindow = SW_HIDE;
CreateProcessA(NULL,"cmd /C tempBat.bat",NULL,NULL,false,CREATE_NO_WINDOW,NULL,NULL,&si,&pi);
} while (result == STILL_ACTIVE);
TerminateProcess(pi.hProcess,0); //needed because I will hang at program exit otherwise
Sleep(100); //give windows time to completely close the file before opening it
std::ifstream file("tempfile.txt",std::ios::in);
//Get the length of the string
length = file.tellg();
//Allocate ram for buffer
holder = new char[length+1]; // 1 for the null term
//Dump the file to ram
TempCharPTR = holder;
while (file.get(*TempCharPTR)) {TempCharPTR++; };
*TempCharPTR = '\0';
//Delete the temp file
Sleep(100); //make sure windows is done with the file from our last close before we delete it
system("del /q /f tempfile.txt");
system("del /q /f tempBat.bat");
return holder;

Thanks, all!

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

BC AdBot (Login to Remove)


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users