zoukankan      html  css  js  c++  java
  • Question : New int[0] and Sizeof on dynamically allocated memory

    Sizeof on dynamically allocated memory

      delete [] does know the size that was allocated. However, that knowledge resides in the runtime or in the operating system's memory manager, meaning that it is not available to the compiler during compilation. And sizeof() is not a real function, it is actually evaluated to a constant by the compiler, which is something it cannot do for dynamically allocated arrays, whose size is not known during compilation.

    Also, consider this example:

    int *arr = new int[256];
    int *p = &arr[100];
    printf("Size: %d
    ", sizeof(p));

      How would the compiler know what the size of p is? The root of the problem is that arrays in C and C++ are not first-class objects. They decay to pointers, and there is no way for the compiler or the program itself to know whether a pointer points to the beginning of a chunk of memory allocated by new, or to a single object, or to some place in the middle of a chunk of memory allocated by new.

      One reason for this is that C and C++ leave memory management to the programmer and to the operating system, which is also why they do not have garbage collection. Implementation of newand delete is not part of the C++ standard, because C++ is meant to be used on a variety of platforms, which may manage their memory in very different ways. It may be possible to let C++ keep track of all the allocated arrays and their sizes if you are writing a word processor for a windows box running on the latest Intel CPU, but it may be completely infeasible when you are writing an embedded system running on a DSP.

    new int[0]

      

    From 5.3.4/7

    When the value of the expression in a direct-new-declarator is zero, the allocation function is called to allocate an array with no elements.

    From 3.7.3.1/2

    The effect of dereferencing a pointer returned as a request for zero size is undefined.

    Also

    Even if the size of the space requested [by new] is zero, the request can fail.

      That means you can do it, but you can not legally (in a well defined manner across all platforms) dereference the memory that you get - you can only pass it to array delete - and you should delete it.

    Here is an interesting foot-note (i.e not a normative part of the standard, but included for expository purposes) attached to the sentence from 3.7.3.1/2

    [32. The intent is to have operator new() implementable by calling malloc() or calloc(), so the rules are substantially the same. C++ differs from C in requiring a zero request to return a non-null pointer.]

  • 相关阅读:
    【算法杂谈】本博客算法总结
    【算法杂谈】LJX的迪杰斯特拉算法报告
    首师大附中互测题:50136142WXY的坑爹百度地图【B006】(可以喝的超大桶水)
    【其它】一些好用的的翻*软件
    【更新】关于最近博客的随笔更新问题
    首师大附中互测题:99999999海岛帝国后传:算法大会【D001】
    首师大附中互测题:LJX的校园:入学典礼【C003】
    首师大附中互测题:50229234海岛帝国:独立之战【C002】
    二叉苹果树(树形DP)
    打鼹鼠
  • 原文地址:https://www.cnblogs.com/yetanghanCpp/p/9115635.html
Copyright © 2011-2022 走看看