forums Computer Tutorials Computer Help and Spyware Removal File DatabaseUninstall Database Windows Startup Programs Database Computer Resources Computer Glossary Forums Computer Help and Spyware Removal
 

Welcome Guest ( Log In | Click here to Register a free account now! )



Register a free account to unlock additional features at BleepingComputer.com
Welcome to Bleeping Computer, 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.

 
Reply to this topicStart new topic
> C++ Dealing With Ram
Billy O'Neal
post May 11 2008, 12:02 AM
Post #1


Big Brother is Watching You
******

Group: HJT Senior Classmen
Posts: 1,714
Joined: 17-January 08
From: Airstrip One
Member No.: 184,215



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:
CODE
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];
strcpy(holder,command);
strcat(holder," > tempfile.txt");
ofstream tempfile("tempBat.bat",ios::trunc);
tempfile << holder;
tempfile.close();
delete [] holder;
STARTUPINFOA si = { sizeof(STARTUPINFOA) };
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
PROCESS_INFORMATION pi;
CreateProcessA(NULL,"cmd /C tempBat.bat",NULL,NULL,false,CREATE_NO_WINDOW,NULL,NULL,&si,&pi);
do
{
GetExitCodeProcess(pi.hProcess,&result);
Sleep(50);
} while (result == STILL_ACTIVE);
TerminateProcess(pi.hProcess,0); //needed because I will hang at program exit otherwise
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
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
//
file.seekg(0,std::ios_base::end);
length = file.tellg();
file.seekg(0,std::ios_base::beg);
//
//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';
file.close();
//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!

Billy3


--------------------
In the event I fail to reply within twenty-four hours, feel free to send me a PM.
Have I helped you? If so, please sign My Guestboox to help me get into college!
Join BC.com's Folding Team (#38444)! Help Stanford University find a cure for diseases!
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 5th July 2008 - 06:22 PM


Advertise   |   About Us   |   Terms of Use   |   Privacy Policy   |   Contact Us   |   Site Map   |   Chat   |   Tutorials   |   Uninstall List
Discussion Forums   |   The Computer Glossary   |   Resources   |   RSS Feeds   |   Startups   |   The File Database   |   Malware Removal Guides

© 2003-2008 All Rights Reserved Bleeping Computer LLC.