Dark Iot

Electronic and Programming for pleasure


malloc vs. new


This programm is to run in a IDE with a debuger.
Dependend from compiling x86 or x64 it crashes on different points.

But on a crash, you can check iCount, how many 1000 byte slices are allocated with malloc.

On x64 it crashes because of a unpermitted memory write, because the pointer table has just 10.000.000 entrys.
If you compile with x86 you hitting the 4 GB limit, so you allocate a bit less then 2 GB on Windows 10.

Differences and common's:
  • malloc and new just limited by the available memory
  • instead of returning NULL the new opperator throws a exception, while malloc returns a NULL pointer
  • new calls a constructor if available, and returns a class or struct.
    malloc returns VOID, which could be castet to something else, and dosn't call a constructor

  • main.cpp