Solved

Invalid variable value with lookup table/linked list

Posted on 2011-03-22
5
400 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 34

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

Title # Comments Views Activity
How to print into std::string like sprintf using C++11? 12 118
Beginner to Unreal Engine 4 5 105
Why isn't object file created? 6 90
Precision Problem in C++ 7 49
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

749 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