Solved

Invalid variable value with lookup table/linked list

Posted on 2011-03-22
5
377 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 32

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now