Thread: Why does this structure allocate memory?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    Why does this structure allocate memory?

    Hi,

    I came across following piece of code:

    Code:
    typedef struct{
      int a;
      char ch;
    }statusrecord; 
    
    typedef struct{
    	int b;
    	int c;
           statusrecord stRec[];
    } rsp;
      
    int main()
    {
    	rsp Francis;
    	printf("%d",sizeof(Francis));
    }
    Ouput :

    8 // using Visual C++

    1. Why doesn't storage space for the structure stRec is allocate?how does the memory model look like?
    2. How does one declare a dynamic structure?

    Thanks
    Last edited by sanddune008; 07-14-2009 at 05:04 AM.

  2. #2
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    1. An empty array, I bet it's UB
    2. Anything dynamic, use pointers

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    statusrecord stRec[];
    Why is your array empty ?

    Also there is no dynamic memory allocation going on in your code. The number produced by printf, once you specify the size of the array will be the size of the number of bytes of all the members of the struct added together.

    the sizeof statusrecord would be
    4 + 1 = 5 bytes + certain amount of padding added by the compiler. Lets assume its 3.
    that makes it 8 bytes.

    sizeof rsp = 4 + 4 = 8 bytes.

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    sorry the output is 8....

    But why the memory is not allocated to the array?

  5. #5
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    Quote Originally Posted by sanddune008 View Post
    sorry the output is 8....

    But why the memory is not allocated to the array?
    Why would it be if the array has no elements?

  6. #6
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Quote Originally Posted by Tux0r View Post
    Why would it be if the array has no elements?
    Then how will the memory model look like if following piece of code is added:

    Code:
    	for (i=0;i<5;i++)
    		anoop.stRec[i].a = i;

  7. #7
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    anoop.stRec[0].a = 0;
    anoop.stRec[1].a = 1;
    anoop.stRec[2].a = 2;
    anoop.stRec[3].a = 3;
    etc

    it will probably crash because you cannot access stRec[0] (element 1) if you want to you have to declare it as statusrecord stRec[1]; and statusrecord stRec[5]; for the loop to work.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by sanddune008 View Post
    Then how will the memory model look like if following piece of code is added:

    Code:
    	for (i=0;i<5;i++)
    		anoop.stRec[i].a = i;
    Undefined behavior.
    Incidentally, I don't think that your array is even standard compliant. It shouldn't compile.
    There are no such things as dynamic arrays in C. You have to code them yourself with dynamic memory.
    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.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You bet wrong, Tux0r - it's not undefined behaviour, unless you try to access non-existent array elements, but it is an ugly hack from the early days of C.

    The struct rsp has a zero-element array. However, if you dynamically allocate memory like this;
    Code:
    #include <stdlib.h>
    
    /*  sanddune's declarations  */
    
    int main()
    {
        rsp *with_two = malloc(sizeof(rsp) + 2 * sizeof(statusrecord));
        if (with_two != NULL)
        {
             /*  effectively with_two points at an rsp object that has two statusrecords */
    
            with_two->stRec[0].a = 5;      /*  OK as we have two statusrecords */
            with_two->stRec[0].ch = 'A';  /*  OK as we have two statusrecords */
            with_two->stRec[1].a = 5;     /*  OK as we have two statusrecords */
            with_two->stRec[1].ch = 'A';  /*  OK as we have two statusrecords */
            with_two->stRec[2].a = 5;     /*  Undefined behaviour as we have only two statusrecords */
            with_two->stRec[2].ch = 'A';  /*  Undefined behaviour as we have only two statusrecords */
            free(with_two);        
        }
     
        return 0;
    }
    The (claimed) advantage of this is that only one malloc() call is needed. If stRec was declared as a pointer, it would be necessary to do this.
    Code:
    int main()
    {
        rsp *with_two = malloc(sizeof(rsp) + 2 * sizeof(statusrecord));
        if (with_two != NULL)
        {
             with_two->stRec = (statusrecord *)((char *)rsp + sizeof(rsp));
    
             /*  other manipulations of elements of with_two->stRecstuff as per previous example */
    
            free(with_two);
        }
    }
    or
    Code:
    int main()
    {
        rsp *with_two = malloc(sizeof(rsp));
        if (with_two != NULL)
        {
             with_two->stRec = malloc(2*sizeof(statusrecord));
    
             if (with_two->stRec != NULL)
             {
    
                 /*  other manipulations of elements of with_two->stRecstuff as per previous example */
    
                  free(with_two->stRec);
             } 
            free(with_two);
        }
    }
    Last edited by grumpy; 07-14-2009 at 05:17 AM.

  10. #10
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Quote Originally Posted by sanddune008 View Post
    Then how will the memory model look like if following piece of code is added:

    Code:
    	for (i=0;i<5;i++)
    		anoop.stRec[i].a = i;
    Firstly where exactly did this anoop come from?

    Secondly, stRec is supposed to be an array. So, you specify a size for it, like this -
    stRec[5].

    Then you can initialize like you did in your code.

  11. #11
    Registered User
    Join Date
    Apr 2009
    Posts
    145
    Quote Originally Posted by Spidey View Post
    Firstly where exactly did this anoop come from?
    Copy paste advantage......

  12. #12
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by sanddune008 View Post
    Hi,

    I came across following piece of code:

    Code:
    typedef struct{
      int a;
      char ch;
    }statusrecord; 
    
    typedef struct{
    	int b;
    	int c;
           statusrecord stRec[];
    } rsp;
      
    int main()
    {
    	rsp Francis;
    	printf("%d",sizeof(Francis));
    }
    Ouput :

    8 // using Visual C++

    1. Why doesn't storage space for the structure stRec is allocate?how does the memory model look like?
    2. How does one declare a dynamic structure?

    Thanks
    Forget about size of the struct, my question is how the hell is this code even getting compiled without any size of the array stRec[]. Someone please throw some light on it.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  13. #13
    C++11 User Tux0r's Avatar
    Join Date
    Nov 2008
    Location
    Sweden
    Posts
    135
    C has its ways.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    C has a lot of old, and nowadays, bad practice stuff. A history full of legacy...
    That's what happens when you have backwards compatibility...
    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.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by BEN10 View Post
    Forget about size of the struct, my question is how the hell is this code even getting compiled without any size of the array stRec[]. Someone please throw some light on it.
    Refer my previous post in this thread.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  2. allocate apecified memory location for a c variable
    By BharathKumar in forum Linux Programming
    Replies: 5
    Last Post: 06-01-2007, 03:47 PM
  3. free memory in structure
    By franziss in forum C++ Programming
    Replies: 22
    Last Post: 01-08-2007, 05:16 PM
  4. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 04:14 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM