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.
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.
//
// 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
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.
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.
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.
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.
I know some beginners will appreciate it XD.
Umm. No.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; March 19th, 2013 at 11:09 AM.
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. 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/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
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..
...
well, that is not the point because the point is the idea I got from that XD.
..
Second, I can only consider this
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:
int main()
{
const int MAX_NUMB =10;
static int entrada[MAX_NUMB] = {0};
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
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.
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.
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
...
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).
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.
Last edited by eightyfive; March 23rd, 2013 at 05:32 AM.
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.
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.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; March 21st, 2013 at 05:11 PM.
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
Last edited by eightyfive; March 22nd, 2013 at 12:13 AM.
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?
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.
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
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar
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
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?
Since you're suggesting this the burden of proving its correctness falls upon you. You'll have to point out the relevant sections in the C++ standard. Stretching a language isn't wrong but you need to formally ensure it didn't break. Testing is not enougth.
And not everything that's allowed is good programming practice. It may lead to brittle and error-prone code. This is especially true with C++. It's a very potent language that must be handled with care. It's easy to shoot oneself in the foot. In fact Java came about as a direct response to this problem.
I'd say Pseudopointer offers an interesting combination of meaningless functionality with naive and ignorant implementation. It's definately a strong candidate for the C++ Hall of Infamy.
Last edited by nuzzle; March 22nd, 2013 at 03:34 AM.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.