user907810
user907810

Reputation: 3368

Using structs with index to create vectors

I came across a similar struct in a piece of code. while trying to use these I somehow got stuck. Here is what I wrote based on the structs I came across.

#include<stdio.h>
#include<stdint.h>
#include <string.h>
#include <stdlib.h>

typedef struct MyStruct{
    unsigned char *ptr;
    int len;
}MyStruct;
typedef MyStruct *Vec_MyStruct;

struct MyGroup {
    int n_MyStruct;
    Vec_MyStruct myStructs;
};
typedef struct MyGroup MyGroup;
typedef struct MyGroup *Vec_MyGroup;

typedef struct MyBigGroup {
    int n_MyBigGroups;
    Vec_MyGroup myBigGroups;
}MyBigGroup;


int ArrayData();

int main(int argc, char *argv[]){

    printf("Started..\n");
    ArrayData();
    return 0;
}

int ArrayData()
{
    printf("ArrayData called..\n");
    MyStruct bb1;
    memset(&bb1,0,sizeof(bb1));
    bb1.ptr = (unsigned char *)malloc(6);
    bb1.len = 6;
    char *ch = "Hello";
    memcpy(bb1.ptr, ch, bb1.len);
    //      printf("1: ptr  %s\n", bb1.ptr);
    //      printf("1: len %d\n", bb1.len);

    MyStruct bb2;
    memset(&bb2,0,sizeof(bb2));
    bb2.ptr = (unsigned char *)malloc(6);
    bb2.len = 6;
    char *c = "World";
    memcpy(bb2.ptr, c, bb2.len);
    //      printf("2: ptr  %s\n", bb2.ptr);
    //      printf("2: len %d\n", bb2.len);


    Vec_MyStruct vec = malloc(sizeof(vec));

    vec[0] = bb1;
    vec[1] = bb2;
    printf("vec[0]: ptr  %s\n", vec[0].ptr);
    printf("vec[0]: len %d\n", vec[0].len);
    printf("vec[1]: ptr  %s\n", vec[1].ptr);
    printf("vec[1]: len %d\n", vec[1].len);
    //

    MyGroup perm;
    perm.n_MyStruct = 2;// 2 My_Struct vectors
    perm.myStructs = vec;
    printf("perm.myStructs[0].ptr: ptr  %s\n", perm.myStructs[0].ptr);
    printf("perm.myStructs[0].len: len %d\n", perm.myStructs[0].len);
    printf("perm.myStructs[1].ptr: ptr  %s\n", perm.myStructs[1].ptr);
    printf("perm.myStructs[1].len: len %d\n", perm.myStructs[1].len);

    MyBigGroup grp;
    grp.n_MyBigGroups = 1;
    grp.myBigGroups = &perm;


    printf("grp.myBigGroups[0].myStructs[0].ptr: ptr  %s\n", grp.myBigGroups[0].myStructs[0].ptr);
    printf("grp.myBigGroups[0].myStructs[0].len: len %d\n", grp.myBigGroups[0].myStructs[0].len);
    printf("grp.myBigGroups[0].myStructs[1].ptr: ptr  %s\n", grp.myBigGroups[0].myStructs[1].ptr);
    printf("grp.myBigGroups[0].myStructs[1].len: len %d\n", grp.myBigGroups[0].myStructs[1].len);
    return 0;
}

So MyGroup has a "list" of MyStruct items. It also has a variable that tells me how many MyStruct items exist. Vec_MyStuct just points to the MyStruct. There is another grouping of the MyGroup which is the MyBigGroup. But what worries me here is although the code is working just fine, I am unsure if I am allocating memory correctly.Especially the MyBigGroup.. Or is there a more efficient way to use these structs to define multiple entries of MyStructs?

Thanks.

Upvotes: 0

Views: 45

Answers (1)

interjay
interjay

Reputation: 110138

Vec_MyStruct vec = malloc(sizeof(vec));

vec[0] = bb1;
vec[1] = bb2;

This is wrong. You are allocating the size of a single pointer, then writing two structs into the memory. It should be malloc(2 * sizeof(*vec)).

Note that typedefing a pointer is often considered bad style, especially when the typedef has a confusing name such as Vec_MyStruct.

As far as I can see, the other parts of your code are correct, but some are somewhat error-prone. For example, the code that allocates the strings hard-codes the sizes. I'd make a function for filling the fields of a MyStruct which receives the string as a parameter, then uses strlen to determine its size.

Additionally, in MyBigGroup, you used a pointer to a local variable grp.myBigGroups = &perm;. This will work in this small example, but could lead to issues in larger code - the pointer won't be valid after the function exits, and if you try freeing the memory it will cause problems.

Upvotes: 3

Related Questions