关于realloc的注意事项

强烈注意realloc的size应该是你想扩充到的num数*sizeof(type), 像下面这样就越界了 :

if( 0 == ( a = (int *)realloc(static_cast(a), 20) ) ){     // 20 should be 20 * sizeof(int)
    cerr << "realloc mem failed is " << endl;
    return -1;
  }

  cout << "realloc success" << endl;

  for(int k = 0; k < 20; k++){
    a[k] = k;
  }

你可能感兴趣的:(关于realloc的注意事项)