[SOLVED] C++: How to Know I'm Storing and Retrieving from the Same index
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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;
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;
}
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.