about structures memory allocation

Discussion in 'C Programming' started by bharath539@gmail.com, Jun 21, 2007.

  1. Guest

    how much memory is allocated for following structure
    struct bharath
    {
    int b;
    char c;
    float d;
    }

    and how?
    , Jun 21, 2007
    #1
    1. Advertising

  2. wrote:
    > how much memory is allocated for following structure
    > struct bharath
    > {
    > int b;
    > char c;
    > float d;
    > }
    >
    > and how?
    >

    Enough to fit at least the total memory requirements of the struct.
    Since the primitive types in the struct can vary in width from platform
    to platform and implementation to implementation (except char, of
    course) the size of the struct will also vary.

    How do you do it, or how is memory allocated at runtime? The former is
    just allocating for any type, really. If the latter, this is likely
    implementation specific, at least based on my lurking in this newsgroup,
    and my (poor) knowledge of the Standard.
    --
    clvrmnky <mailto:>

    Direct replies will be blacklisted. Replace "spamtrap" with my name to
    contact me directly.
    Clever Monkey, Jun 21, 2007
    #2
    1. Advertising

  3. said:

    > how much memory is allocated for following structure
    > struct bharath
    > {
    > int b;
    > char c;
    > float d;
    > }


    None. It's a type, not an object.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
    Richard Heathfield, Jun 21, 2007
    #3
  4. balu Guest

    On Jun 21, 7:33 pm, Richard Heathfield <> wrote:
    > said:
    >
    > > how much memory is allocated for following structure
    > > struct bharath
    > > {
    > > int b;
    > > char c;
    > > float d;
    > > }

    >
    > None. It's a type, not an object.
    >
    > --
    > Richard Heathfield
    > "Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
    > email: rjh at the above domain, - www.

    let the int size is 4B then the allocation will be?
    i want to know the info regarding padding or some other else related
    to padding?
    balu, Jun 21, 2007
    #4
  5. Chris Dollin Guest

    wrote:

    > how much memory is allocated for following structure
    > struct bharath
    > {
    > int b;
    > char c;
    > float d;
    > }
    >
    > and how?


    Objects of that type will have enough memory allocated to hold an
    int, a char, a float (in that order), plus any necessary padding.

    The details vary with the implementation -- which may include the
    option settings for your compilation(s).

    If you want to know how much has /actually/ been allocated, use
    `sizeof (struct bharath)`.

    --
    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
    Chris Dollin, Jun 21, 2007
    #5
  6. balu wrote:
    > On Jun 21, 7:33 pm, Richard Heathfield <> wrote:
    >> said:
    >>
    >>> how much memory is allocated for following structure
    >>> struct bharath
    >>> {
    >>> int b;
    >>> char c;
    >>> float d;
    >>> }

    >> None. It's a type, not an object.
    >>

    [Please use a newsreader that understands how to trim sigs, or trim them
    yourself. I've done this for you here.]

    > let the int size is 4B then the allocation will be?
    > i want to know the info regarding padding or some other else related
    > to padding?
    >

    This is exactly the sort of thing that people talk about when they say
    something is implementation specific. That is, once you actually
    allocate for a type, the specifics of how that memory is allocated is
    not necessarily dictated by the Standard.

    Simply defining (or is it declaring?) the type, of course, allocates no
    memory.
    --
    clvrmnky <mailto:>

    Direct replies will be blacklisted. Replace "spamtrap" with my name to
    contact me directly.
    Clever Monkey, Jun 21, 2007
    #6
  7. Guest

    On Jun 21, 7:49 pm, Clever Monkey <>
    wrote:
    > balu wrote:
    > > On Jun 21, 7:33 pm, Richard Heathfield <> wrote:
    > >> said:

    >
    > >>> how much memory is allocated for following structure
    > >>> struct bharath
    > >>> {
    > >>> int b;
    > >>> char c;
    > >>> float d;
    > >>> }
    > >> None. It's a type, not an object.

    >
    > [Please use a newsreader that understands how to trim sigs, or trim them
    > yourself. I've done this for you here.]
    >
    > > let the int size is 4B then the allocation will be?
    > > i want to know the info regarding padding or some other else related
    > > to padding?

    >
    > This is exactly the sort of thing that people talk about when they say
    > something is implementation specific. That is, once you actually
    > allocate for a type, the specifics of how that memory is allocated is
    > not necessarily dictated by the Standard.
    >
    > Simply defining (or is it declaring?) the type, of course, allocates no
    > memory.
    > --
    > clvrmnky <mailto:>
    >
    > Direct replies will be blacklisted. Replace "spamtrap" with my name to
    > contact me directly.


    what i want actually is how much size will be allocated for above
    structuer in a 16 bit machine
    , Jun 21, 2007
    #7
  8. Lew Pitcher Guest

    On Jun 21, 10:56 am, "" <>
    wrote:
    > On Jun 21, 7:49 pm, Clever Monkey <>
    > wrote:
    >
    >
    >
    > > balu wrote:
    > > > On Jun 21, 7:33 pm, Richard Heathfield <> wrote:
    > > >> said:

    >
    > > >>> how much memory is allocated for following structure
    > > >>> struct bharath
    > > >>> {
    > > >>> int b;
    > > >>> char c;
    > > >>> float d;
    > > >>> }
    > > >> None. It's a type, not an object.

    >
    > > [Please use a newsreader that understands how to trim sigs, or trim them
    > > yourself. I've done this for you here.]

    >
    > > > let the int size is 4B then the allocation will be?
    > > > i want to know the info regarding padding or some other else related
    > > > to padding?

    >
    > > This is exactly the sort of thing that people talk about when they say
    > > something is implementation specific. That is, once you actually
    > > allocate for a type, the specifics of how that memory is allocated is
    > > not necessarily dictated by the Standard.

    >
    > > Simply defining (or is it declaring?) the type, of course, allocates no
    > > memory.
    > > --
    > > clvrmnky <mailto:>

    >
    > > Direct replies will be blacklisted. Replace "spamtrap" with my name to
    > > contact me directly.

    >
    > what i want actually is how much size will be allocated for above
    > structuer in a 16 bit machine


    Here's the exact answer for you: your program will allocate
    sizeof(struct bharath) bytes. Other than that, we cannot tell you, as
    the exact number of bytes depends on the compiler, the operating
    system, and the processor.

    What we can tell you is that there may be padding characters inserted
    between each element of the structure to maintain data item alignment.
    If, *for instance*, ints were 4 bytes wide, and floats were 8 bytes
    wide, and both were required to be aligned on "even" boundaries (ints
    to &int%4==0 boundaries, floats to &float%8==0 boundaries), then the
    compiler would likely insert zero padding bytes between your int and
    your char, and seven padding bytes between your char and your float.
    Lew Pitcher, Jun 21, 2007
    #8
  9. Lew Pitcher Guest

    Oops... a correction

    On Jun 21, 11:09 am, Lew Pitcher <> wrote:
    [snip]
    > What we can tell you is that there may be padding characters inserted
    > between each element of the structure to maintain data item alignment.
    > If, *for instance*, ints were 4 bytes wide, and floats were 8 bytes
    > wide, and both were required to be aligned on "even" boundaries (ints
    > to &int%4==0 boundaries, floats to &float%8==0 boundaries), then the
    > compiler would likely insert zero padding bytes between your int and
    > your char, and seven padding bytes


    make that three padding bytes

    > between your char and your float.
    Lew Pitcher, Jun 21, 2007
    #9
  10. wrote:
    > On Jun 21, 7:49 pm, Clever Monkey <>
    > wrote:
    >> balu wrote:
    >>> On Jun 21, 7:33 pm, Richard Heathfield <> wrote:
    >>>> said:
    >>>>> how much memory is allocated for following structure
    >>>>> struct bharath
    >>>>> {
    >>>>> int b;
    >>>>> char c;
    >>>>> float d;
    >>>>> }
    >>>> None. It's a type, not an object.

    [...]
    *sigh*

    >>> let the int size is 4B then the allocation will be?
    >>> i want to know the info regarding padding or some other else related
    >>> to padding?

    >> This is exactly the sort of thing that people talk about when they say
    >> something is implementation specific. That is, once you actually
    >> allocate for a type, the specifics of how that memory is allocated is
    >> not necessarily dictated by the Standard.
    >>
    >> Simply defining (or is it declaring?) the type, of course, allocates no
    >> memory.

    [...]
    *double sigh*

    > what i want actually is how much size will be allocated for above
    > structuer in a 16 bit machine
    >

    Enough to hold the structure, plus any padding, on this specific 16-bit
    machine.

    Folks here are not being coy. There is no way to know, in general, how
    your compiler will allocate space, even if we know something about it or
    the platform is is generating code for. (Like it happens to be a
    "16-bit" machine, whatever that is. How are those bits represented?)

    If you have a specific implementation in mind, on a specific platform,
    then someone with the knowledge and access to the documentation could
    tell you with more detail.

    This very quality of C is why the compiler has been successfully
    targeted to a great many platforms, and why standard code is relatively
    easy to port.
    --
    clvrmnky <mailto:>

    Direct replies will be blacklisted. Replace "spamtrap" with my name to
    contact me directly.
    Clever Monkey, Jun 21, 2007
    #10
  11. CBFalconer Guest

    "" wrote:
    >
    > how much memory is allocated for following structure
    > struct bharath {
    > int b;
    > char c;
    > float d;
    > }
    > and how?


    Exactly "sizeof(struct bjarath);", in bytes. How is system
    dependant.

    --
    <http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
    <http://www.securityfocus.com/columnists/423>
    <http://www.aaxnet.com/editor/edit043.html>
    cbfalconer at maineline dot net



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Jun 21, 2007
    #11
  12. On Thu, 21 Jun 2007 14:56:39 -0000, in comp.lang.c ,
    "" <> wrote:

    >
    >what i want actually is how much size will be allocated for above
    >structuer in a 16 bit machine


    There is no correct answer. Depending on compiler settings, hardware
    requirements and so on, it could be anything from seven bytes upwards.
    Its impossible to say.

    Why do you need to know?
    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Mark McIntyre, Jun 21, 2007
    #12
  13. Default User Guest

    wrote:

    > how much memory is allocated for following structure
    > struct bharath
    > {
    > int b;
    > char c;
    > float d;
    > }


    This is, for some reason, a common newbie question. It doesn't need to
    be. When it comes to struct sizes, there are only two things to keep in
    mind:

    1. The actual size is implementation-specific. That means it varies
    from compiler to compiler, perhaps even with the same compiler
    depending on options. There's no way to know from code inspection.

    2. When you need to know the size in a program, the sizeof operator
    will tell you. The actual value is of little importance.




    Brian
    Default User, Jun 21, 2007
    #13
  14. wrote:
    > how much memory is allocated for following structure
    > struct bharath
    > {
    > int b;
    > char c;
    > float d;
    > }

    ^^ ';' needed

    None, until an instance of that structure is declared.
    If you have one
    struct bharath foo;
    the space allocated is
    sizeof foo
    or
    sizeof(struct bharath)
    and is at least
    sizeof(int)+sizeof(float)+1

    > and how?

    With whatever mechanism your compiler allocates space for other variables.
    Martin Ambuhl, Jun 21, 2007
    #14
  15. Army1987 Guest

    <> ha scritto nel messaggio news:...
    > how much memory is allocated for following structure
    > struct bharath
    > {
    > int b;
    > char c;
    > float d;
    > }


    None, because you are not declaring an object.

    If you wrote
    struct bharath {
    int b;
    char c;
    float d;
    } a;

    It would allocate sizeof a bytes, i.e. sizeof(struct bharath)
    bytes.

    > and how?


    The same way it is allocated for any other object.
    Army1987, Jun 21, 2007
    #15
  16. Army1987 Guest

    <> ha scritto nel messaggio news:...
    > On Jun 21, 7:49 pm, Clever Monkey <>
    > wrote:
    >> balu wrote:
    >> > On Jun 21, 7:33 pm, Richard Heathfield <> wrote:
    >> >> said:

    >>
    >> >>> how much memory is allocated for following structure
    >> >>> struct bharath
    >> >>> {
    >> >>> int b;
    >> >>> char c;
    >> >>> float d;
    >> >>> }
    >> >> None. It's a type, not an object.

    >>
    >> [Please use a newsreader that understands how to trim sigs, or trim them
    >> yourself. I've done this for you here.]
    >>
    >> > let the int size is 4B then the allocation will be?
    >> > i want to know the info regarding padding or some other else related
    >> > to padding?

    >>
    >> This is exactly the sort of thing that people talk about when they say
    >> something is implementation specific. That is, once you actually
    >> allocate for a type, the specifics of how that memory is allocated is
    >> not necessarily dictated by the Standard.
    >>
    >> Simply defining (or is it declaring?) the type, of course, allocates no
    >> memory.
    >> --
    >> clvrmnky <mailto:>
    >>
    >> Direct replies will be blacklisted. Replace "spamtrap" with my name to
    >> contact me directly.

    >
    > what i want actually is how much size will be allocated for above
    > structuer in a 16 bit machine
    >

    It doesn't even have to be the same on all 16-bit machines, or even
    on the same machine with different compilers.
    Army1987, Jun 21, 2007
    #16
  17. Jack Klein Guest

    On Thu, 21 Jun 2007 11:29:36 -0400, Clever Monkey
    <> wrote in comp.lang.c:

    > wrote:


    [snip]

    > > what i want actually is how much size will be allocated for above
    > > structuer in a 16 bit machine
    > >

    > Enough to hold the structure, plus any padding, on this specific 16-bit
    > machine.


    No, actually, just enough to hold the structure. If there is any
    padding in the structure, it is _IN_ the structure.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jun 21, 2007
    #17
  18. "" <> writes:
    > how much memory is allocated for following structure
    > struct bharath
    > {
    > int b;
    > char c;
    > float d;
    > }
    >
    > and how?


    The comp.lang.c FAQ is at <http://www.c-faq.com/>. Section 2 covers
    structures (as well as unions and enumerations). Read it. If you're
    still confused, come back and ask again.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jun 22, 2007
    #18
  19. Chris Dollin Guest

    wrote:

    > On Jun 21, 7:49 pm, Clever Monkey <>
    > wrote:
    >> balu wrote:
    >> > On Jun 21, 7:33 pm, Richard Heathfield <> wrote:
    >> >> said:

    >>
    >> >>> how much memory is allocated for following structure
    >> >>> struct bharath
    >> >>> {
    >> >>> int b;
    >> >>> char c;
    >> >>> float d;
    >> >>> }


    (fx:snipping)

    > what i want actually is how much size will be allocated for above
    > structuer in a 16 bit machine


    It depends on the implementation.

    Really.

    A "16-bit machine" (and what /that/ means isn't unambiguous) might have
    a C implementation which used 32-bit ints. Or 16-bit ints. It might have
    32-bit floats. Or 64. Floats might need to be allocated on 16-bit boundaries.
    Or 32-bit boundaries. Or 8-bit boundaries. chars might be 8 bits wide.
    Or 16. You might be able to /select/ these things with, say, command-line
    switches.

    It depends on the implementation.

    --
    Chris "just like life, really" Dollin

    Hewlett-Packard Limited registered no:
    registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
    Chris Dollin, Jun 22, 2007
    #19
  20. SM Ryan Guest

    "" <> wrote:
    # how much memory is allocated for following structure

    None. Structures only exist in the compiler; in the code
    you have blocks of allocated memory and offsets into them.

    A variable of type T needs no more than sizeof(T) char sized
    units allocated on appropriate memory boundary.

    # struct bharath
    # {
    # int b;
    # char c;
    # float d;
    # }

    A variable of type (struct bharath) will be allocated at
    least sizeof(struct bharath) bytes. How a compiler allocates
    the space is really up to the compiler. If you need to know
    there are field offset macros to find otu where each field
    begins in the variable's allocated space.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    I hope it feels so good to be right. There's nothing more
    exhilarating pointing out the shortcomings of others, is there?
    SM Ryan, Jun 24, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    675
    Eric Sosman
    Mar 22, 2005
  2. Alfonso Morra
    Replies:
    11
    Views:
    701
    Emmanuel Delahaye
    Sep 24, 2005
  3. Ken
    Replies:
    24
    Views:
    3,827
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    969
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,166
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page