Struct memory issues

This is a discussion on Struct memory issues within the C Programming forums, part of the General Programming Boards category; I've heard to use the heap/free store whenever possible when working with structs/classes and using the stack sparingly, but for ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    71

    Struct memory issues

    I've heard to use the heap/free store whenever possible when working with structs/classes and using the stack sparingly, but for a lot of open source C projects that use structs, they seem to use the stack. I don't really get it. Isn't that poor programming?

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    It isn't necesarily poor programming. The stack is just fine for a small, controlled amount of data. I use the heap when I don't know how much space I need, or the amount changes constantly. Structs are often used to pass multiple variables to a function, or just to group data together. In that case, I see no problem using the stack.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    71
    I see. What if I need to make several instances? Like a structure representing a character in a videogame. Such a structure would be used for possibly hundreds of such characters.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Yeah - I'd put that on the heap. Especially if you have any number of characters. For instance - an online game when people can join remotely and then leave whenever they want. You'd definitely need a dynamic structure on the heap.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    71
    I see. Well, the said open source projects I mentioned before included such games like the RPG type. So, that confused me.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It all depends on how much stack-space you actually have, and how much data you need to store.

    In modern PC OS's such as Windows or Linux, the stack is a couple of megabytes or so, by default.

    In an embedded system, the stack may range from a few hundred bytes to a few kilobytes.

    So, the size consideration is that if you need some space that would be a few kilobytes, in a modern PC OS, then it's fine to put it on the stack. If you need a few hundred kilobytes, then put it on the heap, because you are going to use up too much of the stack if you put that much data on the stack.

    If you are writing your game for an embedded OS (e.g. a mobile phone), then putting a few kilobytes of data on the stack may well be the equivalent of putting several hundred kilobytes on the stack in a modern PC OS.

    It is also worth remembering that "out of stack" is almost certainly a catastrophic failure. Running out of heap is possible to recover from (at least to some extent - your application probably has to quit, but it can do so with at least some sort of message to the user to say what went wrong, rather than a OS-generated "bad memory access" error).

    Another consideration is the actual life-time and usage of the data. If you have data that needs to persist from one function call to another, it is likely that you do not have any choice but to put it on the heap [or use global variables, but that is generally not a good option].

    If possible and not likely to cause a problem, but data on the stack. It saves a lot of trouble, as there is no need to call a function to "get" the memory, and there is no need to free the memory later on - it is automatically managed by the language itself.


    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    You mention classes... and even games. Classes are only available in C++, and many, if not most, games are written in C++... unless you found one written in C.
    So terminology clashes here... which language is it?
    OK, so it applies to both C and C++, but still...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory problem, maybe malloc struct...
    By s_siouris in forum C Programming
    Replies: 3
    Last Post: 07-11-2008, 09:34 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-02-2007, 12:48 AM
  4. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  5. Dynamic memory alloction for my array of struct
    By pears0 in forum C Programming
    Replies: 13
    Last Post: 03-11-2005, 11:53 AM

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