?
Solved

Invalid variable value with lookup table/linked list

Posted on 2011-03-22
5
Medium Priority
?
414 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 35

Accepted Solution

by:
sarabande earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

765 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