# Pseudopointers

Show 50 post(s) from this thread on one page
Page 1 of 4 1234 Last
• March 19th, 2013, 04:15 AM
eightyfive
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.

Exampling (is exampling a word? exemplifying)
The Difference Between a Static Method & Class Method | eHow.com
Data Structures

But no one talks about a formal definition, therefore I see a reason to call
these variables like this XD.

So the problem was to enter 10 numbers (integers), get them listed and get
the program to tell us how many repetitions were there, more or less XD

so we're going to use 10 integer variables, and they are to be stored
contiguously. let's take into account that 1 integer are 4 bytes, having
all this we're going to the code.

main.cpp
Code:

```// // By 85 // elhacker.net // etalking.com.ar // boyscout_arg@hotmail.com // 2013 // ///////////////////////////////////////////////////////////////////////////////////////////////// //#include<windows.h> #include<stdio.h> #include<stdlib.h> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void Test1(); void Test2(); void Test3(); ///////////////////////////////////////////////////////////////////////////////////////////////// int main(){         system("cls");         printf("Test1:\n");         Test1();         system("pause");         system("cls");         printf("Test2:\n");         Test2();         system("pause");         system("cls");         printf("Test3:\n");         Test3();         system("pause");         return 0; } /////////////////////////////////////////////////////////////////////////////////////////////////```
pseudopointers1.cpp
Code:

``` // // By 85 // elhacker.net // etalking.com.ar // boyscout_arg@hotmail.com // 2013 // ///////////////////////////////////////////////////////////////////////////////////////////////// #include<stdio.h> #include<stdlib.h> #include<string.h> #include <typeinfo> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // No tenemos garantía de que sean almacenadas contiguamente! static int entrada1=0;//entrada1+0x0 static int entrada2=0;//entrada1+0x4 static int entrada3=0;//entrada1+0x8 static int entrada4=0;//entrada1+0xC static int entrada5=0;//entrada1+0x10 static int entrada6=0;//entrada1+0x14 static int entrada7=0;//entrada1+0x18 static int entrada8=0;//entrada1+0x1C static int entrada9=0;//entrada1+0x20 static int entrada10=0;//entrada1+0x24 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void Test1(){ /*         ////////////////////////////////////         // Basic check (Designed for 10 variables no more)         //int entrada10=0;// Hacerlo cagar XD         printf("Estas direcciones deben estar en forma contigua!\n");         printf("0x%X\n",&entrada1);         printf("0x%X\n",&entrada2);         printf("0x%X\n",&entrada3);         printf("0x%X\n",&entrada4);         printf("0x%X\n",&entrada5);         printf("0x%X\n",&entrada6);         printf("0x%X\n",&entrada7);         printf("0x%X\n",&entrada8);         printf("0x%X\n",&entrada9);         printf("0x%X\n",&entrada10);         int flag_exit=0;         if( ((&entrada2)-((&entrada1)+0x1)) !=0){flag_exit=1; goto error;}         if( ((&entrada3)-((&entrada2)+0x1)) !=0){flag_exit=2; goto error;}         if( ((&entrada4)-((&entrada3)+0x1)) !=0){flag_exit=3; goto error;}         if( ((&entrada5)-((&entrada4)+0x1)) !=0){flag_exit=4; goto error;}         if( ((&entrada6)-((&entrada5)+0x1)) !=0){flag_exit=5; goto error;}         if( ((&entrada7)-((&entrada6)+0x1)) !=0){flag_exit=6; goto error;}         if( ((&entrada8)-((&entrada7)+0x1)) !=0){flag_exit=7; goto error;}         if( ((&entrada9)-((&entrada8)+0x1)) !=0){flag_exit=8; goto error;}         if( ((&entrada10)-((&entrada9)+0x1)) !=0){flag_exit=9; goto error;} error:         if(flag_exit>0){                 printf("It seems they are not stored contiguously. Error %d\n", flag_exit);                 system("pause");                 return;         }*/         //////////////////////////////////// #define XPOINTERTYPE unsigned long #define XVARTYPE int         //if(sizeof(XVARTYPE) != sizeof(entrada1)) return;         unsigned char OFFS = (unsigned char)sizeof(XVARTYPE);// Up to 255         XPOINTERTYPE entradaX=(XPOINTERTYPE)&entrada1;// Una especie de puntero THIS         XPOINTERTYPE inicio_bloque=(XPOINTERTYPE)&entrada1;         XPOINTERTYPE fin_bloque=(XPOINTERTYPE)&entrada10;         int rep = 0;         const int MAX_NUMB =10;         char* format;         if(!strcmp(typeid(XVARTYPE).name(), "int")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "long")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "long int")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "long long")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned long")) format="%X";         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned int")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned short")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "float")) format="%f";         else if(!strcmp(typeid(XVARTYPE).name(), "double")) format="%f";         else if(!strcmp(typeid(XVARTYPE).name(), "double long")) format="%f";         else if(!strcmp(typeid(XVARTYPE).name(), "long double")) format="%f";         else return;         for(int i=0;i<MAX_NUMB;i++)         {                 printf("Introduzca un número\n");                 scanf(format,(XPOINTERTYPE*)entradaX);                 rep=0;                 XPOINTERTYPE apuntador1=(XPOINTERTYPE)&entradaX;//Doble puntero                                 for(XPOINTERTYPE j=inicio_bloque;j<(inicio_bloque+(OFFS*i));j+=OFFS)                 {                         XPOINTERTYPE apuntador2=(XPOINTERTYPE)&j;//Doble puntero                         if(**(XVARTYPE**)apuntador1 == **(XVARTYPE**)apuntador2){                                 rep++;                         }                 }                 if(rep)                 {                         char* info;                         if(!strcmp(typeid(XVARTYPE).name(), "int")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "long")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "long int")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "long long")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned long")) info="%X had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned int")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned short")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "float")) info="%f had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "double")) info="%f had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "double long")) info="%f had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "long double")) info="%f had %d repetitions\n";                         printf(info,**(XVARTYPE**)apuntador1,rep);                         //while(getchar()!='\n');                         system("pause");                 }                 entradaX+=OFFS;         }         entradaX=(XPOINTERTYPE)&entrada1;//Arreglar el pseudopuntero THIS         for(int k=1;k<=MAX_NUMB;k++)         {                 printf(format,*(XVARTYPE*)entradaX);                         entradaX+=OFFS;         }         printf("\n");         entradaX=(XPOINTERTYPE)&entrada1;//Arreglar el pseudopuntero THIS } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////```
pseudopointers2.cpp
Code:

```// // By 85 // elhacker.net // etalking.com.ar // boyscout_arg@hotmail.com // 2013 // ///////////////////////////////////////////////////////////////////////////////////////////////// #include<stdio.h> #include<stdlib.h> #include<string.h> #include <typeinfo> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Vamos a usar un array para asegurarnos de que los 10 enteros // sean almacenados de forma contigua. static int entradas[10] = {0}; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void Test2(){ #define XPOINTERTYPE unsigned long #define XVARTYPE int         //if(sizeof(XVARTYPE) != sizeof(entradas[0])) return;         unsigned char OFFS = (unsigned char)sizeof(XVARTYPE);// Up to 255         XPOINTERTYPE entradaX=(XPOINTERTYPE)&entradas[0];// Una especie de puntero THIS         XPOINTERTYPE inicio_bloque=(XPOINTERTYPE)&entradas[0];         XPOINTERTYPE fin_bloque=(XPOINTERTYPE)&entradas[9];         int rep = 0;         const int MAX_NUMB =10;         char* format;         if(!strcmp(typeid(XVARTYPE).name(), "int")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "long")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "long int")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "long long")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned long")) format="%X";         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned int")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned short")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "float")) format="%f";         else if(!strcmp(typeid(XVARTYPE).name(), "double")) format="%f";         else if(!strcmp(typeid(XVARTYPE).name(), "double long")) format="%f";         else if(!strcmp(typeid(XVARTYPE).name(), "long double")) format="%f";         else return;         for(int i=0;i<MAX_NUMB;i++)         {                 printf("Introduzca un número\n");                 scanf(format,(XPOINTERTYPE*)entradaX);                 rep=0;                 XPOINTERTYPE apuntador1=(XPOINTERTYPE)&entradaX;//Doble puntero                                 for(XPOINTERTYPE j=inicio_bloque;j<(inicio_bloque+(OFFS*i));j+=OFFS)                 {                         XPOINTERTYPE apuntador2=(XPOINTERTYPE)&j;//Doble puntero                         if(**(XVARTYPE**)apuntador1 == **(XVARTYPE**)apuntador2){                                 rep++;                         }                 }                 if(rep)                 {                         char* info;                         if(!strcmp(typeid(XVARTYPE).name(), "int")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "long")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "long int")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "long long")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned long")) info="%X had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned int")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned short")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "float")) info="%f had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "double")) info="%f had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "double long")) info="%f had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "long double")) info="%f had %d repetitions\n";                         printf(info,**(XVARTYPE**)apuntador1,rep);                         //while(getchar()!='\n');                         system("pause");                 }                 entradaX+=OFFS;         }         entradaX=(XPOINTERTYPE)&entradas[0];//Arreglar el pseudopuntero THIS         for(int k=1;k<=MAX_NUMB;k++)         {                 printf(format,*(XVARTYPE*)entradaX);                 entradaX+=OFFS;         }         printf("\n");         entradaX=(XPOINTERTYPE)&entradas[0];//Arreglar el pseudopuntero THIS } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////```
pseudopointers3.cpp
Code:

```// // By 85 // elhacker.net // etalking.com.ar // boyscout_arg@hotmail.com // 2013 // ///////////////////////////////////////////////////////////////////////////////////////////////// #include<stdio.h> #include<stdlib.h> #include<string.h> #include <typeinfo> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Vamos a usar una estructura para asegurarnos de que los 10 enteros // sean almacenados de forma contigua. struct Entradas{         int a,b,c,d,e,f,g,h,i,j; }; static struct Entradas entradas; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void Test3(){ #define XPOINTERTYPE unsigned long #define XVARTYPE int         //if(sizeof(XVARTYPE) != sizeof(entradas.a)) return;         unsigned char OFFS = (unsigned char)sizeof(XVARTYPE);// Up to 255         XPOINTERTYPE entradaX=(XPOINTERTYPE)&entradas.a;// Una especie de puntero THIS         XPOINTERTYPE inicio_bloque=(XPOINTERTYPE)&entradas.a;         XPOINTERTYPE fin_bloque=(XPOINTERTYPE)&entradas.j;         int rep = 0;         const int MAX_NUMB =10;         char* format;         if(!strcmp(typeid(XVARTYPE).name(), "int")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "long")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "long int")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "long long")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned long")) format="%X";         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned int")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned short")) format="%d";         else if(!strcmp(typeid(XVARTYPE).name(), "float")) format="%f";         else if(!strcmp(typeid(XVARTYPE).name(), "double")) format="%f";         else if(!strcmp(typeid(XVARTYPE).name(), "double long")) format="%f";         else if(!strcmp(typeid(XVARTYPE).name(), "long double")) format="%f";         else return;         for(int i=0;i<MAX_NUMB;i++)         {                 printf("Introduzca un número\n");                 scanf(format,(XPOINTERTYPE*)entradaX);                 rep=0;                 XPOINTERTYPE apuntador1=(XPOINTERTYPE)&entradaX;//Doble puntero                                 for(XPOINTERTYPE j=inicio_bloque;j<(inicio_bloque+(OFFS*i));j+=OFFS)                 {                         XPOINTERTYPE apuntador2=(XPOINTERTYPE)&j;//Doble puntero                         if(**(XVARTYPE**)apuntador1 == **(XVARTYPE**)apuntador2){                                 rep++;                         }                 }                 if(rep)                 {                         char* info;                         if(!strcmp(typeid(XVARTYPE).name(), "int")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "long")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "long int")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "long long")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned long")) info="%X had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned int")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "unsigned short")) info="%d had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "float")) info="%f had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "double")) info="%f had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "double long")) info="%f had %d repetitions\n";                         else if(!strcmp(typeid(XVARTYPE).name(), "long double")) info="%f had %d repetitions\n";                         printf(info,**(XVARTYPE**)apuntador1,rep);                         //while(getchar()!='\n');                         system("pause");                 }                 entradaX+=OFFS;         }         entradaX=(XPOINTERTYPE)&entradas.a;//Arreglar el pseudopuntero THIS         for(int k=1;k<=MAX_NUMB;k++)         {                 printf(format,*(XVARTYPE*)entradaX);                         entradaX+=OFFS;         }         printf("\n");         entradaX=(XPOINTERTYPE)&entradas.a;//Arreglar el pseudopuntero THIS } ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////```
-------------------------------------------------------------------
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.
Code:

```for(int i=0;i<MAX_NUMB;i++) {         printf("Introduzca un numero\n");         scanf("%d",(int*)entradaX);         ... }```
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.
Code:

```... int apuntador1=(int)&entradaX;//Doble puntero for(int j=inicio_bloque;j<(inicio_bloque+(0x4*i));j+=0x4) {         int apuntador2=(int)&j;//Doble puntero         if(**(int**)apuntador1 == **(int**)apuntador2){                 c++;         } } ...```
The variable 'entradaX' or "pseudo THIS pointer" it's incremented, so it
points to the last number or the current one (it depends).
This is done by incrementing it in the FOR loop
Code:

`entradaX+=0x4;`
point now is, that this variable gets reimplemented, so it needs to be
arranged its address in order it always points to the first integer variable,
everytime it will be reimplemented again.
Code:

`entradaX=(int)&entrada1;`
All that has certain analogy to the THIS pointer, but I want to say
it is not the THIS pointer, not any close to it. It was just a mere
comparision, that's why I said "pseudo"

To list it is the same, it is passed the content in the pseudopointer to printf
Code:

```for(int i=1;i<=MAX_NUMB;i++) {         printf("%d\n",*(int*)entradaX);                 entradaX+=0x4; }```
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.

Here's the link to the code.
http://www.mediafire.com/?q3l5yqplduxpfi6
• March 19th, 2013, 05:08 AM
VictorN
Re: Pseudopointers
Well, what was wrong for you to use an array? :confused:
• March 19th, 2013, 11:07 AM
Paul McKenzie
Re: Pseudopointers
Quote:

Originally Posted by eightyfive
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!).

I hope you told the beginner to use arrays, and not what you are posting now.
Quote:

But no one talks about a formal definition,
That is because no one would want to waste a lot of time writing code like this, when they simply use arrays.
Quote:

so we're going to use 10 integer variables, and they are to be stored
contiguously.
You mean like this:
Code:

```int main() {   int numArray[10]; }```
That one line of code does everything you just described.

Second, this:
Code:

```int main() {         static int entrada1=0;//entrada1+0x0         static int entrada2=0;//entrada1+0x4         static int entrada3=0;//entrada1+0x8         static int entrada4=0;//entrada1+0xC         static int entrada5=0;//entrada1+0x10         static int entrada6=0;//entrada1+0x14         static int entrada7=0;//entrada1+0x18         static int entrada8=0;//entrada1+0x1C         static int entrada9=0;//entrada1+0x20         static int entrada10=0;//entrada1+0x24         int c = 0;         const int MAX_NUMB =10;```
That entire block of code is this:
Code:

```int main() {     const int MAX_NUMB =10;     static int entrada[MAX_NUMB] = {0};```
Last, the code you posted will not work if sizeof(int*) != sizeof(int).
Code:

```int entradaX=(int)&entrada1;//PSEUDOPUNTERO THIS XD int inicio_bloque=(int)&entrada1; int fin_bloque=(int)&entrada10;```
None of this is valid if pointers are, for example 64-bit values and ints are 32-bit values. You will be truncating the pointer value when attempting to assign it to an int.
Quote:

I know some beginners will appreciate it XD.
Umm. No.

Regards,

Paul McKenzie
• March 19th, 2013, 11:36 AM
GCDEF
Re: Pseudopointers
Part of being a good programmer means realizing when you're taking a simple problem and adding unnecessary complexity to it. I would imagine the beginner you were helping is scarred for life.
• March 19th, 2013, 11:37 AM
2kaud
Re: Pseudopointers
Quote:

For all that garbage
Quite right!:thumbd:
• March 21st, 2013, 07:53 AM
eightyfive
Re: Pseudopointers
Sorry guys but I have to say your comments are very poor of quality,
because in first place, the beginner has nothing to do with all this, maybe Paul came with an acceptable appreciation..
Quote:

...
well, that is not the point because the point is the idea I got from that XD.
..
Second, I can only consider this
Quote:

Originally Posted by Paul McKenzie
...
[/code]
Last, the code you posted will not work if sizeof(int*) != sizeof(int).
Code:

```int entradaX=(int)&entrada1;//PSEUDOPUNTERO THIS XD int inicio_bloque=(int)&entrada1; int fin_bloque=(int)&entrada10;```
None of this is valid if pointers are, for example 64-bit values and ints are 32-bit values. You will be truncating the pointer value when attempting to assign it to an int.
Umm. No.

Regards,

Paul McKenzie

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?

and why do you say again and again, something like this:
Quote:

int main()
{
const int MAX_NUMB =10;
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
• March 21st, 2013, 08:39 AM
laserlight
Re: Pseudopointers
Quote:

Originally Posted by eightyfive
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?

I don't understand what you are trying to get here. Do you even understand the point that Paul McKenzie was trying to make, or are you merely fixated on your "solution" and closed to good practice?

Quote:

Originally Posted by eightyfive
and why do you say again and again, something like this:

Because an array (or some other suitable container) would be a normal solution to the problem. Good solutions are worth repeating when they are not in the limelight.

Quote:

Originally Posted by eightyfive
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

Well, now that we know that, we know that it is his teacher who scarred him for life. You were just an accessory to the crime :)
• March 21st, 2013, 08:58 AM
eightyfive
Re: Pseudopointers
Quote:

Originally Posted by laserlight
I don't understand what you are trying to get here. Do you even understand the point that Paul McKenzie was trying to make, or are you merely fixated on your "solution" and closed to good practice?

Because an array (or some other suitable container) would be a normal solution to the problem. Good solutions are worth repeating when they are not in the limelight.

Well, now that we know that, we know that it is his teacher who scarred him for life. You were just an accessory to the crime :)

wrong once again, because he never got scared at all. he solved his own problem by his own, and he never saw this code,
so he is safe and sound :D
• March 21st, 2013, 10:59 AM
Paul McKenzie
Re: Pseudopointers
Quote:

Originally Posted by eightyfive
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.
Quote:

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.

Regards,

Paul McKenzie
• March 21st, 2013, 11:46 AM
OReubens
Re: Pseudopointers
Quote:

Originally Posted by eightyfive
His problem involved the fact that his teacher didn't allow him to use arrays, pointers, dynamic allocation or even structs.

If you have a programming task.
And that task requires a "list" or "series" of numbers
And you are not allowed to use arrays, containers, pointers, structs...

Then that teacher is doign a TERRIBLE job at teaching and giving his students tasks that have ZERO real-world value. Not even as an educational value.

Quote:

...
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).
• March 21st, 2013, 02:03 PM
eightyfive
Re: Pseudopointers
Reubens, think now in this program

Code:

``` REMOVED BECAUSE IT IS NOW IN THE FIRST POST.```
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.
• March 21st, 2013, 03:43 PM
GCDEF
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.
• March 21st, 2013, 04:20 PM
Paul McKenzie
Re: Pseudopointers
Quote:

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.
Quote:

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.

Regards,

Paul McKenzie
• March 22nd, 2013, 12:08 AM
eightyfive
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
• March 22nd, 2013, 01:55 AM
Paul McKenzie
Re: Pseudopointers
Quote:

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:
Code:

```/*        // Logs de depuramiento (IGNORE! XD)         printf("0x%X\n",&entrada1);         printf("0x%X\n",&entrada2);         printf("0x%X\n",&entrada3);         printf("0x%X\n",&entrada4);         printf("0x%X\n",&entrada5);         printf("entradaX 0x%X\n",&entradaX);         printf("entradaX 0x%X\n",(int*)entradaX);         printf("*entradaX 0x%X\n",*(int*)entradaX);         system("pause");*/```
Quote:

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:
Quote:

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?
Quote:

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:
Code:

```#include <iostream> int main() {   static int x;   static int y;   int *z = &x + 1;  // What is the magic number to add?    if ( z ==  &y )       std::cout << " OK";  // how will we get here?   else       std::cout << "Not OK"; }```
Regards,

Paul McKenzie
Show 50 post(s) from this thread on one page
Page 1 of 4 1234 Last