Search code examples
cdynamic-memory-allocation

execute bad access and nested structure


I have any error with execute bad access for this program.

  1. Firstly, I have 3 structure.
    struct format {
        int initialCyclicShift;
        int nrOfSymbols;
        int startingSymbolIndex;
        int formatID;
    };
    
    struct  PUCCH_Resource {
        //38.331 maxNrofPUCCH-Resources = 128
        int pucch_ResourceId;
        int startingPRB;
        int intraSLotFrequencyHopping;
        int secondHopPRB;
        // 5 formats in PUCCH, 38.211. Also in 38.331
        struct format (*formatList)[5];    //(*formatList) is a pointer that points to the whole array of size 5. The element of the array is a struct format
    };
    
    struct PUCCH_ResourceSet {
        // 38.331 maxNrOfPUCCH-ResourceSet=4
        int pucch_ResoureSetId;
        // 38.331 maxNrofPUCCH-ResourcePerSet = 32; for Initial Access: maxNrofPUCCH-ResourcePerSet = 16. There could be repetition of pucch resource in different sets.
        struct PUCCH_Resource (*ResourceList)[32];
    };
  1. Then I initialize the pointer, pointing to the parents structures PUCCH_ResourceSet , and children structures.
    struct PUCCH_ResourceSet *pucch_ResourceSetPtr = (struct PUCCH_ResourceSet*) malloc (sizeof(struct PUCCH_ResourceSet));

    pucch_ResourceSetPtr->ResourceList = malloc(sizeof(struct PUCCH_Resource));
    
    for (int i = 0; i <32; i++){
        for (int j = 0; j <5; j++)
            pucch_ResourceSetPtr->ResourceList[i]->formatList[j]= malloc(sizeof(struct format));
    }

(edit) I tried to initialize all the structs as suggested by member below, but still have the same issue.

  1. and then try a get function that assign value to each member of the struct, for example:
    pucch_ResourceSetPtr->ResourceList[0]->pucch_ResourceId=0;                       
    pucch_ResourceSetPtr->ResourceList[0]->startingPRB = 0;
    pucch_ResourceSetPtr->ResourceList[0]->formatList[0]->formatID=0;  //this is where I have the error

The line of code above is the first to assign a value to a member of struct; this is where I have the error.

Upon the bugging, I see that when the PUCCH_ResourceSet, it see a memory block is assigned to the struct, its child struct, PUCCH_Resource, but not its grandchildren struct, format as show in the screenshot below.

I suspect it is the issue, but dont understand why no memory block is assigned to the format struct.

enter image description here

In addition, after some manipulating of code in order to allocated memory to member of structs, I have a build error at the format struct:

enter image description here

Does any one know the reason for this behavior and how I can resolve it?


Solution

    1. First in a nested structure, I have to allocate memory to the structure.

    2. There are multiple issues in structure, that cause issue with the way I allocate memory. For example, I used pointer to any array instead of array of pointers.

       struct format (*formatList)[5];     //this is no array of pointer but a pointer to the beginning of an array.
      

    I should just use:

    `struct format *formatList[5];`
    

    For such, I couldn't allocate memory as I wanted in step 1 properly.

    The suitable code for the declared structs are as below.

    struct PUCCH_ResourceSet *pucch_ResourceSetPtr = (struct PUCCH_ResourceSet*) malloc (sizeof(struct PUCCH_ResourceSet));
    
    printf("%d", sizeof(struct PUCCH_Resource*));
    for (int i = 0; i <32; i++){
        pucch_ResourceSetPtr->ResourceList[i] = malloc(sizeof(struct PUCCH_Resource));
        
        for (int j =0; j <5 ; j++)
        {
            pucch_ResourceSetPtr->ResourceList[i]->formatList[j] = malloc(sizeof(struct format));
        }
    }