Dynamically allocated list of dynamically allocated structs is overwriting

RyanL

In the following code, I can't figure out for the life of me why I get the same output (ie the same name) when running the dump_data() method after adding 2 (or more) User_struct (with different names) using the addUser() method.

struct Friends_struct {
    struct User_struct *this;
    struct Friends_struct *next;
};

typedef struct Friends_struct *Friends;    

typedef struct User_struct {
    const char *name;
    Friends amigos;
} User;

static User **userList;             // global table of users
static int numUsers;                // global number of users
static int tableSize;               // global size of table


void create_amigonet() {    
    tableSize = INIT_TABLE_SIZE;
    numUsers = 0;

    userList = malloc(tableSize * sizeof(User *));
}

void addUser(const char *name) {
    userList[numUsers] = malloc(sizeof(User));
    userList[numUsers]->name = name;
    userList[numUsers]->amigos = 0;

    numUsers++;

    if (numUsers == tableSize) {
        tableSize += INIT_TABLE_SIZE;

        User **moreUsers;
        moreUsers = realloc(userList, tableSize * sizeof(User *));
        userList = moreUsers;
    }
}

void dump_data() {
    for (int i=0; i<numUsers; i++) {
        printf("%s; friends:", userList[i]->name);
    }

    printf("\n");
}

Different .c file:

void do_add(const char *name) {
    addUser(name);
}

int main(void) {
    create_amigonet();
    do_add("Dan");
    do_add("Jim");
    dump_data();
}

If this is run, the output I expect would be something like

Dan; friends:
Jim; friends:

yet I am getting

Jim; friends:
Jim; friends:

How/where am I overwriting my data?

Christoph Freundl

The problem is that you are storing only a char pointer in User_struct and the pointers you pass in the addUser calls are only temporaries. You should change the type of name to a char array of sufficient length and use strcpy to copy the name within addUser.

I cannot fully explain why the example is actually showing the error because I thought that both name strings should be valid throughout main() but it seems that the generated code is reusing the location where "Dan" was stored and therefore you are getting "Jim" twice after the second addUser call.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Freeing a Dynamically Allocated Array of Structs After Function Return.

From Dev

How to erase all data from an array of structs that is not dynamically allocated

From Dev

Create Dynamically Allocated Array with Pointers to Structs C++

From Dev

Dynamically allocated array of structs passed to function and accessed with indexing

From Dev

Unwanted behaviour when accessing/freeing array of dynamically allocated structs

From Java

Initializing Dynamically allocated integer

From Dev

Is string dynamically allocated?

From Dev

free with dynamically allocated memory

From Dev

Reallocating dynamically allocated struct

From Dev

Deletion of Dynamically Allocated Memory

From Dev

Dynamically allocated object producer

From Dev

Dynamically Allocated Memory Constructor

From Dev

free a dynamically allocated array inside another dynamically allocated array

From Dev

std::copy for dynamically allocated pointers

From Dev

Dynamically allocated array in C++

From Dev

Incorrect read in dynamically allocated matrix

From Dev

How to extend a dynamically allocated array

From Dev

Dynamically allocated string arrays in C

From Dev

looping through a dynamically allocated matrix

From Dev

dynamically allocated memory with garbage characters

From Dev

Initializing structures with dynamically allocated memory

From Dev

Dynamically allocated array in C++

From Dev

FILE pointer in C, dynamically allocated?

From Dev

free dynamically allocated memory in QT

From Dev

Incorrect read in dynamically allocated matrix

From Dev

Write dynamically allocated structure to file

From Dev

binary search on dynamically allocated array

From Dev

Dynamically allocated string arrays in C

From Dev

Properly free dynamically allocated struct

Related Related

  1. 1

    Freeing a Dynamically Allocated Array of Structs After Function Return.

  2. 2

    How to erase all data from an array of structs that is not dynamically allocated

  3. 3

    Create Dynamically Allocated Array with Pointers to Structs C++

  4. 4

    Dynamically allocated array of structs passed to function and accessed with indexing

  5. 5

    Unwanted behaviour when accessing/freeing array of dynamically allocated structs

  6. 6

    Initializing Dynamically allocated integer

  7. 7

    Is string dynamically allocated?

  8. 8

    free with dynamically allocated memory

  9. 9

    Reallocating dynamically allocated struct

  10. 10

    Deletion of Dynamically Allocated Memory

  11. 11

    Dynamically allocated object producer

  12. 12

    Dynamically Allocated Memory Constructor

  13. 13

    free a dynamically allocated array inside another dynamically allocated array

  14. 14

    std::copy for dynamically allocated pointers

  15. 15

    Dynamically allocated array in C++

  16. 16

    Incorrect read in dynamically allocated matrix

  17. 17

    How to extend a dynamically allocated array

  18. 18

    Dynamically allocated string arrays in C

  19. 19

    looping through a dynamically allocated matrix

  20. 20

    dynamically allocated memory with garbage characters

  21. 21

    Initializing structures with dynamically allocated memory

  22. 22

    Dynamically allocated array in C++

  23. 23

    FILE pointer in C, dynamically allocated?

  24. 24

    free dynamically allocated memory in QT

  25. 25

    Incorrect read in dynamically allocated matrix

  26. 26

    Write dynamically allocated structure to file

  27. 27

    binary search on dynamically allocated array

  28. 28

    Dynamically allocated string arrays in C

  29. 29

    Properly free dynamically allocated struct

HotTag

Archive