pointer and struct allocation
hello!!
i my source code i use the following structs:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
typedef struct
{
uint8_t source;
uint8_t destination;
}IP4Hdr;
typedef struct
{
IP4Hdr *ip4h;
uint16_t src_port;
uint16_t dst_port;
int packets;
int is_here;
}Flows;
typedef struct
{
IP4Hdr *ip4h;
float num_flows; //for each ip
int is_here;
}IPs;
typedef struct
{
Flows *flows;
IPs *ips;
}Slots;
| |
at a point, i declare a pointer to these structs. Now, to allocate the
IPS* and
Flows* i do the following:
1 2 3 4 5
|
Flows* flows = calloc(MAXFLOWS, sizeof(*flows));
for(i = 0; i < MAXFLOWS; i++)
{
flows[i].ip4h = calloc(1, sizeof( *(flows[i].ip4h) ) );
}
| |
and same thing for
IPs*. But when i do something similar for the
Slots*:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
Slots* slots = calloc(MAXSLOTS, sizeof(*slots));
for (j=0;j<MAXSLOTS;j++){
slots[MAXSLOTS].flows=calloc(MAXFLOWS, sizeof(*(slots[MAXSLOTS].flows)));
for(i = 0; i < MAXFLOWS; i++)
{
8. slots[MAXSLOTS].flows[i].ip4h = calloc(1, sizeof( *(flows[i].ip4h) ) );
}
slots[MAXSLOTS].ips=calloc(MAXIPS, sizeof(*(slots[MAXSLOTS].ips)));
for(i = 0; i < MAXIPS; i++)
{
13. ips[i].ip4h = calloc(1, sizeof( *(ips[i].ip4h) ) );
}
}
| |
i get an error for 8 and 13 line for
undeclared 'flows' and undeclared 'ips'
what am i missing??
thanks!!
Last edited on
I think this is closer to what you want:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
Slots* slots = calloc(MAXSLOTS, sizeof(Slots));
for (j=0;j<MAXSLOTS;j++)
{
slots[j].flows=calloc(MAXFLOWS, sizeof(Flows));
for(i = 0; i < MAXFLOWS; i++)
{
slots[j].flows[i].ip4h = calloc(1, sizeof(IP4Hdr) );
}
slots[j].ips=calloc(MAXIPS, sizeof(IPs));
for(i = 0; i < MAXIPS; i++)
{
slots[j].ips[i].ip4h = calloc(1, sizeof(IP4Hdr) );
}
}
| |
Last edited on
you 're right!!!! thanks a lot Peter!
Topic archived. No new replies allowed.