1

I understand that malloc() allocates at least the required size so it can give more that that. The question is will it give at least the same memory size in the same architecture?

Example:

int * myint = (int*)malloc(sizeof(int))
sizeof(myint)  //---> this gives me 8

will it give me 8 every time in my computer?

6502
  • 112,025
  • 15
  • 165
  • 265
DalekSupreme
  • 1,493
  • 3
  • 19
  • 32
  • 1
    Do you have any reason to believe otherwise? – dandan78 Nov 05 '13 at 10:47
  • 4
    You are not checking the size of the allocated data but the size of a pointer. – Michael M. Nov 05 '13 at 10:47
  • Like a great many people you are misunderstanding what `sizeof` does. It does *not* give you the size of a heap allocated memory block. There is no C++ function that does that. This is one of the many good reasons to use `std::vector` instead of malloc – john Nov 05 '13 at 10:48
  • I only realized now that you tagged C++, but use `malloc`, which is a C construct. While most of C is available in C++, it is rarely appropriate there. – Jan Hudec Nov 05 '13 at 11:45

4 Answers4

4

If you didn't do the malloc, it would still give you 8. Because it's not related to the malloc at all!

sizeof is a compile-time constant that tells you size needed to store instance of the type of argument. Type of argument is int * and your compiler apparently compiles for 64-bit, so pointer takes 8 bytes.

Note: In C++, sizeof is always compile-time constant. In C there is a special case for variable length arrays where the actual runtime size of the array is returned. But C++ does not have variable length arrays, so you don't have to bother with this special case.

Another note: In C++, you should be using vector and other collections and if all else fails (there is no make_unique, unfortunately) new, but usually not malloc.

Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
  • Ah I see, another [fastest gun in the west](http://meta.stackexchange.com/q/9731/169090) enthusiast – Shahbaz Nov 05 '13 at 10:50
3

There is no standard way of knowing exactly how much memory has been reserved behind-the-scenes by the runtime. There isn't even a standard way of finding out how much you originally asked for, either. (If you need to remember this, you need to store it yourself.)

A given implementation may reserve exactly what you ask for, or some higher amount, and it may vary from time to time. But you should neither know nor care.

As others have pointed out, in your code you are in fact asking for the size of the int* pointer rather than the size of the allocated block.

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
1

sizeof will always return the same value in a given compilation because it's a compile-time value (it's not determined when running the program.

Note however that for example my 64-bit ubuntu machine can also run 32-bit code and the size of some types will change depending on if I want to compile a 32-bit executable (e.g. with gcc -m32 ...) or a 64-bit executable. So the value returned by sizeof may depend on compilation options.

sizeof(myint) in your code is however returning the size of the pointer, there is no way to know the real size of the actually allocated memory block (this is a detail that is left to the malloc implementation). All you know (portably) from a value returned from malloc is that it's enough memory (i.e. not less than what you asked) and that can be used to store any basic type (it respects the most stringent alignment on the platform).

6502
  • 112,025
  • 15
  • 165
  • 265
0

it is because you are finding out the size of pointer not the size of the alloted memory

Alvin
  • 387
  • 4
  • 7
  • 18