Memory Allocation

This is a discussion on Memory Allocation within the C++ Programming forums, part of the General Programming Boards category; I need to know how I can truely allocate memory. I am writing a program that reads a text file, ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    3

    Angry 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

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    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
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    3

    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
    Attached Files Attached Files

  4. #4
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    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
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    3

    Memory Allocation

    Thanks buddy, I'll give it a try.

    Daniel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory allocation question
    By dakarn in forum C Programming
    Replies: 11
    Last Post: 12-01-2008, 10:41 PM
  2. Dynamic memory allocation.
    By HAssan in forum C Programming
    Replies: 3
    Last Post: 09-07-2006, 05:04 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. C memory allocation to c++
    By markucd in forum C++ Programming
    Replies: 2
    Last Post: 11-30-2005, 04:56 AM
  5. Understanding Memory Allocation
    By Ragsdale85 in forum C Programming
    Replies: 7
    Last Post: 10-31-2005, 07:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21