what is wrong with this code please

This is a discussion on what is wrong with this code please within the Windows Programming forums, part of the Platform Specific Boards category; okay i have the following structure in place: Code: typedef struct connections connections; struct connections{ // Structure to contain information ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    54

    what is wrong with this code please

    okay i have the following structure in place:

    Code:
    typedef struct connections connections;
    struct connections{   // Structure to contain information about connections to items
         int ID;   // The identification number of the connected item
         float strength;   // A float to represent the strength of the   connection.
    	 connections * next;   // Pointer to the next connection in the list.
    };
    
    
    typedef struct theNode theNode;
    struct theNode{   // The main structure that represents an item of Organism or Resource
    	 int id;   // The identification number of the item
    	 float levels[ARMAX];   // The array of floats. Limited to 5 indexes
    	 connections * conn;   // Pointer to a list of connections
    };

    all this works fine i assume cause it gives me no errors in other parts of the code, the next section keeps crashing my system

    Code:
    struct theNode *temp = NULL;
    	struct connections  *tmp = NULL;
    	int len = 0;
    	float strength; //Variable used to hold connection strength
    	
    	switch(message)
    	{
    	case WM_INITDIALOG:
    		return TRUE;
    	case WM_COMMAND:
    		switch(LOWORD(wParam))
    		{
    		case IDOK:
    			len = GetWindowTextLength(GetDlgItem(hwnd, IDC_EDIT_STRENGTH));
    			if(len > 0)
    			{
    				char* buf1;
    				buf1 = (char*)GlobalAlloc(GPTR, len + 1); //allocate memory
    				GetDlgItemText(hwnd, IDC_EDIT_STRENGTH, buf1, len + 1);
    				//call to convertTextToNum function and populate traits array
    				strength = convertTextToNum(buf1);
    				GlobalFree((HANDLE)buf1); //free the memory
    
    
    				ConPrintf( hCon,255,"ID # = %d\n", index);//debug
    				ConPrintf( hCon,255,"Connection ID # = %d\n", index2);//debug
    				ConPrintf( hCon,255,"Connection Strength = %0.3f\n", strength);//debug
    		
    
    				temp = theItems[index];
    
    				tmp = temp->conn;
    				
    				tmp->ID = index2; // <----crashes here in debug when i watch
    				tmp->strength = strength;
    
    				addConnection2(tmp, index); //add connections to organism/resource
    				
    			}
    			
    			EndDialog(hwnd, IDOK);
    			break;
    		}
    		break;
    		default:
    			return FALSE;
    	}
    	return TRUE;
    both index and index2 are static ints declared elsewere, i am new to visual studio and am confused by the CXX0030 code that appears when i debug

    i hope i explained this and gave enough code

    thanx in advance

    korbitz

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    	temp = theItems[index];
    
    	tmp = temp->conn;
    				
    	/* Add line to see value of tmp before we use it */
    	ConPrintf( hCon,255,"tmp = %p\n", tmp);//debug
    
    	tmp->ID = index2; // <----crashes here in debug when i watch
    When you crash dereferencing a pointer it means that the pointer is either NULL or not valid. Therefore your problem is not here but when you set(or fail to set) the value of temp->conn.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    thanx for the reply

    okay checked value of tmp and its output is 00000000

    Therefore your problem is not here but when you set(or fail to set) the value of temp->conn.
    hmmmm, bit unsure what you mean there, i assume i need to initialise the variables contained within the structure

    or am i missing the point all together??

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >>okay checked value of tmp and its output is 00000000<<
    In the context of a pointer 0 is NULL.

    >>hmmmm, bit unsure what you mean there, i assume i need to initialise the variables contained within the structure<<

    Yes you should have done this earlier in your code or maybe you want to allocate temp->conn if it is NULL?

    Code:
    if (temp->conn == NULL)
    {
            temp->conn = (connections *) malloc(sizeof(connections));
            if (temp->conn == NULL) exit(-1); // Out of memory
    }
    
    tmp = temp->conn;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 08:10 AM
  2. Replies: 4
    Last Post: 12-07-2002, 04:24 PM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 01:51 AM

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