-3

I was wondering that I have read everywhere, pointers dont occupy space in memory because they store the address of a variable. But, for them to store the address, the storing would require some space in the first place. Correct me if I am wrong, I am quite confused.

Will

int *ptr;

take space in memory when we declare it?

Like,

int num;

will take 2 bytes. or not?

If not, how will the pointer "store" the address without having any space to store it?

tod
  • 1,539
  • 4
  • 17
  • 43
Electrux
  • 19
  • 6
  • 1
    "pointers dont occupy space in memory" - Plain wrong! Where do you think they store the address? A _declaration_ alone will never occupy memory for any object type. You ask very basic questions. Please read a good C book; from single seperate questions you will not get the whole picture and miss very important details at least. – too honest for this site Mar 24 '16 at 10:51
  • 1
    I'm not sure where this "everywhere" that you read this is, but I've never been there. – juanchopanza Mar 24 '16 at 10:54
  • `int num` will take 4 bytes. or rather, `sizeof(int)` bytes. – Shark Mar 24 '16 at 10:54
  • ok... thank you for answering :) – Electrux Mar 24 '16 at 10:55
  • 1
    I was a junior programmer when ints were taking only two bytes. –  Mar 24 '16 at 10:55
  • ints are still 2 bytes in some places like on Arduino, however. in the normal world, we call them `short int` or `int16` :D – Shark Mar 24 '16 at 10:56

4 Answers4

0

No variable (named object) takes zero space.

Although it can be optimized away, e.g. if it isn't used.

However, it's possible for a base class object to take zero bytes, under some special conditions, essentially that this “empty base class optimization” must not cause two distinct objects of the same type to apparently reside at the same address.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
0

Yes, it takes space but just a small one that does not depend on the actual type. It is 8 Bytes on 64-bit platform for example. This address is stored on the stack by the way.

Example:

class foo; // the size of this class is 10 byte
void bar(){
    foo* x; // this is just 4 bytes on x86 machine
    x=new foo{}; // there is 10 bytes on the heap and the original 4 bytes on the stack
    delete x; //we just have now the 4 bytes of the pointer in the stack
}// the pointer is destroyed and nothing left in the heap or in the stack

If you want a better understanding, consider the pointer as a class which has the following design idea :

template <typename T>
class pointer{
public:
    void allocate(){
       p=new T{};
    }
    T* p;
};

When you allocate an object of it, you should do this:

pointer<int> my_pointer; // This is obviously a stack object
my_pointer.allocate(); // Now you have allocated memory on heap

P.S. Previous example is so breif and useless. It just for explaining a small idea

Humam Helfawi
  • 19,566
  • 15
  • 85
  • 160
  • umm... which stack? and wouldn't it be better to simply use a normal integer instead of a pointer because, 32 bit would mean 4 bytes which is more than integer itself? – Electrux Mar 24 '16 at 10:53
  • 1
    it is not about size. storing in the heap or in the stack depends on other criteria http://stackoverflow.com/questions/599308/proper-stack-and-heap-usage-in-c – Humam Helfawi Mar 24 '16 at 10:58
  • 1
    oh ok thanks for the link and answer :) – Electrux Mar 24 '16 at 11:00
  • 1
    @Electrux: "which stack" is a good question. C++ has a conceptual stack, which is the set of all functions which are in progress plus their associated variables. There's often also a hardware stack, and this is _similar_. But inlined functions for instance are not on the inline stack, and some variables may temporarily exist in hardware registers instead of the stack etc. – MSalters Mar 24 '16 at 11:05
  • @MSalters thanks for explaining :) – Electrux Mar 24 '16 at 11:10
0

Obviously,

 int *ptr

would reserve space in memory. Pointer is, after all, just a memory address therefore a number and therefore represented in memory as a series of bits(bytes). How much memory a pointer occupies is platform specific and depends on the size of addressable memory(usually 32 or 64 bits).

  • A constant address can be optimized away by hard-coding it as an immediate operand in the program, so that no data space is allocated to it. –  Mar 24 '16 at 11:00
  • And a non-constant address could be in a register instead of memory. – MSalters Mar 24 '16 at 11:00
  • Of course but conceptually it **is** stored in memory(if we look at registers as a form of memory) just as any other variable. – Ivan Živković Mar 24 '16 at 12:23
0

A pointer occupies memory as any other variable, since it holds the address in memory of a variable, it usually takes 32 bits for any type.

But a pointer can be used as an array to allocate memory on the heap, however it continues to store the starting address of the array so it doesnt change size (for example when you use malloc).

You can check the size of a pointer by yourself by using sizeof.

Matth
  • 154
  • 7