Allocating memory for structs

Discussion in 'C Programming' started by Win Sock, Oct 6, 2007.

  1. Win Sock

    Win Sock 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 .
    Win Sock, Oct 6, 2007
    #1
    1. Advertising

  2. Win Sock

    santosh Guest

    Win Sock 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 .


    Yes, place each pair in a sub-structure and place both the
    sub-structures in a union.
    santosh, Oct 6, 2007
    #2
    1. Advertising

  3. "Win Sock" <> wrote in message
    news:...
    > 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 .
    >

    union. If you have either a len0 and pointer0, or len1 and pointer1, and you
    want to squeeze both into the same space, unions are designed for the task.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Oct 6, 2007
    #3
  4. Win Sock <> writes:
    > 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 .


    Given that declaration, no, not really.

    Others have suggested using a union, but what are you really trying to
    accomplish? Presumably allocating memory for only a subset of a
    structure is a means to an end, not an end in itself. If you tell us
    what your actual goal is, we might be able to help (or we might advise
    you that it's not worth doing).

    --
    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, Oct 6, 2007
    #4
  5. Win Sock

    Default User Guest

    Win Sock 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 .



    This doesn't make very much sense to me. What problem are you trying to
    solve?



    Brian
    Default User, Oct 6, 2007
    #5
  6. Win Sock

    CBFalconer Guest

    Win Sock 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 .


    You probably need a union. However, the types you are using in the
    struct seem rather silly. I would suggest replacing uit32 with
    size_t, and uint8 with unsigned long. Also don't forget to include
    a label for the struct, i.e.:

    struct name {
    size_t len0;
    unsigned long *pointer0;
    ...

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Oct 7, 2007
    #6
  7. CBFalconer <> writes:
    > Win Sock 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 .

    >
    > You probably need a union. However, the types you are using in the
    > struct seem rather silly. I would suggest replacing uit32 with
    > size_t, and uint8 with unsigned long.

    [...]

    I agree that size_t is probably a better type for the len0 and len1
    members, but how on Earth did you conclude that pointer0 and pointer1
    should be pointers to unsigned long rather than to uint8?

    (There's no standard type called uint8; perhaps the OP meant uint8_t?)

    --
    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, Oct 7, 2007
    #7
  8. Win Sock

    CBFalconer Guest

    Keith Thompson wrote:
    > CBFalconer <> writes:
    >> Win Sock 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 .

    >>
    >> You probably need a union. However, the types you are using in the
    >> struct seem rather silly. I would suggest replacing uit32 with
    >> size_t, and uint8 with unsigned long.

    > [...]
    >
    > I agree that size_t is probably a better type for the len0 and len1
    > members, but how on Earth did you conclude that pointer0 and pointer1
    > should be pointers to unsigned long rather than to uint8?


    Frankly, I have no idea. Thus proving I can write garbage with the
    best of them. At the moment pointer to unsigned char seems more
    reasonable.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>


    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Oct 7, 2007
    #8
  9. Win Sock

    Army1987 Guest

    On Sat, 06 Oct 2007 19:43:45 +0200, Win Sock 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 .

    Am I having a dejà-vu, or was this identical question already
    posted here a few weeks ago?

    What are you trying to do?
    I think that you could allocate space for len0 and pointer0 by
    doing malloc(offsetof(struct foo, len1)), but I don't have the
    courage to try it on my DS9K. And anyway, there is probably a
    *much* better way to do what you ultimately need.
    As for len1 and pointer1, in some cases
    unsigned char *realptr = malloc(sizeof struct unnamed
    - offsetof(struct unnamed, len1));
    struct unnamed *fakeptr = realptr - offsetof(struct unnamed, len1);
    could work, but there will be big problems if e.g. realptr happens
    to be at the beginning of a segment, or whatever, so *don't do
    that*.
    I repeat, what you are trying to do? It is *very* likely that you
    picked the wrong path towards that goal.

    --
    Army1987 (Replace "NOSPAM" with "email")
    A hamburger is better than nothing.
    Nothing is better than eternal happiness.
    Therefore, a hamburger is better than eternal happiness.
    Army1987, Oct 7, 2007
    #9
    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. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    635
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    556
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    639
    Eric Sosman
    Oct 11, 2005
  4. dev_15
    Replies:
    5
    Views:
    510
    Default User
    Nov 15, 2007
  5. Rakesh Kumar
    Replies:
    5
    Views:
    680
    James Kanze
    Dec 21, 2007
Loading...

Share This Page