What is CrtIsValidHeapPointer(pUserData)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ahmed mahmoud
    New Member
    • Feb 2009
    • 9

    What is CrtIsValidHeapPointer(pUserData)

    I am new to C programming language
    i am trying to implement linked list using C
    but i found that error CrtIsValidHeapP ointer(pUserDat a)
    i don't know what is it

    my code
    Code:
    struct node
    {
    	int value;
    	struct node * next;
    };
    
    void addNode(struct node * t)
    {
    	t->next = (struct node *)malloc(sizeof(struct node));
    	if(t->next != NULL)
    	{
    		(t->next)->value = 5;
    		(t->next)->next = NULL;
    	}
    	else
    	{
    	   printf("FATAL ERROR NO MEMORY");
    	}
    }
    
    
    int main(array<System::String ^> ^args)
    {
    	struct node * p;
        struct node head;
    	head.value = 1;
    	head.next = NULL;
    	p = & head;
           addNode(p);
    	while(p != NULL)
    	{
    		printf("%d\n",(*p).value);
    		p = (*p).next;
    	}
    	p = &head;
    	free(p->next);
    	p->next = NULL;
    	free(p);
    	p = NULL;
        return 0;
    }
    i know that there is lot of things that is not implemented efficiently but now i am focusing only on this error
    thanks in advance
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You call free(p) but p has been set to &head and not malloced so you are trying to free memory that is not allocated on the heap and hence you get the error.

    Comment

    Working...