If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Pseudopointers

I recently helped a beginner with solving some situation. he needed
to load 10 integer numbers, getting to list them and to tell how many of them
resulted in a repetition (and how many times!).

well, that is not the point because the point is the idea I got from that XD.

I decided to solve that using pointer arithmetics and using 10 integer variables.
the reason will tell you later.

A variable uses memory space and it has a memory address too.
we all know that, and also we know a variable can save a value.
that value could be a memory address for example, just like pointers do.

For that, in this topic I will show how to make use of variables to work
as pointers, but obviously not using pointer type variables at all.

Honestly, there's no reason at all to make something like this, moreover,
you can make use of pointers, just the way the language makes it possible for
us. But it also allows us to do in the other way so let's continue.

Neither I'm sure whether the term "pseudopointer" is correct or not,
i was searching by GOOGLE so I can know if the expression was used before.
it seems it was used to mean something oriented to OBJECTS.

-------------------------------------------------------------------
EARLIER EXPLANATION
---------------------------
Considering the 10 variables like a memory block of 10*4BYTES, we're
going to store the blockstart memory address and the blockend one.

Code:

int inicio_bloque=(int)&entrada1;
int fin_bloque=(int)&entrada10;

This variable stores the first-variable memory address which stores the
first number. This variable is more or less like a THIS pointer, to call it
something, because it gets displaced along of the block of 10 variables,
it is, that it goes changing its value to the address of any of the other
10 integer variables we use to store the numbers.
For all that garbage, I have no drama in calling it a "THIS pseudopointer" XD.

Code:

int entradaX=(int)&entrada1;

To store the 10 numbers we do 10 iterations, but scanf saves the input in the
address that is contained in the variable called 'entradaX'. Note the typecasting,
so scanf will know it is about a pointer.

quoting this part, is where it is made the comparision of each input against
all the numbers already were inputted till the moment.
Notice that it is used a local variable called 'apuntador1', that in fact is
doing as a double pointer or a pointer to pointer, because it holds the address
of another variable, which at the same time holds the value of another variable again.
For that, to access the final value (a number), it is implemented the double pointer
notation.
In what is related to the FOR loop, as we said it were 10 integers 4 bytes each one,
so the loop will count from-to 4bytes till it does 10 iterations.

If you think this is a pile of junk, it's alright. I'm not discovering America,
but see that this is not the way to make something like this, you normally use
pointer type variables, so I thought it was alright to show how to make it without.
I know some beginners will appreciate it XD.

Re: Pseudopointers

For all that garbage

Quite right!

All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/

Re: Pseudopointers

but, I think you are very closed to the INT type, consider it could be done using another data type, just think about it. why so closed to the INT type?

When you post code, it will get commented on.

The code you posted will not work on a system that has 64-bit pointers and 32-bit integers. Practically all Windows C++ compilers, including VC++, operate this way for 64-bit programs. So your code when compiled as a 64-bit executable with one of the most popular compilers used around the world would not work, or at the very least, not work reliably due to truncation.

his problem involved the fact that his teacher didn't allow him to use arrays, pointers, dynamic allocation or even structs.
but you didn't know that part, so it's okay

If that's the case, then there is no guarantee any of your code works, because you rely on an implementation detail, and that is that variables when declared "together" are contiguous.

The only way you can guarantee contiguousness is to write code that guarantees it. An array, dynamically allocated memory, etc. guarantee that what you have is a contiguous block of memory. Declaring variables one after the other does not guarantee this.

NO. There is absolutely NO guarantee in C++ that these numbers will be sequentially in memory, with exactly 4 bytes difference between their respective addresses. C++ doesn't even guarantee that local variables in a function are in a formal stack.

I know of at least 1 cpu that doesn't have an expliciet hardware stack support (no stack pointer or CPU instructions to push/pop/call/return) and it's most used C++ compiler maintains all local data itself in a linked list. And on that particular compiler, the above int's would be separated by 8 bytes, and be in reverse order (entrada1 having an address 8 higher than entrada 2).

If you want 2 or more variable definitions to have any sort of guaranteed relation between them, they at least need to be either in a struct/class or in an array. In a struct you may need additional compiler specific support such as explicitely defining structure packing to get the effect you need.
Also note that an "int" is not necessarily 4 bytes. the only guarantee is "at least 16 bits" and it can be either 1 complement or 2 complement mode. (so the guaranteed range is -32767 to +32767 (-32768 is not guaranteed for an int).
An int could be 2, 4, 8 bytes (And it could even be 5 bytes, although rare, it does exist).

Re: Pseudopointers

which is basically the same as the first, tell me how would you get it to work when it is compiled as a x64 EXE file.

since you are a bit obsesive with the INT type, I let you decide the type now. tell me how would you modify it?

and don't tell me that you won't init a static variable (INT in this case) with any value or maybe with ZERO because you would be SO WRONG,

please apply the changes, or maybe you would like to use sizeof operator to get size of an integer in both systems (32 and 64 -bits),
you just show me, or at least don't tell me anymore to use an array or a struct. The kid is probably recently viewing basic pointer usage, as he says his teacher didn't allow the use of arrays and structs, and so.

Re: Pseudopointers

But you're essentially trying to duplicate arrays by defining 10 int variables and assuming they're contiguous, then manipulating pointers to access them. As mentioned, there's no guarantee the ints are contiguous, therefore no guarantee your approach will work.

Without arrays or other containers, and with a known and manageable number of ints, a brute force kind of solution would be the bet approach. Set up 10 variables to hold the values entered and set up 10 counters. Each time a variable is entered, see if you've seen it before and increment the counter if you have.

Re: Pseudopointers

since you are a bit obsesive with the INT type, I let you decide the type now. tell me how would you modify it?

Why would anyone want to modify this code? I know you may be proud of the code (even proud enough to invent a name, "Pseudopointers"), but I bet it was never really reviewed by experienced C++ programmers. Is this the first time you've been told about the wrong things about the code?

First, declaring variables one right after the other does not guarantee they are contiguous in memory. So right away, your code is unmodifiable because it makes wrong assumptions. Just because one variable is declared after the other doesn't mean that the variables are "next to" each other in memory.

please apply the changes, or maybe you would like to use sizeof operator to get size of an integer in both systems (32 and 64 -bits),you just show me, or at least don't tell me anymore to use an array or a struct.

What we are stating to you is why your code is invalid. An array or struct must be used, as declaring variables next to each other doesn't make them contiguous in memory. I don't know how more simply it can be stated.

Re: Pseudopointers

yeh stop saying that an array must be used or I will get crazy of hearing you, I told you the problem doesn't involve the use of an array, struct or whatelseever.
Also , you seem to know about compiler internals, memory mapping, etc so tell me why wouldn't the 10 variables be stored contiguously? because you're so sure they will be not. you was told of it? or is your mere assumption?
regarding the code, just replace XTYPE with the datatype you want, arrange constant called OFFS (make it variable) using sizeof datatype or whatever you want to use, and get that to work for x64.
so my code will be valid (or just use a struct of 10 integers, I don't care, since isn't the point).
see later

Re: Pseudopointers

Originally Posted by eightyfive

yeh stop saying that an array must be used or I will get crazy of hearing you, I told you the problem doesn't involve the use of an array, struct or whatelseever.

In C++, you can't assume that variables declared together are contiguous in memory. It's just that simple, but for some odd reason you do not wish to accept that fact. The only way to guarantee that variables are contiguous is to use an aggregate type.

Look at the code you commented out. You even experimented with what a certain compiler did, and wrote code based on a compiler implementation by testing what printf() gave you. That is not the way you write C++ programs, and that is to use compiler internals to come up with a general solution. Here is the commented out code:

Also, you seem to know about compiler internals, memory mapping, etc so tell me why wouldn't the 10 variables be stored contiguously? because you're so sure they will be not. you was told of it? or is your mere assumption?

Did you read post #10 by OReubens, or do you think he is lying? He stated he knows compilers where your assumption is wrong.

Second, nowhere in the ANSI/ISO C++ specification does it state how variables are stored in memory. It has nothing to do with knowing compiler internals -- it's about knowing the rules of C++ and what is stated by ANSI/ISO. You cannot declare variables independent of each other and assume what the compiler will do with them in terms of how they will be placed in memory. I will state it again:

If that's the case, then there is no guarantee any of your code works, because you rely on an implementation detail, and that is that variables when declared "together" are contiguous.

Do you know the difference between "implementation-detail" and a guarantee by the language as to what is to occur?

regarding the code, just replace XTYPE with the datatype you want, arrange constant called OFFS (make it variable) using sizeof datatype or whatever you want to use, and get that to work for x64.so my code will be valid (or just use a struct of 10 integers, I don't care, since isn't the point).
see later

You posted code in post #1, and you have persons here commenting on what is wrong with it. No one is interested in "modifying" code for the sake of it. It's your code, shouldn't you be changing it, given what has been stated here? If not, that's your choice -- all we're telling you is what we see and where it uses implementation-defined behaviour.

This must be the first place you posted your code, because I can tell you now that another board will tell you the same things we're telling you.

Look, I tell you what: Take this and post it on another board by filling in the number to add to &x so that "OK" gets printed, and come back here and give us a link to the discussion: