Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Invalid variable value with lookup table/linked list

Posted on 2011-03-22
5
Medium Priority
?
421 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

598 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