Need help with memory address

Hi guys,

What data type is used to hold the memory address stored in the pointer variable?
Websearch with "data type of memory address" does yield answers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*----------------------------*
Concept of STACK Memory-CUSTOM*
------------------------------*/


#include <iostream>

using namespace std;

int main ()
{
    long int ad[5] ;   //Array to hold address.
    long int x[5] = {0};     //Array of 5 int variables. Consider it a STACK.
    long int *p;             //Declaration of pointer to an int value.
    p = x;              //Initialized the pointer to the first element of
                        //array or STACK.
    ad[0] = p;         //Initialized the first element to the first address
                        //of x array i.e. x[0].
    int val = 0;

/*-----------------Loop to fill the STACK----------------*/

    for (int i = 0; i < 5; i++){
    cout << "Enter Value: ";
    cin >> *p;          //Overwrite the contents of the address pointed by 'p'.
    cout << i <<". You Entered: " << *p <<"  " << p << "\n";    //With the input value also display the
                                                                //the memory address of it.
    ad[i] = p;         //Address will be stored in add array.
    p++;                //Post increment the memory address.
    }
/*-------------Loop for Resetting the pointer to first array or STACK element-----------*/
        for (int i = 0; i < 5; i++)
       p--;             //Decrement to the previous address.

/*----------------------Data Fetch---------------------*/
    cout << "Enter Ser. No. to Fetch: ";
    cin >> val;
    p = ad[val];
    cout << *p;

    return 0;
}


The web search says LONG INT, but still having the error.
Last edited on
What data type is used to hold the memory address stored in the pointer variable?

Depends what kind of pointer it is. If it's an int-pointer, then the data type is int-pointer. If it's a double-pointer, then the data type is double-pointer. And so on.

long int ad[5] ; //Array to hold address.
If you want to hold addresses, you need an array of pointers.

What are you actually trying to do? What are your needs, such that simply using pointers isn't an option?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;

int main()
{
	int* ad[5];   //Array to hold address.
	int x[5] = { 0 };     //Array of 5 int variables. Consider it a STACK.
	int *p;             //Declaration of pointer to an int value.
	p = x;              //Initialized the pointer to the first element of
						//array or STACK.
	ad[0] = p;         //Initialized the first element to the first address
						//of x array i.e. x[0].
	int val = 0;

	/*-----------------Loop to fill the STACK----------------*/

	for (int i = 0; i < 5; i++) {
		cout << "Enter Value: ";
		cin >> *p;          //Overwrite the contents of the address pointed by 'p'.
		cout << i << ". You Entered: " << *p << "  " << p << "\n";    //With the input value also display the
											//the memory address of it.
		ad[i] = p;         //Address will be stored in add array.
		p++;                //Post increment the memory address.
	}
	/*-------------Loop for Resetting the pointer to first array or STACK element-----------*/
	for (int i = 0; i < 5; i++)
		p--;             //Decrement to the previous address.

 /*----------------------Data Fetch---------------------*/
	cout << "Enter Ser. No. to Fetch: ";
	cin >> val;
	p = ad[val];
	cout << *p;

	return 0;
}
Last edited on
@Repeater,

Maybe what @zarrar12 is looking for is std::intptr_t, or std::uintptr_t ?

Defined as the size of a pointer, but typed as an integer (signed or unsigned)


Certainly possible, but based on the sample code zarrar12 showed, I reckon zarrar12 is looking for pointers :)
A pointer to a pointer holds the memory address of a pointer.
Not sure what any of that has to do with a stack container.

This looks more like a study about pointers than about stacks, but then it is about a stack.

What data type is used to hold the memory address stored in the pointer variable?

Pointers, like ints and chars, are integer values.

However, C and C++ has a strict type system where the treatment of these integer values differs based on the type.

This is because the hardware itself understands the type of thing to be integral to its proper value, and there is not necessarily a 1:1 correlation between a pointer value and the counting numbers.

For example, on some hardware, the nullptr is not a zero value. C and C++ let you think it is, and treat it as such, but this is an abstraction to make handling explicitly invalid pointer values easy.

Likewise, on some (ancient) hardware, an int* type is different than a char* type in actual size and bit patterns.

The popularity of the x86 family of processors has made a lot of people lazy about pointer aliasing, to the point where a lot of MS code even treats DWORD as an acceptable opaque type for internal OS pointer values.


The actual structure of a pointer is implementation dependent, and this issue exists even today: a pointer to a class function is not the same as a standard void*, and you cannot safely perform type-punning with them.

tl;dr: Always use the proper type to manage pointers, and don’t play around with type-punning.
Pointers, like ints and chars, are integer values.

I think more precise terminology would be addresses are binary values (pointer is more than the address it holds and a sequence of bits is only "integer value" if interpreted as such - I could just as well call it a "char array")
Topic archived. No new replies allowed.