Heap on Mac and Linux

#Linux #Mac #C++

Some caveats about heap on mac and linux

#include <iostream>

int main() {

   double *a = new double[5];

   for(auto i = 0; i < 5; i++)
     a[i] = i;

   std::cout << a[-10] << std::endl;


This program runs on Mac without warning or error. However, it gives segmentation fault (core dump) error on ubuntu and cent os.

Published: by ;

L Ma (2017). 'Heap on Mac and Linux', Datumorphism, 09 April. Available at: https://datumorphism.leima.is/til/programming/cpp/cpp-heap-mac-linux-diff/.

Current Ref:

  • til/programming/cpp/cpp-heap-mac-linux-diff.md