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

This is a discussion on Small code works on VS6 crash on VS2005 WHY??? within the C Programming forums, part of the General Programming Boards category; This code runs without problems on VS6, but crashes on VS2005 (I've just started to use VS2005) Please WHY?? Code: ...

  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,672
    Code:
    struct Node *pTemp;
    puts("Adding node");
    pTemp=malloc(sizeof(Products));
    I used to be an adventurer like you... then I took an arrow to the knee.

  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
    Posts
    21,134
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    5,636
    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);
    If I have eight hours for cutting wood, I spend six sharpening my axe.

  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
    Posts
    21,134
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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, 11:51 AM
  4. Replies: 0
    Last Post: 02-21-2002, 05:05 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21