473,396 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pointer Issues ( I think)

158 100+
Ok in short I have a linked. And my problem is that after the first node level the next pointer points to itself and loops forever.


But its more complicated, ill try to explain what im doing the best I can. I would post all the code but it would be way to many files.

I have multi dimensional array of objects declared like:

Expand|Select|Wrap|Line Numbers
  1.  Target targets[10][100]; 
The Target object has a bunch a data in but most importantly for my problem it has a pointer to another Target object. Which is the next variable in most linked lists. The pointer is declared like:

Expand|Select|Wrap|Line Numbers
  1.   Target* lastDetected; 
My code works like this.

Each column in the targets array (theres 10 of them) refers to frame or time t. So after each loop the columns are copied like so: 0->1->2...8->9. Each node or Target object is copied individually. Code that does that is:

Expand|Select|Wrap|Line Numbers
  1.     for (int i = 10- 1; i > 0; i--) {
  2.         for (int j = 0; j < 100; j++) {  
  3.           targets[i][j] = targets[i - 1][j];
  4.         }
  5.     }
  6.  

Well else where in the code the next or lastDetected variable in each object in column 0 is set to another Target object in column 1 of the list of all targets.


However when I try to run in the Target object class. It only works correctly for one time then it goes into an infinite loop.

Expand|Select|Wrap|Line Numbers
  1.  
  2.     Target *nTarget = this;
  3.  
  4.      while(nTarget->lastDetected != NULL)
  5.  
  6.          printf("->%d",nTarget->id);
  7.          nTarget = nTarget->lastDetected;
  8.  
  9.     }
  10.  


Any Thoughts?
Apr 29 '10 #1

✓ answered by donbock

Copying the target column breaks all of the embedded links -- they still point to the object in the old column.

Are you sure it is necessary to copy the columns? You could keep an int variable called thisFrame which holds the column index for the current frame. Each frame you increment thisColumn, being careful to wrap it around back to zero. To access older frames, call a priorFrame function that decrements its argument, being careful to wrap it around back to 9.

8 1331
donbock
2,426 Expert 2GB
Copying the target column breaks all of the embedded links -- they still point to the object in the old column.

Are you sure it is necessary to copy the columns? You could keep an int variable called thisFrame which holds the column index for the current frame. Each frame you increment thisColumn, being careful to wrap it around back to zero. To access older frames, call a priorFrame function that decrements its argument, being careful to wrap it around back to 9.
Apr 29 '10 #2
kardon33
158 100+
"Copying the target column breaks all of the embedded links -- they still point to the object in the old column. "

Could you please explain why that is so. Is there some rule in C++ for why that cant happen? So I can avoid later mistakes.

So what you are saying is I should have a variable to that would start at zero and every frame it would increment one, but when I reach the end of the array (so 9) I bump it back down to zero and the previous frame would be 9.

Well thats at least what im going to try.


Thanks for your help.
Apr 30 '10 #3
kardon33
158 100+
Just tested the concept, worked perfectly.

Thanks again.
Apr 30 '10 #4
donbock
2,426 Expert 2GB
@kardon33
There is no rule in C or C++ that will automatically protect you from having pointers that point someplace other than where you want them to point.

Suppose target[3][50] contains a pointer to target[3][65]. Then you copy the columns. The information that used to be in target[3][50] is now in target[4][50]; and the information that used to be in target[3][65] is now in target[4][65]. However, the pointer that was in target[3][50] (and now is in target[4][50]) still points to target[3][65], which now has completely different contents.

If your pointers never need to link to other columns, then you don't need an actual pointer. Instead, store the row index of the element you want to link to. Those will copy just fine from column to column.
Apr 30 '10 #5
kardon33
158 100+
OK I see thanks, too bad I am always setting my pointers to the other columns creating a relation between each column.
Apr 30 '10 #6
donbock
2,426 Expert 2GB
@kardon33
OK. Then your link could be accomplished by two fields: an absolute row number (as described earlier) and a relative column number.

To follow the link:
add the relative column link value to the current column number (being careful to handle wrap-around properly);
and set the row to the absolute row link value.

This sort of linking would work well despite copying columns ... except for links that point to the column that just got shifted out of the array. There may be an easier way, but my first thought is that your column shifting activity has to also include a scan through all the array elements looking for links that point to the soon-to-be-disappeared column and set those links to a code that means no-link.
Apr 30 '10 #7
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. while(nTarget->lastDetected != NULL) 
  2.  
  3.          printf("->%d",nTarget->id); 
  4.          nTarget = nTarget->lastDetected; 
In this code the loop ends with the printf. Looks very odd indeed. Maybe some missing braces?
May 3 '10 #8
kardon33
158 100+
When I copied the source code the bracket was lost (im not sure how), its in my source code though. That would create a problem if it wasn't :).
May 3 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: PapaTodd | last post by:
Greetings all, I've been fighting with this issue all day. This code is tried and true on other OS's, but when I compile and test on AIX 5.3 using xlC_r, I have problems. Here is the code...
3
by: FaXiA | last post by:
Hi all, I'm working on a program that reads a structure from a file (1000 records). The file read part works great, but I'm hitting a roadblock on howto read the data from the pointer. I'm...
2
by: bob | last post by:
In some code I have the following: 1) (*p) ->value; 2) *p->value; 1) gives an error while 2) works. What are the difference between 1) and 2)?
34
by: sumedh..... | last post by:
double * X size of X->?? size of X->?? double (*X) size of X->?? size of X->??
7
by: agitlin | last post by:
Hello All, I'm working with a SDK for a piece of hardware attached to a Windows CE device. The sample code provided is all written in eVC++ and I'm trying to port it over to a VB.NET...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.