Henrik Klev
Henrik Klev

Reputation: 414

Adding a new entry to struct array overrides all last entries

I'm writing a program that reads information from a file, allocates memory for a struct for every line, and fills that struct with information. A pointer to the struct is then placed in a global array.

Every line in the file (except the first one) is in format 'Unsigned Char - Char - Unsigned Char - Char[]'.

My problem is that every time I add a new struct, the Char[] in all the other structs get overwritten.

Say I have the following file:

1 10 6 first
2 12 7 second
3 15 6 third

When I run my program with that file, and then print the global array it gives something like this output:

ID: 1 | FLAGG: 10 | STR_LEN: 6 | MODELL: third
ID: 2 | FLAGG: 12 | STR_LEN: 7 | MODELL: third
ID: 3 | FLAGG: 15 | STR_LEN: 6 | MODELL: third

instead of the expected:

ID: 1 | FLAGG: 10 | STR_LEN: 6 | MODELL: first
ID: 2 | FLAGG: 12 | STR_LEN: 7 | MODELL: second
ID: 3 | FLAGG: 15 | STR_LEN: 6 | MODELL: third

Code:

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

#define MAX_LENGTH 256

struct myStruct* globarlArray[MAX_LENGTH]

struct mySTruct{
    unsigned char ID;
    char FLAGG;
    unsigned char str_len;
    char* modell;
};

int set_variables(struct myStruct* s, unsigned char new_ID, char new_FLAGG, unsigned char new_str_len, char* new_modell){
    s->ID = new_ID;
    s->FLAGG = new_FLAGG;
    s->str_len = new_str_len;
    s->modell = new_modell;
    return 0;
}
int main(int argc, char* argv[]){
    //Read from file
    ...
    //Initialize structs, add variables to struct, and add struct to global array
    unsigned char ID;
    int ID_INT;
    char FLAGG;
    unsigned char str_len;
    char modell[253];
    char* tempstr;

    for (i = 1; i < lines; i++){
        tempstr = str[i]; //str[1] is first line of into from file, str[2] is second line,...
        Ruter_ID = tempstr[0];
        Ruter_ID_INT = Ruter_ID;
        FLAGG = tempstr[1];
        str_len = tempstr[2]; 
        sprintf(modell, "%s", tempstr+3);
        struct myStruct *line = (struct myStruct*)malloc(sizeof(struct myStruct));
        set_vars(line, ID, FLAGG, str_len, modell);
        globalArray[ID_INT] = line;
    }
    //print array
    for (i = 0; globalArray[i] != NULL; i++){
        printf("ID: %d | FLAGG: %d | str_len: %d | Modell: %s", 
        globalArray[i]->ID, globalArray[i]->FLAGG, globalArray[i]->str_len, 
        globalArray[i]->modell);
    }
    //Some more code
    ...
}

Thanks in advance!

Upvotes: 0

Views: 58

Answers (2)

R Sahu
R Sahu

Reputation: 206607

My problem is that every time I add a new struct, the Char[] in all the other structs get overwritten.

That happens since you are using:

    set_vars(line, ID, FLAGG, str_len, modell);

modell is an array. You are storing the pointer to the first element of that array in every instance of myStruct.

You need to allocate memory for every instance of myStruct and store that pointer1.

    char* copy = strdup(modell);
    set_vars(line, ID, FLAGG, str_len, copy);

You'll then need to make sure that the copy is deallocated.


1 strdup is not a standard library function but it can be easily implemented.

Upvotes: 1

tsfrank
tsfrank

Reputation: 261

Your modell array is being used over and over -- the same chunk of memory is being pointed to by each of your structures. You should give each of your structures a copy of the string (perhaps by using strcpy() or a related function. (You'll have to worry about freeing the memory of that copy, too, when you free the structure).

Upvotes: 0

Related Questions