哪些呢的malloc(0)的回报? [英] what does malloc(0) return?

查看:175
本文介绍了哪些呢的malloc(0)的回报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是什么的malloc(0)的回报?请问答案 realloc的是相同的(的malloc(0),0)

 #包括LT&;&stdio.h中GT;
#包括LT&;&malloc.h所GT;
诠释的main()
{
        的printf(%P \\ N的malloc(0));
        的printf(%P \\ N的realloc(的malloc(0),0));
        返回0;
}

从Linux GCC输出:

  manav @ manav工作站:〜$ GCC -Wall mal.c
manav @ manav工作站:〜$ ./a.out
0x9363008
(零)
manav @ manav工作站:〜$

输出的不断变化,每次为的malloc(0)。这是一个标准答案吗?而为什么会有人比学术研究兴趣获得这样一个指针,其他的?

编辑:

如果的malloc(0)返回虚拟指针,那么如何以下工作:

  INT的main()
{
    无效* PTR =的malloc(0);
    的printf(%P \\ N的realloc(PTR,1024));
    返回0;
}

编辑:

以下code输出可能每次迭代。它为什么不失败?

 #包括LT&;&stdio.h中GT;
#包括LT&;&malloc.h所GT;
诠释的main()
{        INT I;
        无效* PTR;
        的printf(测试用蛮力\\ n);
        对于(i = 0; I< 65000;我++)
        {
                PTR = malloc的(0);
                如果(PTR == realloc的(PTR,1024))
                        的printf(%迭代D:可能\\ n,I);
                其他
                {
                        的printf(无法迭代%d个\\ N,I);
                        打破;
                }
        }
        返回0;
}


解决方案

其他人回答如何的malloc(0)的作品。我会回答,你问这个尚未回答(我认为)的问题之一。现在的问题是关于的realloc(的malloc(0),0)


  

这是什么的malloc(0)返回?请问答案 realloc的是相同的(的malloc(0),0)


标准说,这个约的realloc(PTR,大小)


  • 如果 PTR NULL ,它的行为像的malloc(大小)

  • 否则( PTR NULL ),它由 PTR ,并返回一个指向一个新分配的缓冲区。但是,如果尺寸 0,C89说,效果等同于免费(PTR)。有趣的是,我无法找到C99草案(n1256或n1336)的发言。在C89,唯一明智的价值,在这种情况下返回是 NULL

所以,有两种情况:


  • 的malloc(0)收益 NULL 上的实现。那么你的的realloc()调用等效于的realloc(NULL,0)。这相当于的malloc(0)从上面(这是 NULL 在这种情况下)。

  • 的malloc(0)返回非 - NULL 。然后,调用等效于自由(的malloc(0))。在这种情况下,的malloc(0)的realloc(的malloc(0),0)是的不是的等价物。

请注意,这里存在一个有趣的案例:在第二种情况下,当的malloc(0)返回非 - NULL 成功,但它仍然可以返回 NULL 来表示失败。这将导致一个电话,如:的realloc(NULL,0),这将等同于的malloc(0) ,这可能会或可能不会返回 NULL

我不知道,如果在C99的遗漏是一个疏忽,或者如果它意味着,在C99,的realloc(PTR,0)非 - NULL PTR 不等同于免费(PTR)。我只是试图与的gcc -std = C99 ,和上面的等价于免费(PTR)

修改的:我想我明白你的困惑是什么:

让我们来看看从你的例子code的代码段:

  PTR =的malloc(0);
如果(PTR == realloc的(PTR,1024))

以上是不一样的的malloc(0)== realloc的(的malloc(0),1024)。第二,在的malloc()调用时的两倍,而在第一,你传递一个previously分配指针的realloc ()

让我们先来分析第一个code。假设的malloc(0)不返回 NULL 成功, PTR 有一个有效值。当你做的realloc(PTR,1024)的realloc()基本上给你,有大小1024的新缓冲区和 PTR 无效。一致性实现可以作为一个已在 PTR 返回相同的地址。所以,你的如果情况可能返回true。 (但是请注意,的realloc(PTR,1024)可能是不确定的行为后,看着 PTR 的值。)

现在你问的问题:的malloc(0)== realloc的(的malloc(0),1024)。在这种情况下,我们假设,无论是的malloc(0)在LHS和RHS返回非 - NULL 。然后,它们被保证是不同。此外,从的返回值的malloc() LHS上一直没有免费() D还没有,所以任何其他的malloc()释放calloc()的realloc()可能不返回值。这意味着,如果你写你的病情为:

 如果(的malloc(0)== realloc的(的malloc(0),1024)
    看跌期权(可能);

您不会看到可能在输出(除非双方的malloc() realloc的()失败并返回 NULL )。

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;INT主要(无效)
{
    无效* P1;
    无效* P2;    P1 = malloc的(0);
    P2 = realloc的(P1,1024);
    如果(P1 P2 = =)
        看跌期权(可能的话,OK);    / *忽略了内存泄漏* /
    如果(malloc的(0)== realloc的(malloc的(0),​​1024))
        看跌期权(不应该发生的,什么是错的);
    返回0;
}

在OS X中,我的code没有输出任何东西,当我跑了。在Linux上,它打印可能,确定

What does malloc(0) returns? Would the answer be same for realloc(malloc(0),0) ?

#include<stdio.h>
#include<malloc.h>
int main()
{
        printf("%p\n", malloc(0));
        printf("%p\n", realloc(malloc(0), 0));
        return 0;
}

Output from linux gcc:

manav@manav-workstation:~$ gcc -Wall mal.c
manav@manav-workstation:~$ ./a.out
0x9363008
(nil)
manav@manav-workstation:~$

The output keep changing everytime for malloc(0). Is this a standard answer? And why would anyone be interested in getting such a pointer, other than academic research?

EDIT:

If malloc(0) returns dummy pointer, then how does following works:

int main()
{
    void *ptr = malloc(0);
    printf("%p\n", realloc(ptr, 1024));
    return 0;
}

EDIT:

The following code outputs "possible" for every iteration. Why should it not fail ?

#include<stdio.h>
#include<malloc.h>
int main()
{

        int i;
        void *ptr;
        printf("Testing using BRUTE FORCE\n");
        for (i=0; i<65000; i++)
        {
                ptr = malloc(0);
                if (ptr == realloc(ptr, 1024))
                        printf("Iteration %d: possible\n", i);
                else
                {
                        printf("Failed for iteration %d\n", i);
                        break;
                }
        }
        return 0;
}

解决方案

Others have answered how malloc(0) works. I will answer one of the questions that you asked that hasn't been answered yet (I think). The question is about realloc(malloc(0), 0):

What does malloc(0) return? Would the answer be same for realloc(malloc(0),0)?

The standard says this about realloc(ptr, size):

  • if ptr is NULL, it behaves like malloc(size),
  • otherwise (ptr is not NULL), it deallocates the old object pointer to by ptr and returns a pointer to a new allocated buffer. But if size is 0, C89 says that the effect is equivalent to free(ptr). Interestingly, I can't find that statement in C99 draft (n1256 or n1336). In C89, the only sensible value to return in that case would be NULL.

So, there are two cases:

  • malloc(0) returns NULL on an implementation. Then your realloc() call is equivalent to realloc(NULL, 0). That is equivalent to malloc(0) from above (and that is NULL in this case).
  • malloc(0) returns non-NULL. Then, the call is equivalent to free(malloc(0)). In this case, malloc(0) and realloc(malloc(0), 0) are not equivalent.

Note that there is an interesting case here: in the second case, when malloc(0) returns non-NULL on success, it may still return NULL to indicate failure. This will result in a call like: realloc(NULL, 0), which would be equivalent to malloc(0), which may or may not return NULL.

I am not sure if the omission in C99 is an oversight or if it means that in C99, realloc(ptr, 0) for non-NULL ptr is not equivalent to free(ptr). I just tried this with gcc -std=c99, and the above is equivalent to free(ptr).

Edit: I think I understand what your confusion is:

Let's look at a snippet from your example code:

ptr = malloc(0);
if (ptr == realloc(ptr, 1024))

The above is not the same as malloc(0) == realloc(malloc(0), 1024). In the second, the malloc() call is made twice, whereas in the first, you're passing a previously allocated pointer to realloc().

Let's analyze the first code first. Assuming malloc(0) doesn't return NULL on success, ptr has a valid value. When you do realloc(ptr, 1024), realloc() basically gives you a new buffer that has the size 1024, and the ptr becomes invalid. A conforming implementation may return the same address as the one already in ptr. So, your if condition may return true. (Note, however, looking at the value of ptr after realloc(ptr, 1024) may be undefined behavior.)

Now the question you ask: malloc(0) == realloc(malloc(0), 1024). In this case, let's assume that both the malloc(0) on the LHS and RHS returns non-NULL. Then, they are guaranteed to be different. Also, the return value from malloc() on the LHS hasn't been free()d yet, so any other malloc(), calloc(), or realloc() may not return that value. This means that if you wrote your condition as:

if (malloc(0) == realloc(malloc(0), 1024)
    puts("possible");

you won't see possible on the output (unless both malloc() and realloc() fail and return NULL).

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    void *p1;
    void *p2;

    p1 = malloc(0);
    p2 = realloc(p1, 1024);
    if (p1 == p2)
        puts("possible, OK");

    /* Ignore the memory leaks */
    if (malloc(0) == realloc(malloc(0), 1024))
        puts("shouldn't happen, something is wrong");
    return 0;
}

On OS X, my code didn't output anything when I ran it. On Linux, it prints possible, OK.

这篇关于哪些呢的malloc(0)的回报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆