Thread: Memory issue

  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
    Chappell Hill, Texas
    Posts
    2,332
    328K is not that much memory. How much memory is on your machine?
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Chappell Hill, Texas
    Posts
    2,332
    Could there by another issue, like an uncontrolled loop through the malloc() code?
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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
    Chappell Hill, Texas
    Posts
    2,332
    What code? I don't see no stinkin' code.
    Mainframe assembler programmer by trade. C coder when I can.

  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,336
    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, 09:58 AM
  2. What's the difference?
    By Stonehambey in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 10:26 AM
  3. threads and memory issue
    By Anubhav in forum C Programming
    Replies: 6
    Last Post: 07-25-2006, 04:51 AM
  4. Memory Leak Help
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 06-19-2006, 11:22 AM
  5. Accessing Video Memory Information...need help
    By KneeLess in forum C++ Programming
    Replies: 8
    Last Post: 08-24-2003, 03:53 PM