1

I'm stuck on how to correct implement date inside a struct that been declared with malloc.

I have these two structs:

typedef struct {
int idWork;
float area;
//char tarifa[MAX_TARIFA_LENG];
int tarifa;
} tWorkspace;

typedef struct{
tPartner *socis;
tWorkspace workP[50];
tContract *contract;
int numSocis;
int numWork;
int numContract;
} tCoworking;

Then, I have this function which I didn't coded right

tCoworking* new_partner(tCoworking *a, int partnerId, char *nam, int descom){
bool exist = false;

a->socis=(tPartner*)malloc(sizeof(tPartner*));

printf("ID %d\n", partnerId);

//CHECK IF PARTNERID EXISTS IN THE LIST
if(a->numSocis != 0){
    for(int i=0; i<a->numSocis; i++){
        if(a->socis[i].id == partnerId){
            exist = true;
            printf("\nERROR: Partner %d is already in the list of partners\n",partnerId);
        }
    }
}
if(exist != true){
    //Check if there's no socis
    if(a->numSocis != 0){
        a->socis[a->numSocis].id = partnerId;
        strcpy(a->socis[a->numSocis].name, nam);
        a->socis[a->numSocis].descompte = descom;
        a->numSocis = a->numSocis+1;
    }else{
        a->socis[0].id = partnerId;
        strcpy(a->socis[0].name, nam);
        a->socis[0].descompte = descom;
        a->numSocis = 1;
    }
}
return a;
}

In main, I have:

tCoworking c;

and this is how I call the function with the data:

new_partner (&c, 11, "Anwar Sherman", 10);

What happens is that when I call it for the first time a->socis[0].id gives me the ID 11, which is correct.

But when I call it again new_partner (&c, 16, "Isa Warner", 20); the index 0 lose the previous data and in index 1 writes the data correctly.

I mean, on second call of function, the struct gives me this:

index 0 = 110

index 1 = 16

If I keep calling the function, the previous data is set 0 and the data I pass it "saves"

Why do I use malloc ? I don't know the exact number of users that I will get, so I'm trying to work with Dynamic Memory Allocation and Dynamic Structures

Sorry for bad english and bad explanation, I tried my best.

Thanks.

EDIT

Changed to a->socis=(tPartner*)malloc(sizeof(tPartner))

When I call for the third time my function with different data, it looks like this:

index 0 = 1852989783
index 1 = 0
index 2 = 24

Function calls with data are:

  new_partner (&c, 11, "Anwar Sherman", 10);
  new_partner (&c, 16, "Isa Warner", 20);
  new_partner (&c, 24, "Reagan Sawyer", 8);

More examples:

new_partner (&c, 11, "Anwar Sherman", 10);
new_partner (&c, 16, "Isa Warner", 20);
new_partner (&c, 24, "Reagan Sawyer", 8);
new_partner (&c, 67, "Hashir Lloyd", 10);

What I get:

index 0 = 0
index 1 = 1394634337
index 3 = 0
index 4 = 67
outsider
  • 45
  • 9
  • 1
    `malloc(sizeof(tPartner*));` should be `malloc(sizeof(tPartner))`. You need enough space to hold the entire `tPartner` object, but you only allocated enough space to store a pointer. General rule of thumb: the type argument to `sizeof` inside `malloc` should have one less `*` than the type of the pointer to which the result is being assigned. – Nate Eldredge Oct 02 '20 at 20:38
  • Alternatively, dereference the pointer being assigned instead of trying to rewrite the type: `ptr = malloc(sizeof(*ptr))`. – Nate Eldredge Oct 02 '20 at 20:39
  • Change made. Now when I call for the second time my function, the index 0 is 0, before it was 110. Now it not saving anything. I'm edting my question – outsider Oct 03 '20 at 09:35
  • Oh, you are overwriting `a->socis` with every call to the function, with a pointer to an array that only contains enough space for one `tPartner`. It seems like you want this to point to an array containing all the previous `tPartners`, and so you will need to use `realloc` to expand it instead of overwriting it. – Nate Eldredge Oct 03 '20 at 14:15
  • @NateEldredge This worked, If you want add this is a answer I will accept it. – outsider Oct 07 '20 at 12:54

1 Answers1

1

Try changing this:

a->socis = malloc(sizeof(tPartner*));

to this:

a->socis = malloc(sizeof(tPartner));

since you want to add an object of type tPartner. That requires allocating memory for an object of that size, and not of its pointer, as you do in your code.


PS: Unrelated to your issue: Do I cast the result of malloc? No.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Change made. Now when I call for the second time my function, the index **0** is ```0```, before it was ```110```. Now it not saving anything. I'm edting my question – outsider Oct 02 '20 at 20:51