0

I followed a quite straight forward pseudo code for implementing a linked list of a struct in c, here it is: pseudo code for struct linked list

The problem i am facing is memory loss after adding the 4th node: After the 4th addition, the 4th node points to the 3rd node, which points to a weird address. prior to the 4th addition, the 3rd node pointed to the 2nd node - how did i lose the memory? here's my code:

RailPart *headPart = NULL;

void createRailPart(char *partInfo, char *connectTypes){
    char sPart[1];
    char ePart[1];
    char len[MAX_FIELD_SIZE];
    char price[MAX_FIELD_SIZE];
    char *noneDigit;

    //allocate memory
    RailPart *toPush = (RailPart*)malloc(sizeof(headPart));
    if (toPush == NULL){
        return;
    }

    //initialize data
    sscanf(partInfo, "%[^,],%[^,],%[^,],%[^,]", sPart, ePart, len, price);
    (*toPush).start = sPart[0];
    (*toPush).end = ePart[0];
    for (int i = 0; i < (int) strlen(connectTypes); i++){
        if ((toPush)->end == connectTypes[i]){
            (toPush)->ePartIdx = i;
        }
        if ((toPush)->start == connectTypes[i]){
            (toPush)->sPartIdx = i;
        }
    }
    (*toPush).len = (int) strtol(len, &noneDigit, BASE_10);
    (*toPush).price = (int) strtol(price, &noneDigit, BASE_10);

    //push the new node
    toPush->nextPart = headPart;
    headPart = toPush;
}

here's a documentation of the debugger, showing the memory loss i described clear & informative debugger overview Why am i losing this memory if i allocated it in the heap? what should i do in order to handle the behavior?

Edit: issue fixed, but now i encounter a circle in my linked list - It seems that the computer doesn't remember it already allocated certain parts of the memory and it allocates it again. as you can see, at the 4th iteration, a not in use memory is being allocated, but after reading the sscanf line it changes it's address. how is this possible?

circle in linked list

Errgod
  • 31
  • 1
  • 4
  • 4
    for every time you call `malloc`, call `free`. If you don't do it, even intentionally, the debugger will show you "leaked memory". – NadavS May 09 '20 at 15:35
  • 3
    One problem is here: `malloc(sizeof(headPart))` You're allocating the wrong size. `headfPart` is a pointer. You don't want to use the size of a pointer, you want the size of what it points to: `malloc(sizeof(*headPart))`, or better, `sizeof(RailPart)`. That should help with your memory corruption problem. Once that's fixed, you can go back and add the calls to `free`. – Tom Karzes May 09 '20 at 15:51
  • 1
    Also, don't use syntax like `(*toPush).len`. Just use `toPush->len`. – Tom Karzes May 09 '20 at 15:54
  • 2
    And you should not cast the return value of `malloc()`. https://stackoverflow.com/a/605858/3150445 – jwdonahue May 09 '20 at 16:14
  • Thank you for the comments! I fixed the size allocation, but now i encountered a circle in my linked list. It seems that at the 4th iteration, a new available memory is allocated, but after reading the sscanf line, the allocated address changes to an address that was already allocated. this causes a circle in the graph. i added another informative picture describing the issue – Errgod May 09 '20 at 17:47

0 Answers0