-4

I wanted to test my knowledge, and wrote a small program, which looks like this:

#include <iostream>
#include <cstdlib>

int main()
{
    int* mem1 = (int*) malloc(2 * sizeof(int));
    int* mem2 = (int*) malloc(5 * sizeof(int));

    std::cout << "mem1: " << sizeof(mem1) << std::endl;
    std::cout << "mem2: " << sizeof(mem2) << std::endl;

    return 0;
}

I get the output:

mem1: 8
mem2: 8

When I change the values, that malloc(2 * sizeof(int)) to malloc(3 * sizeof(int)), the output also doesn't change.

This is the Tutorial I used, so I am not entirely sure, if this is memory safe, with the conversion to int* when calling malloc. I have also found this question, but I don't find it to be very helpful in my case. Clang++ is the Compiler I used, but I don't think it makes any difference.

My best guess is, that it allocates the same memory, because it doesn't know where the memory ends.

youngtrashbag
  • 46
  • 1
  • 9
  • 1
    You are not supposed to use `malloc` in C++ like this. It is currently technically always undefined behavior to use `malloc`ed memory without intervening placement-new and it will certainly break in practice for non-trivial types. You are supposed to use `new` or even better no manual dynamic memory management at all. You should use `std::vector` instead. – walnut Feb 10 '20 at 12:03
  • 1
    All the library functions that the linked tutorial site mentions, with the exception of ``, are *C* library functions inherited into C++. The site is not actually teaching the C++ standard library. I suggest you don't use it to learn C++. – walnut Feb 10 '20 at 12:05
  • `sizeof` does not do what you think it does. Did you want [std::size](https://en.cppreference.com/w/cpp/iterator/size) maybe? Also, *why* are you using `malloc` in a C++ program?? You'd *never* do this `int* mem1 = (int*) malloc(2 * sizeof(int));` in a sane C++ program. The C-style casts should just go, but even then the rest is just wrong. Look up `std::vector`. – Jesper Juhl Feb 10 '20 at 12:08

4 Answers4

1

sizeof is telling you the compile-time size of the pointer (it would report the same value even if you never called malloc at all). There is no standards-compliant way to determine the amount of memory allocated by malloc; you have to store it side-band if you need to preserve that information.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
0

The 8 bytes that you see is the size of the pointer(int*). sizeof() is done at compile time and there's no way for the compiler to know this size at compile time(malloc is dynamic memory allocation).

theWiseBro
  • 1,439
  • 12
  • 11
0

The expression sizeof(mem1) will always return the same value, no matter the size of the allocated memory block! This is because mem1 is a pointer to the allocated memory, and the size of a pointer does not change (within a given environment, that is).

In your case (presumably, a build on a 64-bit platform) pointers are 8 bytes in length. You could build this for a different platform (say, a 32-bit system) and you may get a different size (for example, 4); but, on that platform, that size will then always be 4.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0

As mentioned above, there are a few problems in this question:

  1. In C++ you dynamically allocate memory using the new operator. The option of using malloc is for working with c-style APIs, when needed dynamic allocations that will be de-allocated by c-free function. In order to make such an allocation, it is best advised to use std::malloc which you can read about in this link (cpp reference).
  2. The operator sizeof is calculating the size of the static type, meaning that it is giving you back the size of integer pointer, which is constant, depending on your CPU architecture.
  3. Working with raw pointers in C++ is ill-advised, you should always make sure that for each item dynamically created in memory there is an owner, that is the one responsible to delete it when no longer needed. In C++11 and later, this can be achieved by using smart pointers, such as std::unique_ptr. In fact, C++ has created a great Idiom for resource management, called RAII (Resource Allocation Is Initialization), which sets it apart from many other languages.
Kerek
  • 1,106
  • 1
  • 8
  • 18