why_bird
06-19-2008, 02:14 PM
Afternoon all,
I'm fairly new to programming and new here, so please be gentle..! I'm trying to dynamically allocate an array of structs (actually an array of pointers to structs) and I'm having a world of trouble.
This is my attempt so far:
file: arr_struct.c
#include "arr_struct.h"
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int num_struct=24;
int i;
el_to_ASIC * block;
el_to_ASIC ** arr_of_ptrs;
block=malloc(num_struct * sizeof(el_to_ASIC));
arr_of_ptrs=malloc(num_struct * sizeof(el_to_ASIC *));
for(i=0;i<num_struct;i++){
arr_of_ptrs[i]=block+(i * sizeof(el_to_ASIC));
arr_of_ptrs[i]->Mod=i; //insert new block here (see comment below)
arr_of_ptrs[i]->ASIC=2;
arr_of_ptrs[i]->output_num=i+1;
arr_of_ptrs[i]->e_subband=0;
}
for(i=0;i<num_struct;i++)
arr_of_ptrs[i]->Mod+=i*4;
return (0);
}
file: arr_struct.h
#ifndef ARR
#define ARR 1
// Typedefs
typedef struct _el_to_ASIC
{
unsigned int Mod;
unsigned int ASIC;
unsigned int output_num;
unsigned int e_subband;
} el_to_ASIC;
#endif
It does seem to work, but when I run it through valgrind I get lots of invalid write errors. this is part of a larger project, but this code is an 'in vitro' example of the problem I'm having.
I also tried splitting up the memory allocation and assigning to the variables into two seperate blocks (insert:
}
for(i=0;i<num_struct;i++){
where I indicate in my comment in the code.) Then I get a segfault caused by an invalid write, and I can't work out why..
I'm sure I must be misunderstanding something---I've read a lot about malloc etc. (oh, and I know I'm not free-ing, that's just to save space in this snippet, I do in the larger program) but can't seem to see where i'm going wrong.
Thanks!
why_bird
update: p.s. I can't use C++. Not my choice, so pls don't tell me to!
I'm fairly new to programming and new here, so please be gentle..! I'm trying to dynamically allocate an array of structs (actually an array of pointers to structs) and I'm having a world of trouble.
This is my attempt so far:
file: arr_struct.c
#include "arr_struct.h"
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int num_struct=24;
int i;
el_to_ASIC * block;
el_to_ASIC ** arr_of_ptrs;
block=malloc(num_struct * sizeof(el_to_ASIC));
arr_of_ptrs=malloc(num_struct * sizeof(el_to_ASIC *));
for(i=0;i<num_struct;i++){
arr_of_ptrs[i]=block+(i * sizeof(el_to_ASIC));
arr_of_ptrs[i]->Mod=i; //insert new block here (see comment below)
arr_of_ptrs[i]->ASIC=2;
arr_of_ptrs[i]->output_num=i+1;
arr_of_ptrs[i]->e_subband=0;
}
for(i=0;i<num_struct;i++)
arr_of_ptrs[i]->Mod+=i*4;
return (0);
}
file: arr_struct.h
#ifndef ARR
#define ARR 1
// Typedefs
typedef struct _el_to_ASIC
{
unsigned int Mod;
unsigned int ASIC;
unsigned int output_num;
unsigned int e_subband;
} el_to_ASIC;
#endif
It does seem to work, but when I run it through valgrind I get lots of invalid write errors. this is part of a larger project, but this code is an 'in vitro' example of the problem I'm having.
I also tried splitting up the memory allocation and assigning to the variables into two seperate blocks (insert:
}
for(i=0;i<num_struct;i++){
where I indicate in my comment in the code.) Then I get a segfault caused by an invalid write, and I can't work out why..
I'm sure I must be misunderstanding something---I've read a lot about malloc etc. (oh, and I know I'm not free-ing, that's just to save space in this snippet, I do in the larger program) but can't seem to see where i'm going wrong.
Thanks!
why_bird
update: p.s. I can't use C++. Not my choice, so pls don't tell me to!