Solved

Invalid variable value with lookup table/linked list

Posted on 2011-03-22
5
390 Views
Last Modified: 2012-05-11
The code below is commented with the details. Basically the b_hasVoted variable loses its value or never truly takes it when set  in between it being added to the linked list and the pointer to the element in the linked list being added to the Lookup Table. When accessing the variable directly in the linked list it shows the correct value.  I'm not sure if im missing something obvious or not. either way im running out of ideas and i've never used a LUT like this before. Any ideas what the issue is?

///in header
typedef struct answerpacket {
	char			messageType[100];
	int				TimeStamp;
	long			KeypadID;
	char			Value[100];
	unsigned long	sequence;
	unsigned long	retrys;
	SOCKET			sck;
	int				b_hasVoted;
} PackStruct;

Linkedlistclass AnswerList;
void **pointerLUT;
///////////end header

//in oninitdialog
AnswerList.create_list("AnswerList");

pointerLUT = NULL;
	
pointerLUT = (void**)malloc(sizeof (void *) * 11000);

if(pointerLUT == NULL){//debug
	int p=0;
	p=2;
}

// Populate the array with pointers.
PackStruct *newAnswerNode=NULL;
PackStruct *aaaarg=NULL;
for (int i=0; i<11000; i++) {
	newAnswerNode=NULL;
	newAnswerNode=(PackStruct *)malloc(sizeof(PackStruct));
	if(newAnswerNode == NULL){//debug
		int wtf=0;
		wtf=3;
	}
	newAnswerNode->KeypadID = i;
	if(i == 2000){//debug
		int f=0;
		f=7;
	}
	newAnswerNode->b_hasVoted = 2;//used to determine whether packet in LUT is being used. 
	pointerLUT[i] = AnswerList.append_newnode(newAnswerNode); //before stepping over newAnserNode->b_hasVoted shows it is ==2.  .append returns a pointer to the data in the LL
	
	answeris=(PackStruct *)AnswerList.list1->first->data;//this accesses the LL directly and the value in b_hasVoted shows the correct set value of ==2.

	
	aaaarg=NULL;
	aaaarg=(PackStruct *)pointerLUT[i];// aaaarg->b_hasVoted will show uninitilized value. The pointer returned matches with the one returned from the .append above like it should.
}

//more debug. b_hasVoted was a boolean to begin with but was changed to a int.
PackStruct *mss=NULL;

mss = (PackStruct *)pointerLUT[2000];
if(mss != NULL){
	if(mss->b_hasVoted){
		int t=0;
		t=8;
	}else if(!mss->b_hasVoted){
		int r=0;
		r=5;
	}

}

Open in new window

0
Comment
Question by:microwhat
5 Comments
 
LVL 1

Expert Comment

by:data_n_poker
ID: 35194845
Could you provide the code for AnswerList.append_newnode.  Based on the code, and analysis you have done, it would appear that this is the culprit as to why hasVoted isn't being set.  I just need to see that function.  Thank you
0
 
LVL 1

Expert Comment

by:data_n_poker
ID: 35194866
Also, are the other variables set when you access them?  

What may be happening is instead of returning the data, you are just returning an address, which gets wiped out after the AnswerList.append_newnode function ends.  
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35195086
>> Basically the b_hasVoted variable loses its value
Maybe you can find the problem using the debugger. Just step through your program and see at what point the variable loses its value.

If you haven't already done so, you can download the free Visual Studio Expresss C++ 2010:
    http://www.microsoft.com/express/Downloads/

Making programming errors is easy. Finding the problems is harder. So, using a good debugger is essential for development. I like the Visual Studio 2008 C++ debugger so much that I wrote articles on it. If you have VS 2010 the articles still apply. To quickly get started (about 15 minutes learning curve), you can read:

   C/C++ Beginner's Debugging Guide

After becoming familiar with the basics, move onto these two articles:
   Breakpoint Tips for C/C++

   Watch, Memory, Stack Tips: C/C++
0
 
LVL 33

Accepted Solution

by:
sarabande earned 500 total points
ID: 35197100
when you access the data directly in the linked list you were accessing only root (first) node.

when you get the uninitialized value you were casting the return value of AnswerList.append_newnode to a PackStruct pointer. what is quite different.

i would assume the append_newnode returns a pointer to a node of the linked list and if you would cast it to that pointer type you could access the 'data' member of that node which then correctly should be a PackStruct pointer.

 
Linkedlistnode * pn = (Linkedlistnode *)pointerLUT[i];
  aaaarg=(PackStruct *)pn->data;

Open in new window

 

now the aaaarg->bhas_Voted should be correct.

note the name 'Linkedlistnode' was a guess only. it could be a different name.

Sara
0
 

Author Closing Comment

by:microwhat
ID: 35202012
Thank you Sara. That was exactly the issue. I'm still learning pointers. The issue is very clear now.

Thank you to everyone else for the attempts.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to programmatically differentiate between C and Java 10 196
C++ Language error 28 217
I could not build boost code, 10 88
outlook office 365 8 122
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question