Structure Memory Allocation

This is a discussion on Structure Memory Allocation within the C Programming forums, part of the General Programming Boards category; Can anyone tell me how is memory allocated for an array of structure? For example, If I have a structure ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    28

    Structure Memory Allocation

    Can anyone tell me how is memory allocated for an array of structure? For example, If I have a structure like this:

    typedef struct particle { /* Each particle is defined in a struct */
    short q; /* charge */
    float xPos; /* x position */
    float yPos; /* y position */
    }PARTICLE;

    and in the main fuction I declare an array of this struct:

    PARTICLE e_p [N];

    How would the program store this array of particles in the memory? Thanks for help.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    a short is two bytes
    a float is four bytes
    so your struct would be 10 bytes long per N structs

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    28

    Smile

    Got it, Thanks a lot.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by ygfperson
    a short is two bytes
    a float is four bytes
    so your struct would be 10 bytes long per N structs
    Assuming there is no padding...

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    P.Phant
    Guest
    To complete what quzah mentionned. According to my very recently acquired knowledge, on a 32 bit proc, as is, your second member will be aligned to the fourth byte:

    q size: 2, relative offset: 0
    xPos size: 4, relative offset: 4
    yPos size: 4, relative offset: 8

    total size of the structure : 12

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. POSIX Threads and dynamic memory allocation
    By PING in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2009, 10:28 AM
  2. Mysterious memory allocation problem
    By TomServo1 in forum C Programming
    Replies: 7
    Last Post: 07-08-2007, 11:29 AM
  3. Help with file reading/dynamic memory allocation
    By Quasar in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2004, 03:36 PM
  4. Memory allocation at runtime?
    By electrolove in forum C Programming
    Replies: 6
    Last Post: 02-05-2003, 10:39 AM
  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

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