Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using "new"

Status
Not open for further replies.

BeerFizz

Technical User
Jan 23, 2004
28
0
0
I am reading records from a very large file. Approx 15 MB.
I am using "new" to allocate memory for the structure each time I read a record. I test for null.

This works for a smaller file, in the 1MB range. But when I tried with a larger file, it appeared to hang (loop), or something.

Am I runing out of heap space? Is there away to allocate more memory/heap? Is there a different way of doing this all together?

Thanks
phil
 
Depends what you're doing with that memory.

If you're building a linked list of records, and searching from the head element each time, then yes, performance will tail off very rapidly as more records are added to the list.

--
 
Are you using delete afterwards to free the memory?

Skute

"There are 10 types of people in this World, those that understand binary, and those that don't!"
 
>Is there a different way of doing this all together?

Consider if storing the records in an array/vector is applicable. Then you could read it all in one go.

/Per
[sub]
"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."[/sub]
 
OK.

Yes I am building a linked list.

Yes, I do use "free" and walk down the list freeing each cell in the list.

This worked fine with smaller files, with a large file it appears to hang. I understand the performance issues, however, in this case, it hangs during the creation of the linked list. I can't tell how far it got.

Are there limitations to the size of the heap? Any other ideas?

All help appreciated.

Thanks
phil
 
free"?!?!?

The complement of new is delete.



/Per
[sub]
"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."[/sub]
 
pBI = pStartBI;
while (pBI->NextBI != NULL) {
p1BI = pBI;
pBI = pBI->NextBI;
delete p1BI;
}
delete pBI;

right, I am actually using delete.
 
Below is the code I am using to create the linked list. There would be, when complete, about 330,000 cells in the list. BatchItem is a struct with ints and CStrings. I am assuming the compiler knows what size the struct is...

pNxtBI = new BatchItem;
if (pNxtBI == NULL) {
MessageBox("New failed to allocate memory. Fatal Error",
"Memory Error", MB_ICONEXCLAMATION );
return;
}
pNxtBI->NextBI = NULL;
pBI->NextBI = pNxtBI;
pBI = pNxtBI;
 
To make things simple may I sugest you use the collections already provided by STL? If you're determined to use a linked list, there already is one implemented...see docs on std::list





/Per
[sub]
"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."[/sub]
 
Thanks PerFnurt and thanks to everyone else who responded.

Phil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top