LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-04-2018, 11:55 PM   #1
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,416
Blog Entries: 43

Rep: Reputation: 36
C++: How to Know I'm Storing and Retrieving from the Same index


I notice that one of my pitfalls is I store something in my Array object in one location, and try to retrieve it by using a different index, which doesn't work. How do I know, when it gets really complex that I'm storing and retrieving from the same index?
 
Old 02-05-2018, 01:59 AM   #2
jareklakman
LQ Newbie
 
Registered: Jun 2017
Posts: 14

Rep: Reputation: Disabled
Maybe compare / display, for debug purpose, pointers?
 
Old 02-05-2018, 06:44 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by des_a View Post
I notice that one of my pitfalls is I store something in my Array object in one location, and try to retrieve it by using a different index, which doesn't work. How do I know, when it gets really complex that I'm storing and retrieving from the same index?
An object in an array, is only ever at that one location. If you try to use a different index value, then you will be referencing a different location. If this is about your language that you've been envisioning for some time, doubtful anyone can help you. If this is about a another language, post the code.
 
Old 02-05-2018, 12:10 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
exactly, if you're using an array then putting something into one of the elements, no matter what language you're using. if you put it in element number 4 do not expect to find it in element number 5 or 6.

That is like putting your car keys in your left pocket then putting your hand in your right pocket expecting to find your car keys when you need them. come one, where is the logic in that I ask you.

Last edited by BW-userx; 02-05-2018 at 12:12 PM.
 
Old 03-10-2018, 10:33 PM   #5
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,416

Original Poster
Blog Entries: 43

Rep: Reputation: 36
Sorry. This needs clarification. I wasn't trying to say that I thought that if I put it into an index, I can retrieve it from another. What I was trying to say is that sometimes, that gets really complex. Sometimes, the access is way away from the code to store it. The code to store it will mostly always work okay for me in most cases.

But sometimes I have the weirdest problems retrieving it, because I'm using variables to retrieve it. They must be computed by logic, and the logic must always work. For example:

The storing:
Code:
++reg.inif2;
    ++ifcounter2;
   	
   	loc_if = ifcounter2;
   	unsigned long index = loc_if;
   	
   	loc = typescratch[index].loc;
   	
   	reg.inif[index].type.loc = loc;
   	reg.inif[index].type.ifb = loc;   	
    
	saw_if = true;
The retrieving:
Code:
if (reg.type != TVOID && reg.operand != 0)
   	 crash((char *)"Invalid ENDIF instruction.");
   	 
   	loc_if = ifcounter3;
   	unsigned long index = loc_if;
   	     	   	
	if ((reg.ninstruction == IIFELSE || reg.ninstruction == IEIF))
	{
	 if (reg.inif[index].type.elseb != 0)
	 {
	  reg.inif[index].type.endif = reg.inif[index].type.elseb - 3;
     }
     else	  	  
   	  reg.inif[index].type.endif = i;
   	 
   	 saw_endif = true;
Additional code:
Code:
ifcounter3 = reg.ifcounter;
Code:
reg.inif2 = 0;
  reg.ifcounter = 0;
  reg.currif = 0;
 
Old 03-10-2018, 10:34 PM   #6
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,416

Original Poster
Blog Entries: 43

Rep: Reputation: 36
...Think, I posted most of the variables...
 
Old 03-10-2018, 11:05 PM   #7
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
as much as I'd like to help. Looks like struct's and whatever you're doing with them.

Maybe start out smaller and work with setting values inside of your arrays, and then retrieving the values, and working with the element numbers until you get more familiar with arrays. then build up to using them in struct's or array of struct's.

Code:
#include <stdio.h>
int main(void) 
{
    int count1 = 0,count2 = 10;
    int array[10];
    
    while (count1 < 10)
    {
        ++count1;
        array[count1] = count2;
        count2++;     
        
    }
    for ( int a=0;a<10;a++)
        printf("%d\n", array[a]); 
    
    return 0;
}

Last edited by BW-userx; 03-10-2018 at 11:07 PM.
 
Old 03-11-2018, 03:30 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
would be nice to explain your problem better, the posted code is not enough to understand. What did you wanted to achieve, what did you expect, what's happened, what part of that code (or any other) is unclear ....
You may try to use ddd (debugger) to check the values probably.

Finally it looks like C code for me, not C++.
 
Old 03-12-2018, 08:33 AM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,647
Blog Entries: 4

Rep: Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933Reputation: 3933
C++ already ships with a wide variety of "container classes" which are far more sophisticated than simple "arrays."

Conceptually at least, an "array" is a contiguous block of storage containing a certain (fixed) number of consecutive elements which are numbered from zero upwards. Although the word, "index," is commonly used in this context as in "array index," it is not the same meaning as we use in, say, SQL. Or, for that matter, the "loosey goosey" way that they're used in some other languages. (In these languages, "arrays" are actually "associative arrays," which are not arrays at all but so-called "hash tables.")

C++ provides all of these same data structures and many more – ready to use right out of the box and [URL="https://en.wikipedia.org/wiki/Cracker_Jack"with a prize inside[/URL] – but it calls them by their proper names. In C++, the meaning of the word "array" is strict.

Last edited by sundialsvcs; 03-12-2018 at 08:37 AM.
 
Old 03-31-2018, 12:46 PM   #10
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,416

Original Poster
Blog Entries: 43

Rep: Reputation: 36
I guess this problem is too complex. I'm going to close it, and get final responses if any. If anybody has feedback on how to make it less complex, let me know. It's specific enough for a question, just too complex, although this helps some.

I can't really post more code, or we'd end up posting half the program, which would be a few pages (on printed paper). In this program, where I was having trouble with it, the interactions with Arrays, are quite complex at times, because for it to work, they need to be. But sometimes this makes it hard to find the correct index for me.

I have no trouble when it is a simple, few hundred liner program. But when it is a real program, I begin to have trouble. I need to use variables as the indexes and I need to calculate which values they will have based off of complex runtime scenerios.

I typically use my Array object, which I believe off the top of my head, uses vector. It's a little more complex than either a builtin array or a vector. It is pretty similar to Java's Array. I learned how to create it from reading books on the language.

If you get a bad index, it will complain, not crash. That's an advantage of using it, as well as the fact that it knows it's on length, and can be resized at runtime.

I use both structs and classes in this program. I don't know how to use any other container classes, or even what they are.

What I was trying to get an idea of, was the general ways, when it gets complex, to "check" to see if you are at the right index, probably also done at runtime. Or something like that.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: ActiveState Launches Python Package Manager Index (PyPM Index) LXer Syndicated Linux News 0 11-03-2010 03:40 PM
redirect index.php to index.html bittus Linux - Software 6 12-14-2009 10:04 PM
How to change the default webpage configuration from index.php to index.htm, etc meema Linux - Server 4 08-22-2008 09:06 PM
apache index.html doesn't show up but index.php do zoffmann Linux - Server 5 01-28-2008 03:53 PM
Where to put index.php (or index.html) on Slackware 11.0 moonguide Slackware 3 05-08-2007 06:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration