Thread: Small code works on VS6 crash on VS2005 WHY???

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    84

    Small code works on VS6 crash on VS2005 WHY???

    This code runs without problems on VS6, but crashes on VS2005 (I've just started to use VS2005)
    Please WHY??

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct
    {
    	int iCode;
    	char *pName;
    	double dPrice;
    } Products;
    
    struct Node
    {
    	Products products;
    	struct Node *pNext;
    	struct Node *pPreview;
    };
    
    void AddNodeHead (struct Node **pNode)
    {
    	struct Node *pTemp;
    	puts("Adding node");
    	pTemp=malloc(sizeof(Products));
    	
    	puts("Done!");
    
    	if (pTemp==NULL)
    	{
    		puts("Failed to allocate!");
    		// Delete All
    		exit(1);
    	}
    
    	pTemp->pNext=*pNode;
    	pTemp->pPreview=NULL;
    
    	if (*pNode)
    		(*pNode)->pPreview=pTemp;
    
    	*pNode=pTemp;
    }
    
    
    int main ( void )
    {
    	struct Node *pHead=NULL;
    	int i;
    
    	for (i=0;i<3;++i)
    		AddNodeHead(&pHead);
    	return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    struct Node *pTemp;
    puts("Adding node");
    pTemp=malloc(sizeof(Products));
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    Thank you hk_mp5kpdw.
    Howerver I still can't understand what I did wrong...

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The size of a Node structure is not the same as the size of a Products structure. You got undefined behavior.
    You allocate too little space to hold your Node structure.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    I can't believe I did such a stupied mistake.

    Thank you guys!

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by salvadoravi View Post
    I can't believe I did such a stupied mistake.

    Thank you guys!
    to avoid such mistakes use the working always patter

    pObj = malloc (sizeof *pObj);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User aLiNuSh's Avatar
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Posts
    7
    Quote Originally Posted by vart View Post
    to avoid such mistakes use the working always patter

    pObj = malloc (sizeof *pObj);
    Shouldn't this, dereferencing an unallocated pointer, cause undefined behavior?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Not with the use of sizeof.
    Sizeof doesn't dereference the pointer, but merely evaluates the type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-27-2008, 10:25 PM
  2. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  3. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM
  4. Replies: 0
    Last Post: 02-21-2002, 06:05 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM