structure calloc c

32,202

Solution 1

Should be calloc(4, sizeof(*port_data)): Note * before var name.

Solution 2

should be sizeof(port_data_t) not sizeof(port_data*). The former is the size of a port_data_t struct. The latter doesn't mean anything.

Solution 3

#include <malloc.h>
#include <pthread.h>

typedef struct port_data_t {
    size_t task_id;
    pthread_t *thread_id;
    size_t start_port;
    size_t number_ports;
} port_data_t;

port_data_t* f() {
    port_data_t* ports = (port_data_t*)calloc(4, sizeof(port_data_t));
    return ports;
}

Solution 4

Try changing this:

struct port_data_t *port_data = (struct task_data_t*) calloc(4, sizeof(port_data*));

To this:

port_data = (struct port_data_t*) calloc(4, sizeof(*port_data));

Might work a little better. If you've declaring port_data as a global struct, you don't need to re-declare it as a struct port_data_t. GCC should already know that. Of course, how I would do it is this:

port_data = (struct port_data_t*) calloc(4, sizeof(struct port_data_t));

But I don't like putting variables in sizeof(). I try to stick with putting types in there, just out of habit. Plus, it resolves any ambiguities about how exactly a pointer needs to be dereferenced, which is tripping you up in this case.

Share:
32,202
ant2009
Author by

ant2009

Updated on March 09, 2020

Comments

  • ant2009
    ant2009 about 4 years

    C99 gcc

    I keep getting this error. I have a struct outside main. And inside main I am trying to allocate on the stack using calloc. I can't seem to find out what is wrong.

    Thanks for any advice,

    error: expected expression before ‘)’ token

    /* global */
    struct port_data_t                                                                      
    {                                                                                       
        size_t task_id;                                                                     
        pthread_t *thread_id;                                                               
        size_t start_port;                                                                  
        size_t number_ports;                                                                
    } *port_data;                                                                           
    
    
    /* main function */
    struct port_data_t *port_data = (struct task_data_t*) calloc(4, sizeof(port_data*));
    
    • Mitch Wheat
      Mitch Wheat about 15 years
      any reason you are casting to a different type than the one you are assigning to?
    • Mitch Wheat
      Mitch Wheat about 15 years
      I think you might need to post the entire code (if not too long)
    • qrdl
      qrdl about 15 years
      calloc() allocates memory on heap rather then on stack so you have to manually free() it to prevent leaks. alloca() allocates on stack but it is not standard fucntion - neither C99 nor POSIX. However it is present in BSD and Linux.
  • AC88
    AC88 about 15 years
    I prefer using *<allocated ptr>, less breakage if the type changes.
  • visual_learner
    visual_learner about 15 years
    Should be sizeof(struct port_data_t), not sizeof(port_data_t) unless I'm badly mistaken.
  • visual_learner
    visual_learner about 15 years
    No, he means sizeof(struct port_data_t). I've checked this with GCC and it doesn't work without the struct keyword in there.
  • AC88
    AC88 about 15 years
    A pluser! Kill the heathen pluser! :)
  • AC88
    AC88 about 15 years
    Of course, using the variable is much safer when you are changing the pointer type...
  • Kyrol
    Kyrol over 7 years
    I think this is the best answer 'cause the one accepted could create misunderstanding between *<allocated ptr> and <allocated ptr>*