Home > OS >  How are arrays of Structs with arrays and non-arrays organized in memory?
How are arrays of Structs with arrays and non-arrays organized in memory?

Time:06-07

I just started learning structs, and I'm trying to use them with pointers to get better skills at C programming. Now, in the exersice I have to preload 2 of the 4 employees that can be loaded on the program (specifically, slot 0 and 1 of db1[4]). Now, while preloading, the program is able to load the first employee (name, sex and salary), but upon trying to load the 2nd employee, I get a segmentation fault (line 27).

My proffessor told me that by getting the size of the struct definition with sizeof(db_mold), I, in theory, would be able to jump slot by slot, by multiplying n times the result of sizeof(db_mold) and adding it to the pointer.

sizeof(db_mold) appears to be 28, but after using the formula (sizeof(db_mold)*n) and adding it to db1p, I get a segmentation fault.

Now, as I've read in another question, structs can have padding, but I didn't understand where are those paddings placed, and how big they were. Also, something that isnt mentioned in that question, is what would happen if i had an AoSwAaNA (Array of Structs with Arrays and Non-Arrays).

Here is my code:

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

typedef struct {
    char nombre[20];
    char sexo;
    unsigned int sueldo;
}db_mold;

void preload(db_mold *, int);
void user_load(db_mold *, int);

int main(){
    db_mold db1[4] = {0};
    preload(&db1[0], (sizeof(db_mold)));
    user_load(&db1[0], (sizeof(db_mold)));
}

void preload(db_mold *db1p, int sizedb){
    strcpy((db1p (sizedb*0))->nombre, "Franco");
    (db1p (sizedb*0))->sexo = 'M';
    (db1p (sizedb*0))->sueldo = 20000;

    strcpy((db1p (sizedb*1))->nombre, "Romina");
    (db1p (sizedb*1))->sexo = 'F';
    (db1p (sizedb*1))->sueldo = 23000;
}

void user_load(db_mold *db1p, int sizedb){
    for (int i = 2; i < 4; i  ){
        char tmp[20] = {0};
        printf("Ingrese el nombre del empleado %d: ", i);
        fgets(tmp, sizeof tmp, stdin);
        strcpy(tmp, (db1p (sizedb*i))->nombre);
        //fgets((db1p (sizedb*i))->nombre, sizeof *((db1p (sizedb*i))->nombre), stdin);
        printf("Ingrese el Sexo (M o F) del empleado %d: ", i);
        (db1p (sizedb*i))->sexo = getchar();
        getchar();
        printf("Ingrese el salaio del empleado %d: ", i);
        scanf("%u", &(db1p (sizedb*i))->sueldo);
        printf("Empleado Cargado!\n");
    }
}

NOTE: It isn't finished

CodePudding user response:

Either your professor was mistaken or you misunderstood. You don't need to multiply by the size of the structure, because pointer arithmetic does this automatically. If you do it as well, all your indexes are multiplied twice, which accesses outside the array, causing undefined behavior.

You would only need to do this if you converted the pointer to char* before doing the arithmetic.

Padding is not a problem because sizeof includes the padding.

  • Related