4

I'm trying to create a pointer to a 6 element int in a function to return it later, so for that purpose I'm using malloc, but it seems to be acting not as I expected. Here's the code:

int j = 0;
for (;j < 5; j++) {
    int * intBig = malloc(j * sizeof(int));
    printf("sizeof intBig - %ld\n", sizeof(intBig));
}

Prints the same number 8 bytes as the sizeof(intBig) at each iteration. Whereas I would expect a series of 4, 8, 12, 16. What am I missing in this instance?

Morgan Wilde
  • 16,795
  • 10
  • 53
  • 99
  • sizeof(intBig) will give you the size of pointer intBig not the total size of the malloced array. – A. K. Sep 17 '13 at 15:50

4 Answers4

10

This is because you're printing the size of an int *. Such a pointer always has the same size. sizeof is a compiler construct. It cannot know things that only occur at runtime, such as dynamic memory allocation. Would it be something like

int intBig[100];

then you would get the size of the array back (in bytes), because the compiler knows how large it is. But the result of the sizeof operator is always a compile-time constant¹, so there is no way what you have there could yield anything else.

Besides, you have a memory leak there because you're not free-ing your memory again.


¹ Variable Length Arrays (VLA) are an exception, but they were not used here.

Joey
  • 344,408
  • 85
  • 689
  • 683
  • Your new example is quite broken, in addition to being nonsense. Even if we fix it (`int a[100]; int* p = a;`), it's still wrong, because `sizeof p` is still a pointer size, not equal to `sizeof a` – Ben Voigt Sep 17 '13 at 15:51
  • Too much Java recently. Fixed. – Joey Sep 17 '13 at 15:53
  • 2
    "sizeof operator is always a compile-time constant" is inconsistent with the C standard 6.5.3.4 2 "... If the type of the operand is a **variable length array type**, the operand is evaluated; *otherwise*, the operand is not evaluated and the result is an integer constant." – chux - Reinstate Monica Sep 17 '13 at 16:42
  • @chux: It'd be nice if you could correct the answer, then. I cannot delete it now that it has been accepted and I never ventured that far into newer C standards. – Joey Sep 17 '13 at 19:38
  • @Јοеу Hope the edit meets with your approval. – chux - Reinstate Monica Sep 17 '13 at 19:53
5

You cannot use sizeof to figure out the size of a memory block returned from malloc().

Except for variable length arrays in C99 and later, sizeof works only on statically known sizes.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
unwind
  • 391,730
  • 64
  • 469
  • 606
2

Because every time you are printing the size of a pointer which is the size of an address which is 8 bytes.

Marc
  • 2,631
  • 1
  • 12
  • 13
2

sizeof tells you the size of the pointer intBig, not what it points to.

There's no standard way to discover the size of the memory block it points to, so you have to remember that separately.

If you have access to C++, just use std::vector for your dynamic array needs... it knows its size and doesn't forget to deallocate.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720