0
#include<stdio.h>
#include<stdlib.h>

int main()
    {
        int *p;
        p = (int *)malloc(20);
        printf("%d\n", sizeof(p));
        free(p);
        return 0;
    }

On my 64-bit machine, 4 is printed as the size of p. I'm assuming this is because integers take up 4 bytes in memory, and p is an integer pointer. What if I was running a 32-bit machine? Also, what would happen if I replaced int *p with double *p and (int *)malloc(20) with (double *)malloc(20)?

db2791
  • 1,040
  • 6
  • 17
  • 30
  • your question is "what is the size of a pointer". Where the pointer is pointing makes no difference to this; variables have fixed sizes. – M.M Feb 19 '15 at 06:15
  • On my 64b Ubuntu, `p` has a size of 8 bytes, ... – Déjà vu Feb 19 '15 at 06:18
  • @MattMcNabb Hmm do you mean "*pointer* variables have fixed sizes"? – Déjà vu Feb 19 '15 at 06:57
  • @ring0 it is right if its a 32bit system size is 4 for a pointer while 64bit its 8. Here his build must be in 32 bit even though his OS is 64bit. Isn't it? – Dinal24 Feb 19 '15 at 07:36
  • @ring0 no, all variables. (They don't change size after being created) – M.M Feb 19 '15 at 08:42

4 Answers4

0

You are assuming wrong. In printf("%d\n", sizeof(p)); you are not printing size of integer.You are printing here sizeof 'pointer to an integer' which is 4 bytes in your case.Most probably you will get same result on 32-bit machine.

Now about malloc, It allocates number of bytes and returns pointer to it.So same size of memory will be allocated even if you cast the pointer from int* to double*.

Vagish
  • 2,520
  • 19
  • 32
0

In pointers, It will take four bytes for all pointers.

So while you are checking with sizeof, even it is a character pointer it will give four bytes. If you need the value of that pointer use like this.

  printf("%d\n", sizeof(p));// It will give the pointer size.

malloc is allocating the given bytes. And it will give equal to all the pointers. Then don't cast the result of malloc. Refer this link.

Community
  • 1
  • 1
Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
0

Irrespective of whether 64-bit system or 32-bit system, size of a pointer variable is 4 bytes by default since 64-bit build settings also have Debug32 by default.
If we specifically change build settings on 64-bit, then the pointer variable can hold 8 bytes. I'm assuming this is because integers take up 4 bytes in memory, and p is an integer pointer.

Your assumption is not correct ! To answer your doubt not only integer pointer take up 4 bytes.

int *ptrint;
char *ptrchar;
float *ptrfloat;
double *ptrdouble;

Here all ptrint, ptrchar, ptrfloat, ptrdouble takes 4 bytes of memory since it would be the address stored in that variable.

And if you replace int *p with double *p and (int *)malloc(20) with (double *)malloc(20) , the size would be still 4 bytes. I hope this ans cleared your doubts.

Shivaraj Bhat
  • 841
  • 2
  • 9
  • 20
  • In a 64-bit machine, generally all compilers and default build settings would be 32. Hence pointer variable size would be 4 bytes in general. – Shivaraj Bhat Feb 19 '15 at 07:47
  • _Irrespective of whether 64-bit system or 32-bit system, size of a pointer variable is 4 bytes._ then obviously its not 'Irrespective', only if you have the wrong build settings... – Dinal24 Feb 19 '15 at 07:53
0

You have some misunderstanding let me point them,

p = (int *)malloc(20);

You are allocating memory of 20 bytes and malloc returns a void pointer and but compiler does the casting for you and (int *) is not needed. Even though you have a pointer to a double or an int it takes the same amount of bytes (In a 32bit system this merely for mapping 4GB memory space).

// Should this be 4 or 8?
printf("%d\n", sizeof(p));

This should be 8 on a x64 platform if your executable or the build is x64 only. I assume your build is 32bit and it returns 4.

Above printf has a wrong specifier. sizeof returns size_t and not an int. So correct form should be,

printf("%zu\n", sizeof(p));
Dinal24
  • 3,162
  • 2
  • 18
  • 32