4

I am not very familiar with pointers, since i mostly do java, and java has no pointers and now i am learning C++. In a C++ tutorial, in order to know the size of memory occupied by a variable, the tutor used size of on a pointer to the variable, i.e,

    int v = 23;
    int *p = &v;

    cout << sizeof(p) << endl;

This got me confused because in my first year i was taught in C programming that i will need to do sizeof on the variable itself. So i first concluded that they meant the same. But i when i tried on my computer i had different results. I have the following code...

    #include <iostream>

    int main()
    {
            int *ptr = new int;
            int n = 23;

            ptr = &n;

            std::cout << sizeof(n) << std::endl;
            std::cout << sizeof(ptr) << std::endl;

            return 0;
    }

When i run the above code, i get output of 4, and 8. But my friend compiled and executed the same code on his machine and he had output of 4 and 4. I don't know why this is happening and why the tutor used sizeof on a pointer to the variable instead of on the variable itself, since he wanted to know the amount of memory occupied by that variable. I know variables in C/C++ have different memory capacities because of different architectures, atleast that is what i was taught in C. That an int in 64bit machine has a different size from that on a 32bit machine. But i though my results had to be at least consistent i.e 8 and 8, or 4 and 4. I am using a 64bit architecture and a 64bit OS, my friend is using a 64bit architecture with a 32bit OS.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
ivange
  • 434
  • 1
  • 3
  • 12
  • *"java has no pointers"* - Nothing could be further from the truth. Java has pointers everywhere, much more than you'll ever find in modern C++, including all those horribly tedious null checks and violations of the fail-fast principle. Java just tends to call its pointers "references", except of `NullPointerException`. Java does not have C++ references. – Christian Hackl Dec 16 '15 at 14:30
  • I understand what you mean by saying java has pointers but just calls them references. But saying things like that might be misleading to some C/C++ programmers. They might start thinking of pointer arithmetic, deference etc. I might be wrong but i know java does not do any of those. But what do I even know :-)http://stackoverflow.com/questions/2629357/does-java-have-pointers – ivange Dec 18 '15 at 02:46

5 Answers5

10

In your code,

 cout << sizeof(p) << endl;

gives you the size of the variable p, which is of type int *.

This is not the same as

cout << sizeof(*p) << endl;

or

cout << sizeof(int) << endl;

which will give you the size occupied by an int variable.

Having said that, just to clarify, to get to know the size occupied by a variable, you need to use the sizeof operator on that variable, not to a pointer-to-that variable. ( What you learnt in first year is correct ).

Note, size of a pointer is dependent on the architecture, so it can vary. In some architecture, size of a pointer can be 32 bits (sizeof will return 4), in some others it can be 64 bits (sizeof will return 8).

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
3

You have to use sizeof to the value itself to know how much memory is occupied by the variable. I think the tutor did wrong.

int v = 23;
int a[5] = {0, 1, 2, 3, 4};
int *p = &v;

cout << sizeof(p) << endl; // print the size of int*
cout << sizeof(*p) << endl; // print the size of int
cout << sizeof(v) << endl; // print the size of int

p = a;
cout << sizeof(p) << endl; // print the size of int*
cout << sizeof(*p) << endl; // print the size of int
cout << sizeof(a) << endl; // print the size of int[5]
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
1

sizeof(ptr) gives the size of pointer ptr and its size is implementation defined . Same is true for sizeof(int). That's the reason you and your friend getting the different results.
Other possibilities that you may get the output as 4 4 and 8 8.

haccks
  • 104,019
  • 25
  • 176
  • 264
0

sizeof will give you the number of bytes that are needed to represent object of certain type.In your line std::cout << sizeof(n) << std::endl; n is int so this will give you what is the size of int valiable.
The line std::cout << sizeof(ptr) << std::endl; ptr is of type pointer to an integer so this will give you the size of int*. Their values are not guaranteed to be the same and will both vary independently on different architectures. The c++ standard doesn't say what the sizes of those types should be.

From the N 3690 standard draft:

"The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id. The sizeof operator shall not be applied to an expression that has function or incomplete type, to an enumeration type whose underlying type is not fixed before all its enumerators have been declared, to an array of runtime bound, to the parenthesized name of such types, or to a glvalue that designates a bit-field. sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1. The result of sizeof applied to any other fundamental type (3.9.1) is implementation-defined."

Abstraction
  • 511
  • 2
  • 9
0

For a compiler, all pointers have a fixed size irrespective of the data type. Size of pointer in 32 bit computer is 4 bytes and that in 64 bit the size of pointer is 8 bytes. That is the reason your friend is getting 4 as pointer size and you are getting 8.