Bryan Rosen
Bryan Rosen

Reputation: 35

strings in linked list are overlapping with each other C

I'm trying to make a structure to document a group of people. For some reason whenever I input the person's room name, the room name replaces the person's first name. This shows in Test 5 within the addResi function.

struct resi {
    char *firstname;
    char *lastname;
    double stnscore;
    char *roomName;
    struct resi *next;
};
struct resi *head = NULL;
struct resi *current = NULL;

void printAll() {
    struct resi *outer = head;
    if (outer == NULL) {
        printf("Empty\n"); fflush(stdout);
    }
    while (outer != NULL) {
        printf("First Name: %s, Last Name: %s, Score: %lf, roomName: %s\n", outer->firstname, outer->lastname, outer->stnscore, outer->roomName); fflush(stdout);
        outer = outer->next;
    }
}
void addResi(char *firstname, char *lastname, double stnscore, char *roomName) {
    printf("test 0\n"); fflush(stdout);

    struct resi *link = (struct resi*) malloc(sizeof(struct resi));
    printf("test 1 %s\n", firstname); fflush(stdout);

    strcpy(link->firstname, firstname);
    printf("test 2 %s\n", link->firstname); fflush(stdout);

    strcpy(link->lastname, lastname);
    printf("test 3\n"); fflush(stdout);

    link->stnscore = stnscore;
    printf("test 4\n"); fflush(stdout);

    strcpy(link->roomName, roomName);
    printf("test 5 %s %s\n", link->firstname, link->roomName); fflush(stdout); //they shouldn't be the same.

    link->next = head;
    head = link;
}

int main (void)
{
    int totalStud, tempX = 0;
    char firTemp[21], lasTemp[21], roomTemp[21];
    double scrTemp;

    printf("How many residences?\n"); fflush(stdout);
    scanf("%d", &totalStud);
    if (totalStud < 5) {
        printf("The number %d is less than 5, please type a different number\n",
            totalStud); fflush(stdout);
    }
    while (totalStud < 5) {
        scanf("%d", &totalStud);
    }

    printf("type the residences with following format\nfirst name last name score room\n"); fflush(stdout);

    for (tempX = 0; tempX < totalStud; tempX++) {
        scanf("%20s %20s %lf %20s", firTemp, lasTemp, &scrTemp, roomTemp);
        printf("test mid %s %s %lf %s\n", firTemp, lasTemp, scrTemp,
            roomTemp); fflush(stdout);
        addResi(firTemp, lasTemp, scrTemp, roomTemp);
        printAll();
    }
}

if I typed "5", then "Bob Billy 45.5 Jackson" the last output should look like "First Name: Bob, Last Name: Billy, Score: 45.500000, roomName: Jackson" but instead it shows up as "First Name: Jackson, Last Name: Billy, Score: 45.500000, roomName: Jackson"

Upvotes: 1

Views: 114

Answers (2)

Dino Dini
Dino Dini

Reputation: 463

It is just luck that it got as far as it did without crashing. The behaviour is undefined because you are copying to whatever memory firstname, lastname and roomname are pointing to and these were never set to anything meaningful. Whenever you allocate memory with malloc, the contents of that memory is essentially random until you initialise it. As already mentioned, you can probably make a change by using an array definition such as firstname[256], but another solution is to use strdup instead of strcpy, which will make a copy of a string. You would need to free this memory later if you delete an instance of the struct to avoid a memory leak.

Upvotes: 0

cxw
cxw

Reputation: 17051

A resi doesn't actually hold the space for the names - it just hold pointers to them. The minimal change I know of is to change char *firstname to char firstname[256], and likewise for the other char * fields in resi.

The pointers in resi hold locations in memory where characters go, not the characters themselves. When you malloc, those locations are unspecified - they could be anything. Therefore, the strcpy calls put characters somewhere in memory, but we're not sure where!

Since you have not defined where to put those characters, I suspect some of those random locations in memory are overlapping, so several strcpy calls are putting data in the same part of memory. That could cause the behaviour you are seeing.

Upvotes: 1

Related Questions