Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    C - trying to dynamically allocate an array of structs...

    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
    Code:
    #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
    Code:
    #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:
    Code:
    }
    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!
    Last edited by why_bird; 06-19-2008 at 02:15 PM. Reason: update

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    With the double star pointers, you need to malloc the **arr_of_ptrs then go through and allocate each of the 24 pointers, e.g.:

    Code:
    for(x = 0; x < num_struct; x++)
    
    arr_of_ptrs[x] = (struct el_to_ASIC*)malloc(sizeof(struct el_to_ASIC));
    Then you can allocate the structs after that (you will have to do another malloc). You could also make it so it did 24 again instead of 1 in my example, if you wanted a 24x24 two dimensional array. (My example gives you a 24x1 array) If you only need a 1D array, just use a single level of indirection.
    OracleGuy

  • #3
    New to the CF scene
    Join Date
    Jun 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks!

  • #4
    New to the CF scene
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Your code might be breaking the continuity of the memory. It would depend on the way malloc is implemented by the compiler, but it certainly does not guarantee a single block of memory of size (sizeof(num_struct*struct el_to_ASIC)).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •