-
Memory Allocation
I need to know how I can truely allocate memory.
I am writing a program that reads a text file, and stores every string in an array. However, the size of the file will be unknown.
I have tried using new and delete, I have even tried to pre-read the file just to count the strings as I go. I have a 0 sucess rate.
Is there anything I can do to catch this.
PLEASE HELP
-
The new and delete operators should work depending on how you have your code structured. Post the code you are having difficulty with and maybe I can help.
Another possible method would be to use an STL data structure like a list to handle the memory allocation for you automatically. If you are familiar with data structures, I would suggest going this route.
David
-
Source code
Here is the source code, if you have any question let me know.
If I allocate a specific quantity of memory it works fine. It is when I try to determine how much to allocate, that is where I am having trouble.
I appreciate you looking at this.
Daniel
-
In your main function there is no need to close and reopen your ifstream object. All that you need to do is clear the EOF marker and set the file pointer position back to 0.
Code:
fin.clear();
fin.seekg(0);
The way you have it does not clear the EOF status and so nothing is read in again.
Also, you will need to change your logic for outputing the results to the screen and file. The nested for loop should probably a while loop. Here is an example:
Code:
// example for PrintSortedArray
while((j < c2-1) && (printing[j] == printing[j+1]))
{
times++; //frequency variable
j++;
}
// use similar for PrintArrayToFile
If you make these changes, it should clear things up. If youhave any further questions, let me know.
David
-
Memory Allocation
Thanks buddy, I'll give it a try.
Daniel