memory allocation question

Discussion in 'C Programming' started by Trance, Apr 18, 2009.

  1. Trance

    Trance Guest

    I would like to set up a structure that looks like

    struct newStruct{

    char radius;
    char *coreShifts;

    }

    then have several such structures and access them using pointers. Now,
    how can I perform the memory allocation when the number of values in
    coreShifts is changing from structure at position 1 vs structure at
    position 2, because each structure will occupy different amounts of
    memory depending on the number of elements in coreShifts.

    Any help or pointing to reading resources would be greatly
    appreciated.
    Thank you.
     
    Trance, Apr 18, 2009
    #1
    1. Advertising

  2. Trance

    Andy Guest

    On Apr 17, 9:54 pm, Trance <> wrote:
    > I would like to set up a structure that looks like
    >
    > struct newStruct{
    >
    > char radius;
    > char *coreShifts;
    >
    > }
    >
    > then have several such structures and access them using pointers. Now,
    > how can I perform the memory allocation when the number of values in
    > coreShifts is changing from structure at position 1 vs structure at
    > position 2, because each structure will occupy different amounts of
    > memory depending on the number of elements in coreShifts.


    No. The structure size itself is fixed no matter how much memory you
    are going to allocate for char *coreShifts.

    struct newStruct var[2];

    var[0].coreShifts = malloc(size1);
    var[1].coreShifts = malloc(size2);


    >
    > Any help or pointing to reading resources would be greatly
    > appreciated.
    > Thank you.
     
    Andy, Apr 18, 2009
    #2
    1. Advertising

  3. Trance

    Trance Guest

    Andy,

    Thank you.

    So in this case the size of the structure is size of char + size of
    pointer data type.

    Now, is the reasoning below correct?

    struct newStruct *var = NULL;
    var = (struct newStruct *)calloc(2,sizeof(struct newStruct));

    This just allocates memory for the structure and has nothing to do
    with the coreShifts memory allocation. I'm wondering if calloc
    initializes the pointers to NULL in the member. For coreShifts, I will
    need to allocate memory for those too:

    var.coreShifts = (char *) calloc(var.radius,sizeof(char));
    (var+1).coreShifts = (char *) calloc((var+1).radius,sizeof(char));


    free(var.coreShifts);
    free((var+1).coreShifts);
    free(var);

    because when one frees up the memory one needs to free both the
    members of the structure and the structure itself.

    Thanks.
     
    Trance, Apr 18, 2009
    #3
  4. Trance

    Andy Guest

    On Apr 17, 10:12 pm, Trance <> wrote:
    > Andy,
    >
    > Thank you.
    >
    > So in this case the size of the structure is size of char + size of
    > pointer data type.


    Not exactly. Becz of alignment..

    sizeof (struct newStruct) >= sizeof(char) + sizeof(char *)...

    >
    > Now, is the reasoning below correct?
    >
    > struct newStruct *var = NULL;
    > var = (struct newStruct *)calloc(2,sizeof(struct newStruct));


    (struct newStruct *) is not required..

    var = calloc(2,sizeof *var); will be a better way to write it..

    >
    > This just allocates memory for the structure and has nothing to do
    > with the coreShifts memory allocation. I'm wondering if calloc


    Yes... exactly.

    > initializes the pointers to NULL in the member. For coreShifts, I will


    Yes. they do. That is difference it has from malloc()...

    > need to allocate memory for those too:
    >
    > var.coreShifts = (char *) calloc(var.radius,sizeof(char));


    I think it should be var[0].coreShifts = calloc(var[0].radius, sizeof
    *var[0].coreShifts);

    > (var+1).coreShifts = (char *) calloc((var+1).radius,sizeof(char));


    like the one right above...

    >
    > free(var.coreShifts);
    > free((var+1).coreShifts);


    The same problem as above...

    > free(var);
    >
    > because when one frees up the memory one needs to free both the
    > members of the structure and the structure itself.


    Yes. you hit the right point in your case..
    >
    > Thanks.
     
    Andy, Apr 18, 2009
    #4
  5. Trance <> writes:
    > Andy,
    >
    > Thank you.
    >
    > So in this case the size of the structure is size of char + size of
    > pointer data type.
    >
    > Now, is the reasoning below correct?
    >
    > struct newStruct *var = NULL;
    > var = (struct newStruct *)calloc(2,sizeof(struct newStruct));
    >
    > This just allocates memory for the structure and has nothing to do
    > with the coreShifts memory allocation. I'm wondering if calloc
    > initializes the pointers to NULL in the member.


    calloc sets the allocated memory to all-bits-zero. It's likely to set
    pointers to null, but it's not guaranteed, and it's a bad idea to
    depend on it.

    You might as well just use malloc:

    struct newStruct *var;
    var = malloc(2 * sizeof *var);

    > For coreShifts, I will
    > need to allocate memory for those too:
    >
    > var.coreShifts = (char *) calloc(var.radius,sizeof(char));
    > (var+1).coreShifts = (char *) calloc((var+1).radius,sizeof(char));


    var[0].coreShifts = malloc(var[0].radius);
    var[1].coreShifts = malloc(var[1].radius);

    [...]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Apr 18, 2009
    #5
  6. Trance

    Andy Guest

    On Apr 17, 11:31 pm, Han from China <>
    wrote:
    > Andy wrote:
    > >> I'm wondering if calloc initializes the pointers to NULL in the
    > >> member. For coreShifts, I will

    >
    > > Yes. they do. That is difference it has from malloc()...

    >
    > I'll try to rescue you from the mean sorts by getting in here first.
    > calloc() initializes the pointer objects to all-bits-zero, which
    > needn't be the null pointer representation.


    Thanks for your correction. Han. Yes. It was my mistake...

    >
    > Yours,
    > Han from China
    >
    > --
    > "Only entropy comes easy." -- Anton Chekhov
     
    Andy, Apr 18, 2009
    #6
  7. CBFalconer <> writes:
    > Trance wrote:
    >>
    >> I would like to set up a structure that looks like
    >>
    >> struct newStruct {
    >> char radius;
    >> char *coreShifts;
    >> }
    >>
    >> then have several such structures and access them using pointers.
    >> Now, how can I perform the memory allocation when the number of
    >> values in coreShifts is changing from structure at position 1 vs
    >> structure at position 2, because each structure will occupy
    >> different amounts of memory depending on the number of elements
    >> in coreShifts.

    >
    > No problem. coreShifts is an array of pointers,


    No, coreShifts is a pointer, not an array of anything.

    > and when you
    > allocate a newStruct you will either set coreShifts to NULL or will
    > allocate (and initialize, probably) the memory needed. The point
    > is that the contents pointed to by coreShifts should have it's own
    > internal marker to define its size. Possibly coreShifts points to
    > strings, which are terminated by a '\0' char.


    Possibly coreShifts (more precisely, the coreShifts member of a given
    struct newStruct object) points to *a* string, where pointing to a
    string is a shorthand for pointing to the first character of a string.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Apr 19, 2009
    #7
  8. Trance

    CBFalconer Guest

    Keith Thompson wrote:
    > CBFalconer <> writes:
    >> Trance wrote:
    >>>
    >>> I would like to set up a structure that looks like
    >>>
    >>> struct newStruct {
    >>> char radius;
    >>> char *coreShifts;
    >>> }
    >>>
    >>> then have several such structures and access them using pointers.
    >>> Now, how can I perform the memory allocation when the number of
    >>> values in coreShifts is changing from structure at position 1 vs
    >>> structure at position 2, because each structure will occupy
    >>> different amounts of memory depending on the number of elements
    >>> in coreShifts.

    >>
    >> No problem. coreShifts is an array of pointers,

    >
    > No, coreShifts is a pointer, not an array of anything.


    A single pointer is an array of one pointer. If the code handles
    it as an array it is quite flexible.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Apr 19, 2009
    #8
  9. Trance

    Ian Collins Guest

    CBFalconer wrote:
    > Keith Thompson wrote:
    >> CBFalconer <> writes:
    >>> Trance wrote:
    >>>> I would like to set up a structure that looks like
    >>>>
    >>>> struct newStruct {
    >>>> char radius;
    >>>> char *coreShifts;
    >>>> }
    >>>>
    >>>> then have several such structures and access them using pointers.
    >>>> Now, how can I perform the memory allocation when the number of
    >>>> values in coreShifts is changing from structure at position 1 vs
    >>>> structure at position 2, because each structure will occupy
    >>>> different amounts of memory depending on the number of elements
    >>>> in coreShifts.
    >>> No problem. coreShifts is an array of pointers,

    >> No, coreShifts is a pointer, not an array of anything.

    >
    > A single pointer is an array of one pointer. If the code handles
    > it as an array it is quite flexible.
    >

    Oh come on that's bollocks and you know it.

    Is "int n;" an array of one int? Is "char* n;" an array of one char*?

    --
    Ian Collins
     
    Ian Collins, Apr 19, 2009
    #9
  10. Trance

    Ian Collins Guest

    pete wrote:
    > Ian Collins wrote:
    >> CBFalconer wrote:

    >
    >>> A single pointer is an array of one pointer. If the code handles
    >>> it as an array it is quite flexible.
    >>>

    >> Oh come on that's bollocks and you know it.
    >>
    >> Is "int n;" an array of one int? Is "char* n;" an array of one char*?

    >
    > It depends on the context.
    > For the purposes of pointer math, the answer is "yes".
    >
    > N869
    > 6.5.6 Additive operators
    >
    > [#7] For the purposes of these operators, a pointer to a
    > nonarray object behaves the same as a pointer to the first
    > element of an array of length one with the type of the
    > object as its element type.


    The important limitation there is "For the purposes of these operators".

    --
    Ian Collins
     
    Ian Collins, Apr 19, 2009
    #10
  11. Trance

    Phil Carmody Guest

    Ian Collins <> writes:
    > pete wrote:
    >> Ian Collins wrote:
    >>> CBFalconer wrote:

    >>
    >>>> A single pointer is an array of one pointer. If the code handles
    >>>> it as an array it is quite flexible.
    >>>>
    >>> Oh come on that's bollocks and you know it.
    >>>
    >>> Is "int n;" an array of one int? Is "char* n;" an array of one char*?

    >>
    >> It depends on the context.
    >> For the purposes of pointer math, the answer is "yes".
    >>
    >> N869
    >> 6.5.6 Additive operators
    >>
    >> [#7] For the purposes of these operators, a pointer to a
    >> nonarray object behaves the same as a pointer to the first
    >> element of an array of length one with the type of the
    >> object as its element type.

    >
    > The important limitation there is "For the purposes of these operators".


    The more important limitation is "a pointer to".

    int n;
    /*...*/ n /* does not decay into a pointer to a first element */

    The answer to your question is clearly 'no'.

    Phil
    --
    Marijuana is indeed a dangerous drug.
    It causes governments to wage war against their own people.
    -- Dave Seaman (sci.math, 19 Mar 2009)
     
    Phil Carmody, Apr 19, 2009
    #11
  12. Phil Carmody <> writes:
    > Ian Collins <> writes:
    >> pete wrote:
    >>> Ian Collins wrote:
    >>>> CBFalconer wrote:
    >>>
    >>>>> A single pointer is an array of one pointer. If the code handles
    >>>>> it as an array it is quite flexible.
    >>>>>
    >>>> Oh come on that's bollocks and you know it.
    >>>>
    >>>> Is "int n;" an array of one int? Is "char* n;" an array of one char*?
    >>>
    >>> It depends on the context.
    >>> For the purposes of pointer math, the answer is "yes".
    >>>
    >>> N869
    >>> 6.5.6 Additive operators
    >>>
    >>> [#7] For the purposes of these operators, a pointer to a
    >>> nonarray object behaves the same as a pointer to the first
    >>> element of an array of length one with the type of the
    >>> object as its element type.

    >>
    >> The important limitation there is "For the purposes of these operators".

    >
    > The more important limitation is "a pointer to".
    >
    > int n;
    > /*...*/ n /* does not decay into a pointer to a first element */
    >
    > The answer to your question is clearly 'no'.


    int n;
    &n + 1; /* a valid pointer value */

    Nobody said that n decays into anything, but n *can* be treated as a
    single-element array.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Apr 19, 2009
    #12
  13. Trance

    Phil Carmody Guest

    Keith Thompson <> writes:
    > Phil Carmody <> writes:
    >> Ian Collins <> writes:
    >>> pete wrote:
    >>>> Ian Collins wrote:
    >>>>> CBFalconer wrote:
    >>>>
    >>>>>> A single pointer is an array of one pointer. If the code handles
    >>>>>> it as an array it is quite flexible.
    >>>>>>
    >>>>> Oh come on that's bollocks and you know it.
    >>>>>
    >>>>> Is "int n;" an array of one int? Is "char* n;" an array of one char*?
    >>>>
    >>>> It depends on the context.
    >>>> For the purposes of pointer math, the answer is "yes".
    >>>>
    >>>> N869
    >>>> 6.5.6 Additive operators
    >>>>
    >>>> [#7] For the purposes of these operators, a pointer to a
    >>>> nonarray object behaves the same as a pointer to the first
    >>>> element of an array of length one with the type of the
    >>>> object as its element type.
    >>>
    >>> The important limitation there is "For the purposes of these operators".

    >>
    >> The more important limitation is "a pointer to".
    >>
    >> int n;
    >> /*...*/ n /* does not decay into a pointer to a first element */
    >>
    >> The answer to your question is clearly 'no'.

    >
    > int n;
    > &n + 1; /* a valid pointer value */
    >
    > Nobody said that n decays into anything, but n *can* be treated as a
    > single-element array.


    Agreed. In some contexts, such as yours above.

    But to get the answer to Ian's question, I think one has to
    address what the meaning of "is" is.

    I take "is" to be a far stronger than just "can be treated as
    the same as in some contexts". "Is" to me means an equivalence
    and substitutability. Everything that applies to an array of
    one element would have to apply to int n, and that clearly
    isn't the case.

    And that rests the case for the 'no' faction.

    Phil
    --
    Marijuana is indeed a dangerous drug.
    It causes governments to wage war against their own people.
    -- Dave Seaman (sci.math, 19 Mar 2009)
     
    Phil Carmody, Apr 19, 2009
    #13
  14. Trance

    CBFalconer Guest

    Ian Collins wrote:
    > CBFalconer wrote:
    >

    .... snip ...
    >
    >> A single pointer is an array of one pointer. If the code handles
    >> it as an array it is quite flexible.

    >
    > Oh come on that's bollocks and you know it.
    >
    > Is "int n;" an array of one int? Is "char* n;" an array of one
    > char*?


    They may be. Try:

    #define N 10
    int *np = NULL; /* one pointer */
    int *ptrs[N] = {NULL}; /* array of N pointers */

    void foo(int *p) {
    /* whatever */
    }

    int main(void) {
    foo(np);
    foo(ptrs);
    return 0;
    }

    and tell me how foo can tell how many int* values are passed with
    p.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Apr 19, 2009
    #14
  15. Trance

    Phil Carmody Guest

    CBFalconer <> writes:
    > Ian Collins wrote:
    >> CBFalconer wrote:
    >>

    > ... snip ...
    >>
    >>> A single pointer is an array of one pointer. If the code handles
    >>> it as an array it is quite flexible.

    >>
    >> Oh come on that's bollocks and you know it.
    >>
    >> Is "int n;" an array of one int? Is "char* n;" an array of one
    >> char*?

    >
    > They may be. Try:
    >
    > #define N 10
    > int *np = NULL; /* one pointer */
    > int *ptrs[N] = {NULL}; /* array of N pointers */
    >
    > void foo(int *p) {
    > /* whatever */
    > }
    >
    > int main(void) {
    > foo(np);
    > foo(ptrs);
    > return 0;
    > }
    >
    > and tell me how foo can tell how many int* values are passed with
    > p.


    None will be passed until the code is actually made to
    compile.

    Phil
    --
    Marijuana is indeed a dangerous drug.
    It causes governments to wage war against their own people.
    -- Dave Seaman (sci.math, 19 Mar 2009)
     
    Phil Carmody, Apr 20, 2009
    #15
  16. Trance

    CBFalconer Guest

    Phil Carmody wrote:
    > CBFalconer <> writes:
    >> Ian Collins wrote:
    >>> CBFalconer wrote:
    >>>

    >> ... snip ...
    >>>
    >>>> A single pointer is an array of one pointer. If the code handles
    >>>> it as an array it is quite flexible.
    >>>
    >>> Oh come on that's bollocks and you know it.
    >>>
    >>> Is "int n;" an array of one int? Is "char* n;" an array of one
    >>> char*?

    >>
    >> They may be. Try:
    >>
    >> #define N 10
    >> int *np = NULL; /* one pointer */
    >> int *ptrs[N] = {NULL}; /* array of N pointers */
    >>
    >> void foo(int *p) {
    >> /* whatever */
    >> }
    >>
    >> int main(void) {
    >> foo(np);
    >> foo(ptrs);
    >> return 0;
    >> }
    >>
    >> and tell me how foo can tell how many int* values are passed with
    >> p.

    >
    > None will be passed until the code is actually made to compile.


    true. But you might get the idea.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Apr 20, 2009
    #16
    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:
    707
    Eric Sosman
    Mar 22, 2005
  2. Rodrigo Dominguez

    memory allocation and freeing memory

    Rodrigo Dominguez, Jun 13, 2005, in forum: C Programming
    Replies:
    11
    Views:
    607
    Jean-Claude Arbaut
    Jun 15, 2005
  3. Ken
    Replies:
    24
    Views:
    3,876
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    993
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,194
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page