Casting a generic or void pointer to point to a struct...

Discussion in 'C Programming' started by redefined.horizons@gmail.com, Aug 1, 2006.

  1. Guest

    First, I would thank all of those that took the time to answer my
    question about creating an array based on a numeric value stored in a
    variable.

    I realize after reading the responses and doing some more research,
    that what I really need is known in C as a "dynamic array". Basically,
    you surpass the array notation and use pointers with memory obtained
    with malloc() or calloc(). I think this will do just what I needed.

    That has brought up another question though. I'm not sure what syntax I
    would use to cast a gneeric or void pointer to a struct that I have
    defined. For example, if I have defined the "new_data_type" struct
    previously in the source code file, would the following code be valid?

    /* Create a generic pointer to the first element in the block of
    memory obtained with the calloc() function. */
    void generic_pointer = calloc(number_of_elements, size_of_element);

    /* If the memory has been sucessfully allocated, cast the generic
    pointer to the correct data type. */
    if(generic_pointer != NULL)
    (struct new_data_type *)generic_pointer
    else
    /* We've got problems. */

    Thanks again for the help everyone.

    Scott Huey
     
    , Aug 1, 2006
    #1
    1. Advertising

  2. MQ Guest

    wrote:
    > First, I would thank all of those that took the time to answer my
    > question about creating an array based on a numeric value stored in a
    > variable.
    >
    > I realize after reading the responses and doing some more research,
    > that what I really need is known in C as a "dynamic array". Basically,
    > you surpass the array notation and use pointers with memory obtained
    > with malloc() or calloc(). I think this will do just what I needed.
    >
    > That has brought up another question though. I'm not sure what syntax I
    > would use to cast a gneeric or void pointer to a struct that I have
    > defined. For example, if I have defined the "new_data_type" struct
    > previously in the source code file, would the following code be valid?
    >
    > /* Create a generic pointer to the first element in the block of
    > memory obtained with the calloc() function. */
    > void generic_pointer = calloc(number_of_elements, size_of_element);
    >
    > /* If the memory has been sucessfully allocated, cast the generic
    > pointer to the correct data type. */
    > if(generic_pointer != NULL)
    > (struct new_data_type *)generic_pointer
    > else
    > /* We've got problems. */
    >
    > Thanks again for the help everyone.
    >
    > Scott Huey


    struct new_data_type * ptr = calloc(number_of_elements,
    size_of_element);
    if(ptr == NULL)
    {
    /*error */
    }
    else
    {
    /*do something */
    }

    MQ
     
    MQ, Aug 1, 2006
    #2
    1. Advertising

  3. MQ Guest

    MQ wrote:
    > wrote:
    > > First, I would thank all of those that took the time to answer my
    > > question about creating an array based on a numeric value stored in a
    > > variable.
    > >
    > > I realize after reading the responses and doing some more research,
    > > that what I really need is known in C as a "dynamic array". Basically,
    > > you surpass the array notation and use pointers with memory obtained
    > > with malloc() or calloc(). I think this will do just what I needed.
    > >
    > > That has brought up another question though. I'm not sure what syntax I
    > > would use to cast a gneeric or void pointer to a struct that I have
    > > defined. For example, if I have defined the "new_data_type" struct
    > > previously in the source code file, would the following code be valid?
    > >
    > > /* Create a generic pointer to the first element in the block of
    > > memory obtained with the calloc() function. */
    > > void generic_pointer = calloc(number_of_elements, size_of_element);
    > >
    > > /* If the memory has been sucessfully allocated, cast the generic
    > > pointer to the correct data type. */
    > > if(generic_pointer != NULL)
    > > (struct new_data_type *)generic_pointer
    > > else
    > > /* We've got problems. */
    > >
    > > Thanks again for the help everyone.
    > >
    > > Scott Huey

    >
    > struct new_data_type * ptr = calloc(number_of_elements,
    > size_of_element);
    > if(ptr == NULL)
    > {
    > /*error */
    > }
    > else
    > {
    > /*do something */
    > }
    >
    > MQ



    correction: size_of_element should be replaced by sizeof(struct
    new_data_type)

    MQ
     
    MQ, Aug 1, 2006
    #3
  4. writes:
    > First, I would thank all of those that took the time to answer my
    > question about creating an array based on a numeric value stored in a
    > variable.
    >
    > I realize after reading the responses and doing some more research,
    > that what I really need is known in C as a "dynamic array". Basically,
    > you surpass the array notation and use pointers with memory obtained
    > with malloc() or calloc(). I think this will do just what I needed.
    >
    > That has brought up another question though. I'm not sure what syntax I
    > would use to cast a gneeric or void pointer to a struct that I have
    > defined. For example, if I have defined the "new_data_type" struct
    > previously in the source code file, would the following code be valid?
    >
    > /* Create a generic pointer to the first element in the block of
    > memory obtained with the calloc() function. */
    > void generic_pointer = calloc(number_of_elements, size_of_element);
    >
    > /* If the memory has been sucessfully allocated, cast the generic
    > pointer to the correct data type. */
    > if(generic_pointer != NULL)
    > (struct new_data_type *)generic_pointer
    > else
    > /* We've got problems. */


    Don't cast the result of malloc() or calloc(). It's unnecessary, and
    it can mask certain errors.

    BTW, it's "void *generic_pointer", not "void generic_pointer".

    Also, keep in mind that calloc() isn't necessarily as useful as you
    might think it is. It initializes the allocated memory to
    all-bits-zero. For pointers or floating-point data, this is not
    necessarily meaningful; there's no guarantee that either a null
    pointer or a floating-point 0.0 is represented as all-bits-zero.

    Usually it's easier to use malloc() (which doesn't initialize the
    allocated object), and just make sure that you don't use any portion
    of the object that you haven't assigned a value to.

    Why do you want a generic pointer rather than a pointer to your
    "new_data_type" struct? For example:

    struct new_data_type {
    /* member declarations here */
    };

    struct new_data_type *ptr;

    ptr = malloc(number_of_elements * sizeof *ptr);

    Sometimes you do need to use void* pointers, but quite often it's
    better to use a specific pointer type.

    --
    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.
     
    Keith Thompson, Aug 2, 2006
    #4
  5. Flash Gordon Guest

    MQ wrote:
    > MQ wrote:
    >> wrote:
    >>> First, I would thank all of those that took the time to answer my
    >>> question about creating an array based on a numeric value stored in a
    >>> variable.
    >>>
    >>> I realize after reading the responses and doing some more research,
    >>> that what I really need is known in C as a "dynamic array". Basically,
    >>> you surpass the array notation and use pointers with memory obtained
    >>> with malloc() or calloc(). I think this will do just what I needed.
    >>>
    >>> That has brought up another question though. I'm not sure what syntax I
    >>> would use to cast a gneeric or void pointer to a struct that I have
    >>> defined. For example, if I have defined the "new_data_type" struct
    >>> previously in the source code file, would the following code be valid?
    >>>
    >>> /* Create a generic pointer to the first element in the block of
    >>> memory obtained with the calloc() function. */
    >>> void generic_pointer = calloc(number_of_elements, size_of_element);
    >>>
    >>> /* If the memory has been sucessfully allocated, cast the generic
    >>> pointer to the correct data type. */
    >>> if(generic_pointer != NULL)
    >>> (struct new_data_type *)generic_pointer
    >>> else
    >>> /* We've got problems. */
    >>>
    >>> Thanks again for the help everyone.
    >>>
    >>> Scott Huey

    >> struct new_data_type * ptr = calloc(number_of_elements,
    >> size_of_element);
    >> if(ptr == NULL)
    >> {
    >> /*error */
    >> }
    >> else
    >> {
    >> /*do something */
    >> }

    >
    > correction: size_of_element should be replaced by sizeof(struct
    > new_data_type)



    In all probability neither is optimal. Firstly, the OP did not say that
    it needed clearing to all bits 0 (remember that floating point 0 and
    null pointers might not be all bits 0) so why pay the cost of zeroing
    the memory? Secondly, there are better ways to use sizeof.

    If you really do want calloc:

    T *ptr = calloc(number_of_elements, sizeof *ptr);
    Then you only have to specify the type in one place, so maintenance is
    easier.

    Or, for malloc
    T *ptr = malloc(number_of_elements * sizeof *ptr);

    Or, if mallocing some time after declaration:
    ptr = malloc(number_of_elements * sizeof *ptr);

    Although the OP should also look up the references to the struct hack
    others have posted and, if using a C99 compiler, the C99 sanctioned
    alternative.
    --
    Flash Gordon
    Still sigless on this computer.
     
    Flash Gordon, Aug 2, 2006
    #5
  6. On Wed, 02 Aug 2006 02:00:42 +0200, Flash Gordon wrote:
    <snip>
    > If you really do want calloc:
    >
    > T *ptr = calloc(number_of_elements, sizeof *ptr); Then you only have to
    > specify the type in one place, so maintenance is easier.
    >
    > Or, for malloc
    > T *ptr = malloc(number_of_elements * sizeof *ptr);
    >


    calloc() might have the benefit of overflow detection.
     
    William Ahern, Aug 2, 2006
    #6
  7. MQ Guest

    Flash Gordon wrote:

    >
    > In all probability neither is optimal. Firstly, the OP did not say that
    > it needed clearing to all bits 0 (remember that floating point 0 and
    > null pointers might not be all bits 0) so why pay the cost of zeroing
    > the memory?


    I don't understand what you are talking about

    MQ
     
    MQ, Aug 2, 2006
    #7
  8. MQ Guest

    Flash Gordon wrote:

    >
    > In all probability neither is optimal. Firstly, the OP did not say that
    > it needed clearing to all bits 0 (remember that floating point 0 and
    > null pointers might not be all bits 0) so why pay the cost of zeroing
    > the memory?


    Ah, OK just realized, calloc zeroes memory, malloc does not. I used
    calloc because thats what the OP is using, so you have to assume they
    are using it for a reason. Why would you use it otherwise?

    > Secondly, there are better ways to use sizeof.
    > sizeof *ptr


    I dont understand how this is better though. Please explain.

    MQ
     
    MQ, Aug 2, 2006
    #8
  9. "MQ" <> writes:
    > Flash Gordon wrote:
    >> In all probability neither is optimal. Firstly, the OP did not say that
    >> it needed clearing to all bits 0 (remember that floating point 0 and
    >> null pointers might not be all bits 0) so why pay the cost of zeroing
    >> the memory?

    >
    > I don't understand what you are talking about


    I don't understand what the problem is.

    The main difference between malloc() and calloc() is that calloc()
    initializes the allocated object to all-bits-zero. There's no
    guarantee that this is useful; all-bits-zero may not be a
    representation of a meaningful value for all types. In particular,
    there's no guarantee that either a null pointer value or a
    floating-point 0.0 is represented as all-bits-zero.

    Because of this, zeroing allocated memory is often a waste of time. A
    better solution is often to allocate using malloc(), and then be
    careful not to read any portion of the allocated object that you
    haven't explicitly assigned a value to.

    --
    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.
     
    Keith Thompson, Aug 2, 2006
    #9
  10. Flash Gordon Guest

    MQ wrote:
    > Flash Gordon wrote:
    >
    >> In all probability neither is optimal. Firstly, the OP did not say that
    >> it needed clearing to all bits 0 (remember that floating point 0 and
    >> null pointers might not be all bits 0) so why pay the cost of zeroing
    >> the memory?

    >
    > Ah, OK just realized, calloc zeroes memory, malloc does not. I used
    > calloc because thats what the OP is using, so you have to assume they
    > are using it for a reason. Why would you use it otherwise?


    Because the OP does not know how to program in C yet.

    >> Secondly, there are better ways to use sizeof.
    >> sizeof *ptr

    >
    > I dont understand how this is better though. Please explain.


    What changes to you have to make to
    ptr = malloc(N * sizeof *ptr);
    if the type of ptr is changed? I'll give you a clue, the answer is none.

    Do you have to find the declaration of ptr to see if the correct amount
    of memory is being allocated?

    Seach the groups archives for a bit for further discussion.
    --
    Flash Gordon
    Still sigless on this computer
     
    Flash Gordon, Aug 2, 2006
    #10
  11. Guest

    I want to thank all the participants on this thread for their
    discussion and responses to the original post.

    I realize after reading the responses that I do want to use malloc()
    and not calloc().

    I am trying to create a "standard" dynamic array structure that can be
    used dynamically with a variety of data types. I want programmers to be
    able to use this dynamic array through its public functions, without
    the need to modify its source code. That is why I was avoiding a call
    to sizeof() and using void pointers in my code.

    I'm still not sure if what I want to do is possible in C, but your
    comments have helped me along in the right direction. I'm sure I'll get
    things figured out after I have some more time to spend with the code.

    Scott Huey

    Flash Gordon wrote:
    > MQ wrote:
    > > Flash Gordon wrote:
    > >
    > >> In all probability neither is optimal. Firstly, the OP did not say that
    > >> it needed clearing to all bits 0 (remember that floating point 0 and
    > >> null pointers might not be all bits 0) so why pay the cost of zeroing
    > >> the memory?

    > >
    > > Ah, OK just realized, calloc zeroes memory, malloc does not. I used
    > > calloc because thats what the OP is using, so you have to assume they
    > > are using it for a reason. Why would you use it otherwise?

    >
    > Because the OP does not know how to program in C yet.
    >
    > >> Secondly, there are better ways to use sizeof.
    > >> sizeof *ptr

    > >
    > > I dont understand how this is better though. Please explain.

    >
    > What changes to you have to make to
    > ptr = malloc(N * sizeof *ptr);
    > if the type of ptr is changed? I'll give you a clue, the answer is none.
    >
    > Do you have to find the declaration of ptr to see if the correct amount
    > of memory is being allocated?
    >
    > Seach the groups archives for a bit for further discussion.
    > --
    > Flash Gordon
    > Still sigless on this computer
     
    , Aug 2, 2006
    #11
  12. MQ Guest

    wrote:
    > I want to thank all the participants on this thread for their
    > discussion and responses to the original post.
    >
    > I realize after reading the responses that I do want to use malloc()
    > and not calloc().
    >
    > I am trying to create a "standard" dynamic array structure that can be
    > used dynamically with a variety of data types. I want programmers to be
    > able to use this dynamic array through its public functions, without
    > the need to modify its source code. That is why I was avoiding a call
    > to sizeof() and using void pointers in my code.
    >
    > I'm still not sure if what I want to do is possible in C, but your
    > comments have helped me along in the right direction. I'm sure I'll get
    > things figured out after I have some more time to spend with the code.


    What about

    struct array
    {
    void * start;
    int length;
    int elem_size;
    };

    create_array(array * a);

    struct array my_array;
    my_array.length = <some length>;
    my_array.elem_size = <some size>;
    create_array(&a);

    create_array function would just be a wrapper to malloc. What other
    functions do you need, other than ones to access elements, and possibly
    resize the array?

    If you need a data structure where you can insert and delete items, you
    should possibly consider using a linked list. Resizing a dynamic array
    using realloc() is a bad idea; memory managers may have to shift the
    entire array in memory if extra space cannot be reallocated at the end
    of memory. Linked lists dont have this problem.

    MQ
     
    MQ, Aug 3, 2006
    #12
    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. Ollej Reemt
    Replies:
    7
    Views:
    605
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    840
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    886
    S.Tobias
    Jul 22, 2005
  4. Replies:
    1
    Views:
    439
    Victor Bazarov
    May 23, 2007
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,427
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page