'visual studio unallocted pointer not NULL

I've encounter an issue in Visual Studio which drives me crazy. It is regarding unallocated pointers.

I wanted to write a simple linked list app.The problem is that freed and unallocated pointers are not NULL which prevents me from iterating the list.

Consider following C code

#include "stdafx.h"
#include <malloc.h> 

typedef struct _item
{
    char data;
    struct _item * pNext;
}item, *pItem;

int _tmain(int argc, _TCHAR* argv[])
{
    pItem listHead;
    pItem listTemp;
    pItem listCurr;

    listHead = (pItem) malloc(sizeof(listHead));
    listHead->data = '0';
    listHead->pNext = NULL; //will create exception in free

    listTemp = listHead;

    while(listTemp->pNext != NULL) //issue 1
    {
        listTemp = listTemp->pNext;//0xfdfdfdfd - never NULL? how to check?
    }

    listCurr = (pItem) malloc(sizeof(listHead));
    listCurr->data = '1';
    listCurr->pNext = NULL; //will create exception in free
    listTemp->pNext = listCurr;

    listTemp = listHead;
    while(listTemp->pNext != NULL)  //issue 2
    {
        printf("%d ", listTemp->data - 48); //"0 "
        listTemp = listTemp->pNext;
    }

    printf("%d ", listTemp->data - 48);
    free(listTemp); //is set to oxfeeefee not to NULL?  //issue 3


    listTemp = listHead;
    while(listTemp->pNext != NULL)  //issue 4
    {
        listTemp = listTemp->pNext;
    }

    free(listTemp);//Not null?

    return 0;
}

in line issue 1 and issue 2, listTemp->pNext is not NULL but 0xfdfdfdfd. This prevents from getting the last element in the list in line issue 3, free doesn't set the freed pointer to null but to 0xfeeefeee. This prevents me from getting last element again.

How can i handle these issues? Thanks for help.



Solution 1:[1]

You seem to have a few issues here. One problem you are having is that you are not allocating enough memory.

listHead = (pItem) malloc(sizeof(listHead));

listHead is a pointer. So you only allocate enough memory to hold a pointer, and not to hold your entire item struct. It should be:

listHead = (pItem) malloc(sizeof(item));

I can't see how issue 1 could ever not be NULL the first time through. Did you step through with a debugger? However, the problem with not allocating enough memory could definitely cause the problem with free(), and it's a little difficult to say for sure what other problems it might cause.

Solution 2:[2]

Syntax altered slightly to suit my compiler. The two main issues were (1) not allocating enough memory, as already commented. (2) wrong sequence for parsing the list.

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

typedef struct item {
    char data;
    struct item * pNext;
} item, *pItem;

void show (pItem list, int cue) {
    printf("List %d: ", cue);
    while(list != NULL) {
        printf("%c ", list->data); 
        list = list->pNext;
    }
    printf("\n");
}

int main(int argc, char* argv[]) {
    pItem listHead, listTemp, listCurr;

    listHead = malloc(sizeof(item));
    listHead->data = '0';
    listHead->pNext = NULL;
    show(listHead, 1);

    listCurr = malloc(sizeof(item));
    listCurr->data = '1';
    listCurr->pNext = NULL;
    listHead->pNext = listCurr;
    show(listHead, 2);

    printf("Freeing: "); 
    while(listHead != NULL) {
        listTemp = listHead;
        printf("%c ", listHead->data); 
        listHead = listHead->pNext;
        free(listTemp);
    }
    printf("\n");

    show(listHead, 3);
    return 0;
}

The above code follows your method of adding the next item to the tail of the list, but I would normally add it before the head and set a new listHead.

listCurr = malloc(sizeof(item));
listCurr->data = '1';
listCurr->pNext = listHead;
listHead = listCurr;

and this will also work for the first item provided you initialised listHead = NULL to indicate an empty list.

Solution 3:[3]

The answer is that you have to set the pointer to the freed memory to NULL yourself. The free function will only release the memory at the pointer back to the heap. The pointer parameter is passed by value and cannot be modified by the free function itself.

Also, you will need to retain a reference to the previous item in your list, so that when you do free the memory and set the pointer to NULL, you do it in the list items and not on the temporary pointer.

listCurr = NULL;
listTemp = listHead;

while(listTemp->pNext != NULL)
{
    listCurr = listTemp;
    listTemp = listTemp->pNext;
}

if(NULL != listCurr)
{
    free(listCurr->pNext);
    listCurr->pNext = NULL;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Evil Dog Pie