Thread: Memory allocation

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    2

    Memory allocation

    Hi,


    If the question had been asked before, I apologize. Honestly I don't even know what's the correct term to search for is, and I'm a complete hack at C programming (have no formal education).


    I just wanted to know which structure would consume more memory, structure A or structure B. In the example I'm giving here, let's say there are 3 students, Amy, Joe and Jack, with particulars as follows:




    Name: Amy
    LastName: Blacksburg
    College_ID: 1
    Age: 18


    Name: Joe
    LastName: (not given)
    College_ID: (not given)
    Age: (not given)


    Name: Jack
    LastName: (not given)
    College_ID: (not given)
    Age: (not given)


    Code:
    ---Structure A ---
    Structure A is a simple struct.
    
    struct student_college_detail
    {
    char name[10];
    int college_id;
    char lastname[10];
    int age; 
    }student_data;
    In the above struct, if I am not mistaken, the size of the struct is 24 bytes for one data. So for the 3 students, Amy, Joe and Jack, then the size of the memory used by default would be initialized to 24*3 = 72.


    ---Structure B ---
    Since only Amy has full information on herself, I thought it would be a waste of memory for Joe and Jack because the memory for lastname, college_id and age would be allocated automatically at the outset even when there is no other info for Joe and Jack other than their Name. So I figured I would create a nested struct.

    Code:
    struct extra_details
    {
    int college_id;
    char lastname[10];
    int age; 
    };
    
    
    struct student_college_detail
    {
    char name[10];
    struct extra_details stu_details;
    }student_data;

    What I mean is that since Amy has the extra details and Joe and Jack don't, I thought I could save a bit of memory bcoz the stu_details would be initialized as NULL for Joe and Jack?
    i.e. AMy would have 24, Joe and Jack would be 2 each. So 24+2+2 = 28 bytes used. And 28 is much lower than 72 in the first example.


    p/s
    What I found out and the reason I'm asking here, upon running main() and checking using sizeof(student_data) for both examples, Structure B (the nested one) ended up using far more memory! It seems so conterintuitive that I thought I may have done something wrong (that I hope you could tell), or is my understanding on this whole memory allocation thing is totally wrong (if so, I'd appreciate if you could point me to a good direction to read up more).
    Or is there another way to really structure the Example so that memory is allocated only if information exist? Again, I'm a complete hack at programming, and I'd be very grateful for any enlightenment.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cencaluk
    What I found out and the reason I'm asking here, upon running main() and checking using sizeof(student_data) for both examples, Structure B (the nested one) ended up using far more memory! It seems so conterintuitive that I thought I may have done something wrong (that I hope you could tell), or is my understanding on this whole memory allocation thing is totally wrong (if so, I'd appreciate if you could point me to a good direction to read up more).
    You are probably observing the effects of padding for alignment, i.e., the amount of space occupied by a struct object may be more that the sum of the amount of space occupied by each of its members because there may be padding bytes in between.

    Quote Originally Posted by cencaluk
    In the above struct, if I am not mistaken, the size of the struct is 24 bytes for one data. So for the 3 students, Amy, Joe and Jack, then the size of the memory used by default would be initialized to 24*3 = 72.
    Because sizeof(char) == 1, it is certain that the name and lastname members will occupy at least 10 bytes each. However, sizeof(int) is at least 1 byte, typically at least two bytes, and probably 4 bytes, i.e., it is implementation defined.

    For me, sizeof(int) == 4, and sizeof(struct student_college_detail) == 32. This brings back the topic of padding: even though the sum of the sizes of the members is 10 + 4 + 10 + 4 == 28, I got 32 because of padding for alignment. I did a simple rearrangement of members:
    Code:
    struct student_college_detail
    {
        char name[10];
        char lastname[10];
        int college_id;
        int age;
    };
    and now sizeof(struct student_college_detail) == 28 because no padding was needed.

    Quote Originally Posted by cencaluk
    Since only Amy has full information on herself, I thought it would be a waste of memory for Joe and Jack because the memory for lastname, college_id and age would be allocated automatically at the outset even when there is no other info for Joe and Jack other than their Name. So I figured I would create a nested struct.
    That would not result in less memory being used: each student_college_detail object will contain a struct extra_details member for which memory will be allocated. All that you did was some rearrangement.

    Quote Originally Posted by cencaluk
    Or is there another way to really structure the Example so that memory is allocated only if information exist? Again, I'm a complete hack at programming, and I'd be very grateful for any enlightenment.
    Yes, at the cost of increased complexity. For example:
    Code:
    struct extra_details
    {
        int college_id;
        int age;
        char lastname[10];
    };
    
    struct student_college_detail
    {
        char name[10];
        struct extra_details *stu_details;
    };
    Now, stu_details is a pointer. So, if you need the extra details, you can malloc space for the struct extra_details object. But this means you have to consider that malloc could return a null pointer, and then you must call free at some point. Also, while you may reduce the memory used, you could also increase it: if all the records have these extra details, then not only is memory dynamically allocated for them anyway, but memory is also allocated for each stu_details pointer.

    Furthermore, you presented a very specific case where two out of three students don't need these "extra details". What if most students need them? What if a student record provides everything except last name? If you want to make everything optional, you could end up with:
    Code:
    struct student_college_detail
    {
        char *name;
        char *lastname;
        int *age;
        int *college_id;
    };
    This could lead to more complication. In short, I would suggest that you stick to the simpler version, unless you really have need of the complexity.

    By the way, you should avoid global variables.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    2
    Wow thanks very much for the concise but clear explanation!
    I'm happy to report that just by rearranging the members, the database I was working on last week showed a marked improvement on memory usage than prior (roughly 14,800K vs 16,400K). I know task manager may not be accurate and the memory usage isn't high to talk about, but I guess to a hack like me that's almost like magic. Haha. and very illustrative. Also, your clarification also lead me to read more on pointers, arrays, dynamic structs etc, which amongst others prodded me to use more arrays than linked list, for example.

    At any rate, thanks again for your reply and the education. Wishing you a happy new year ahead!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory allocation
    By johnmerlino in forum C Programming
    Replies: 1
    Last Post: 04-05-2014, 02:45 PM
  2. memory allocation help
    By moe8 in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 01:32 PM
  3. Memory Allocation
    By rahulsk1947 in forum C Programming
    Replies: 7
    Last Post: 02-25-2006, 01:06 AM
  4. Memory Allocation
    By Strut in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2002, 05:15 PM
  5. Memory Allocation
    By DutchStud in forum C++ Programming
    Replies: 3
    Last Post: 12-28-2001, 04:43 PM

Tags for this Thread