Structure memory allocation

Discussion in 'C Programming' started by uday, Sep 10, 2007.

  1. uday

    uday Guest

    Hi All,
    I need one clarification regarding memory allocation for
    structure. The details are as given below :

    let us consider one structure

    struct {
    uit32 len0;
    uint8 *pointer0;
    uit32 len1;
    uint8 *pointer1;
    );

    When i want to allocate memory using malloc() for this structure is
    there any way i can allocate memory for only for len0 and pointer0 or
    len1 and pointer1 .

    Regards,
    udaykumar
    uday, Sep 10, 2007
    #1
    1. Advertising

  2. uday

    Ian Collins Guest

    uday wrote:
    > Hi All,
    > I need one clarification regarding memory allocation for
    > structure. The details are as given below :
    >
    > let us consider one structure
    >
    > struct {
    > uit32 len0;
    > uint8 *pointer0;
    > uit32 len1;
    > uint8 *pointer1;
    > );
    >
    > When i want to allocate memory using malloc() for this structure is
    > there any way i can allocate memory for only for len0 and pointer0 or
    > len1 and pointer1 .
    >

    No, why would you want to?

    --
    Ian Collins.
    Ian Collins, Sep 10, 2007
    #2
    1. Advertising

  3. uday

    uday Guest

    On Sep 10, 11:31 am, Ian Collins <> wrote:
    > uday wrote:
    > > Hi All,
    > > I need one clarification regarding memory allocation for
    > > structure. The details are as given below :

    >
    > > let us consider one structure

    >
    > > struct {
    > > uit32 len0;
    > > uint8 *pointer0;
    > > uit32 len1;
    > > uint8 *pointer1;
    > > );

    >
    > > When i want to allocate memory using malloc() for this structure is
    > > there any way i can allocate memory for only for len0 and pointer0 or
    > > len1 and pointer1 .

    >
    > No, why would you want to?
    >
    > --
    > Ian Collins.


    I need to combine 2 function which uses the same structure .... i.e
    for Ipv6 and Ipv6oEth i want to use the same struct .... so i will abe
    able to combine the functionality
    uday, Sep 10, 2007
    #3
  4. uday

    Guest

    On Sep 10, 10:49 am, uday <> wrote:
    > Hi All,
    > I need one clarification regarding memory allocation for
    > structure. The details are as given below :
    >
    > let us consider one structure
    >
    > struct {
    > uit32 len0;
    > uint8 *pointer0;
    > uit32 len1;
    > uint8 *pointer1;
    > );
    >
    > When i want to allocate memory using malloc() for this structure is
    > there any way i can allocate memory for only for len0 and pointer0 or
    > len1 and pointer1 .



    I assume you do not intend to use {len0, pointer0} and {len1,
    pointer1}
    simultaneously. In that case, an union may be what you want.

    -p_c_g.
    , Sep 10, 2007
    #4
  5. uday

    Army1987 Guest

    On Sun, 09 Sep 2007 22:49:45 -0700, uday wrote:

    > Hi All,
    > I need one clarification regarding memory allocation for
    > structure. The details are as given below :
    >
    > let us consider one structure
    >
    > struct {
    > uit32 len0;
    > uint8 *pointer0;
    > uit32 len1;
    > uint8 *pointer1;
    > );
    >
    > When i want to allocate memory using malloc() for this structure is
    > there any way i can allocate memory for only for len0 and pointer0 or
    > len1 and pointer1 .


    The latter is impossible. For the former you could try
    malloc(offsetof(struct unnamed, len1)), I think it has undefined
    behavior if you do enough language lawyering on the Standard, but
    it is very likely to work.
    --
    Army1987 (Replace "NOSPAM" with "email")
    If you're sending e-mail from a Windows machine, turn off Microsoft's
    stupid “Smart Quotes†feature. This is so you'll avoid sprinkling garbage
    characters through your mail. -- Eric S. Raymond and Rick Moen
    Army1987, Sep 10, 2007
    #5
  6. uday

    Army1987 Guest

    On Sun, 09 Sep 2007 23:57:33 -0700, uday wrote:

    > On Sep 10, 11:31 am, Ian Collins <> wrote:
    >> uday wrote:

    [snip]
    >> > let us consider one structure

    >>
    >> > struct {
    >> > uit32 len0;
    >> > uint8 *pointer0;
    >> > uit32 len1;
    >> > uint8 *pointer1;
    >> > );

    >>
    >> > When i want to allocate memory using malloc() for this structure is
    >> > there any way i can allocate memory for only for len0 and pointer0 or
    >> > len1 and pointer1 .


    >> No, why would you want to?


    > I need to combine 2 function which uses the same structure .... i.e
    > for Ipv6 and Ipv6oEth i want to use the same struct .... so i will abe
    > able to combine the functionality


    A union of two structs, maybe?
    --
    Army1987 (Replace "NOSPAM" with "email")
    If you're sending e-mail from a Windows machine, turn off Microsoft's
    stupid “Smart Quotes†feature. This is so you'll avoid sprinkling garbage
    characters through your mail. -- Eric S. Raymond and Rick Moen
    Army1987, Sep 10, 2007
    #6
  7. uday <> writes:
    > On Sep 10, 11:31 am, Ian Collins <> wrote:
    >> uday wrote:
    >> > I need one clarification regarding memory allocation for
    >> > structure. The details are as given below :

    >>
    >> > let us consider one structure

    >>
    >> > struct {
    >> > uit32 len0;
    >> > uint8 *pointer0;
    >> > uit32 len1;
    >> > uint8 *pointer1;
    >> > );

    >>
    >> > When i want to allocate memory using malloc() for this structure is
    >> > there any way i can allocate memory for only for len0 and pointer0 or
    >> > len1 and pointer1 .

    >>
    >> No, why would you want to?

    >
    > I need to combine 2 function which uses the same structure .... i.e
    > for Ipv6 and Ipv6oEth i want to use the same struct .... so i will abe
    > able to combine the functionality


    I'm not entirely clear on what you're trying to do, but it would
    probably make sense to use nested structures. For example:

    struct s0 {
    uint32 len0;
    uint8 *pointer0;
    };
    struct s1 {
    uint32 len1;
    uint8 *pointer1;
    };
    struct both {
    struct s0 member0;
    struct s1 member1;
    };

    You can then allocate the entire outer structure or just one of the
    inner structures.

    --
    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, Sep 10, 2007
    #7
  8. uday

    pete Guest

    uday wrote:
    >
    > Hi All,
    > I need one clarification regarding memory allocation for
    > structure. The details are as given below :
    >
    > let us consider one structure
    >
    > struct {
    > uit32 len0;
    > uint8 *pointer0;
    > uit32 len1;
    > uint8 *pointer1;
    > );
    >
    > When i want to allocate memory using malloc() for this structure is
    > there any way i can allocate memory for only for len0 and pointer0 or
    > len1 and pointer1 .


    struct {
    uit32 len;
    uint8 *pointer;
    );

    --
    pete
    pete, Sep 10, 2007
    #8
  9. uday

    Tor Rustad Guest

    uday wrote:
    >
    > I need to combine 2 function which uses the same structure .... i.e
    > for Ipv6 and Ipv6oEth i want to use the same struct .... so i will abe
    > able to combine the functionality
    >


    Hmm.. what with:

    struct ip
    {
    uint32 len;
    uint8 *ptr;
    };

    union ip_v6
    {
    struct ip v6;
    struct ip v6Eth;
    };

    ?

    --
    Tor <torust [at] online [dot] no>
    Tor Rustad, Sep 10, 2007
    #9
  10. uday

    uday Guest

    On Sep 10, 2:35 pm, Keith Thompson <> wrote:
    > uday <> writes:
    > > On Sep 10, 11:31 am, Ian Collins <> wrote:
    > >> uday wrote:
    > >> > I need one clarification regarding memory allocation for
    > >> > structure. The details are as given below :

    >
    > >> > let us consider one structure

    >
    > >> > struct {
    > >> > uit32 len0;
    > >> > uint8 *pointer0;
    > >> > uit32 len1;
    > >> > uint8 *pointer1;
    > >> > );

    >
    > >> > When i want to allocate memory using malloc() for this structure is
    > >> > there any way i can allocate memory for only for len0 and pointer0 or
    > >> > len1 and pointer1 .

    >
    > >> No, why would you want to?

    >
    > > I need to combine 2 function which uses the same structure .... i.e
    > > for Ipv6 and Ipv6oEth i want to use the same struct .... so i will abe
    > > able to combine the functionality

    >
    > I'm not entirely clear on what you're trying to do, but it would
    > probably make sense to use nested structures. For example:
    >
    > struct s0 {
    > uint32 len0;
    > uint8 *pointer0;
    > };
    > struct s1 {
    > uint32 len1;
    > uint8 *pointer1;
    > };
    > struct both {
    > struct s0 member0;
    > struct s1 member1;
    > };
    >
    > You can then allocate the entire outer structure or just one of the
    > inner structures.
    >
    > --
    > 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"


    hi thanks for the possible solution ... In my case i think the use of
    union dont serve my purpose but defining 2 struct within one struct
    will serve my purpose ...
    Regards,
    udaykumar
    uday, Sep 11, 2007
    #10
    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. Replies:
    2
    Views:
    3,083
    Akshay
    Feb 16, 2005
  2. jvax

    memory allocation for structure array

    jvax, Jul 4, 2003, in forum: C Programming
    Replies:
    4
    Views:
    14,286
    siva2152
    Nov 24, 2010
  3. Ken
    Replies:
    24
    Views:
    3,863
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    988
    chris
    Oct 28, 2005
  5. Replies:
    2
    Views:
    603
Loading...

Share This Page