虽然分配空间为0,但是ptr的指针地址是存在了

解决方案 »

  1.   

    看一些msdn就知道了,没什么好奇怪的吧malloc returns a void pointer to the allocated space, or NULL if there is insufficient memory available. To return a pointer to a type other than void, use a type cast on the return value. The storage space pointed to by the return value is guaranteed to be suitably aligned for storage of any type of object. If size is 0, malloc allocates a zero-length item in the heap and returns a valid pointer to that item. Always check the return from malloc, even if the amount of memory requested is small.
      

  2.   

    如果释放 free(ptr); 好像会出错
      

  3.   

    char *ptr = NULL;
    if ((ptr = (char *)malloc(0)) == NULL) 
    puts("Is Null"); 
    else 
    puts("Not Null");你再试试
      

  4.   

    int *ptr = new int;
    delete ptr;
    这个时候ptr肯定不为NULL,不信你运行试试。
    指针是否指向有效空间和本身为什么值并不冲突