Memory issue

This is a discussion on Memory issue within the C Programming forums, part of the General Programming Boards category; I have a generic application which basically reads a file following a specific format. This requires me to allocate memory ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    104

    Memory issue

    I have a generic application which basically reads a file following a specific format. This requires me to allocate memory depending on the file's attributes, and such. The maximum memory the file will ask me to allocate is 65535 bytes. But keep in mind, I allocate in 5 different sections, in which, the maximum bytes the file would ask me to allocate is 65535 bytes, as I said before.

    So basically, if the file has reached its maximum, I will have to allocate:
    65535 * 5 = 327675 bytes.

    malloc() obviously fails in one of these sections. A static array is not a solution either since it is also, too much memory.

    Basically, each one of the "members" that I have to allocate memory for are in the same structure. So in code, it is something like:
    Code:
    typedef struct {
        void *section_1;  /*Required max storage capacity: 65535 bytes*/
        void *section_2;  /*Required max storage capacity: 65535 bytes*/
        void *section_3;  /*Required max storage capacity: 65535 bytes*/
        void *section_4;  /*Required max storage capacity: 65535 bytes*/
        void *section_5;  /*Required max storage capacity: 65535 bytes*/
    } file;
    Any ideas?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    328K is not that much memory. How much memory is on your machine?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    I have 2GB RAM.

    I don't think malloc() fails due to an inability to find available memory, since there obviously is; it just fails after allocating a specific amount of memory.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by abraham2119 View Post

    malloc() obviously fails in one of these sections.
    [/code]

    Any ideas?
    How do you know that? Did the compiler generate any warnings that it cant allocate any more memory for the application ?

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Quote Originally Posted by roaan View Post
    How do you know that? Did the compiler generate any warnings that it cant allocate any more memory for the application ?
    Not the compiler, but the program spits a runtime error. I know malloc() fails since it returns NULL.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Could there by another issue, like an uncontrolled loop through the malloc() code?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Were I you, I would start by printing out the sizes as things get malloc'ed to see what's what.

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    My mistake everybody. I don't allocate 65535 bytes, I allocate 65535 structures. Some of them contain 10 bytes per structure, some of the sections more and some less.

    Is this simply stating that there is not sufficient memory on my computer? If so, what can I do to optimize the code.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    What code? I don't see no stinkin' code.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  10. #10
    Registered User
    Join Date
    Dec 2008
    Posts
    104
    Quote Originally Posted by Dino View Post
    What code? I don't see no stinkin' code.
    My "code" basically consists of the structure above, only that instead of void *, they are <other_struct> *.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There's nothing you can "optimize" about a data structure. It is what it is, and the only way anybody can tell whether its appropriate is to know what it's used for.

    You still seem a little unsure as to how much is actually getting allocated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help finding potential Memory issue
    By JoshNCSU22 in forum C Programming
    Replies: 9
    Last Post: 10-29-2008, 10:58 AM
  2. What's the difference?
    By Stonehambey in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 11:26 AM
  3. threads and memory issue
    By Anubhav in forum C Programming
    Replies: 6
    Last Post: 07-25-2006, 05:51 AM
  4. Memory Leak Help
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 06-19-2006, 12:22 PM
  5. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 04:53 PM

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