回顾我的回答 [英] Review of my answer
问题描述
你好clc。
以下是我对新闻组发布的问题的回复,
其中,一个人说我的建议错了,没有
说到哪里以及为什么。我转向clc。
[BEGINS]
Hi clc.
Following is my reply to a question posted in a newsgroup,
in which, a person said my advice was wrong without
saying where and why. I turn to clc.
[BEGINS]
嗨朋友们,
我突然想到这些问题,看起来对这些问题感到困惑谁能告诉我这个问题的答案?
这段代码可能有错误!如果有任何建议补救措施,那么就给出输出!
#1.c
#include< stdio.h>
您应该包含< stdlib.h>对于malloc。
main()
{
void *指针;
void * vector;
void *地址;
void * location ;
malloc的原型是:
void * malloc(size_t);
其中,
size_t是< stdlib.h>中的无符号整数typedefe。
pointer = malloc(-1);
阅读: http://www.geocities.com/vijoeyz/faq/c/unsigned2.txt
了解如何将负整数分配给未签名的
int。
vector = malloc(0);
当
大小为0时,每个编译器都可以自由定义malloc()的行为。通常,编译器会记录它的工作方式。
两个可能性是:
当请求的大小为零时
*返回一个NULL指针。
*假设一些非零大小
在任何一种情况下,使用返回值malloc()都会导致未定义的
行为。
地址= malloc的(1);
这没关系。
location = NULL;
免费(指针);
免费(矢量);
免费(地址);
如果相应的mallocs成功,那就没关系。
免费(位置);
Hi friends,
I cam across these questions and seem baffled by these
Can anyone tell me answers to this?
This code may have bugs!. If any suggest remedy
to this else give output!
#1.c
#include<stdio.h>
You should include <stdlib.h> for malloc.
main()
{
void *pointer;
void *vector;
void *address;
void *location;
The prototype of malloc is:
void *malloc ( size_t );
where,
size_t is an unsigned integer typedefe in <stdlib.h>.
pointer=malloc(-1);
Read: http://www.geocities.com/vijoeyz/faq/c/unsigned2.txt
to know about how a negative integer is assigned to an unsigned
int.
vector=malloc(0);
Each compiler is free to define the behaviour of malloc () when
the size is 0. Usually, the compiler documents how it does.
Two possibilies are:
When the requested size is zero
* return a NULL pointer.
* Assume some non-zero size
In either case, using the return value malloc() can cause undefined
behaviour.
address=malloc(1);
This is OK.
location=NULL;
free(pointer);
free(vector);
free(address);
If the corresponding mallocs were successful, then this is OK.
free(location);
这也是合法的,但是没有免费的行动(NULL)。
[结束]
This is also legal, but no action occurs for free ( NULL ).
[ENDS]
推荐答案
Vijay Kumar R Zanvar < 6 *********** @ globaledgesoft.com>写道:
"Vijay Kumar R Zanvar" <vi***********@globaledgesoft.com> wrote:
以下是我对新闻组中发布的问题的回复,
其中,有人说我的建议是错误的,没有说明在哪里以及为什么。我转向clc。
Following is my reply to a question posted in a newsgroup,
in which, a person said my advice was wrong without
saying where and why. I turn to clc.
vector = malloc(0);
当大小为0时,每个编译器都可以自由定义malloc()的行为。通常,编译器记录它是如何做的。
两个可能性是:
当请求的大小为零时
*返回一个NULL指针。
vector=malloc(0);
Each compiler is free to define the behaviour of malloc () when
the size is 0. Usually, the compiler documents how it does.
Two possibilies are:
When the requested size is zero
* return a NULL pointer.
这是正确的。
*假设一些非零大小
这不是;或者更确切地说,这是误导。 malloc(0)允许返回
(在C89中)任何唯一指针;在C99中,它被允许表现得好像
它是一些非零大小_除了指针一定不能是
dereferenced_。当_just_假设
某些非零大小时,该限制不存在。
在任何一种情况下,使用返回值malloc()都可能导致未定义的行为。
This is correct.
* Assume some non-zero size
This is not; or rather, it is misleading. malloc(0) is allowed to return
(in C89) just any unique pointer; in C99, it is allowed to behave as if
it were some non-zero size _except that the pointer must not be
dereferenced_. That restriction is not present when you _just_ assume
some non-zero size.
In either case, using the return value malloc() can cause undefined
behaviour.
嗯,不。不完全的。使用它_as如果它指向一些可用的对象_
会导致未定义的行为。
在C89中,如果得到非null结果,我不确定是否,例如,
将它与任何其他相同类型或空指针进行相等比较
调用UB;我不能确定它是否应该是一个_valid_
唯一指针,如果它不是,那么即使是比较也是UB。如果是,
比较必须有效。
在C99中,如果得到非null结果,那么结果必须是有效的
指针,并且可以被视为任何指针_except_,它不能被取消引用
。例如,你可以比较它的相等性,你可以将它传递给一个函数(可能不会解析它,但是......),你可以分配
它是一个指针变量。如果你得到一个空指针,这两个标准也是如此。
Well, no. Not quite. Using it _as if it pointed to some usable object_
causes undefined behaviour.
In C89, if you get a non-null result, I''m not sure whether, e.g.,
comparing it for equality with any other same-type or null pointer
invokes UB; I can''t determine whether it is supposed to be a _valid_
unique pointer, and if it isn''t, even the comparison is UB. If it is,
the comparison must work.
In C99, if you get a non-null result, that result must be a valid
pointer, and can be treated as any pointer _except_ that it cannot be
dereferenced. For example, you can compare it for equality, you can pass
it to a function (which may then not deref it, but...), you can assign
it to a pointer variable. This is also true if you get a null pointer,
under either Standard.
address = malloc(1);
address=malloc(1);
这没关系。
This is OK.
当然,虽然没用,但我认为这不是
代码。
Albeit useless, of course, but I presume that was not the point of the
code.
location = NULL;
免费(指针);
免费(向量);
免费(地址);
location=NULL;
free(pointer);
free(vector);
free(address);
如果相应的mallocs成功,那就没问题。
If the corresponding mallocs were successful, then this is OK.
即使不是,这也没关系。一个不成功的malloc()返回一个null
指针,而free(0)在两个标准下都是合法的(和一个no-op)。
Richard
Even if not, this is OK. An unsuccessful malloc() returns a null
pointer, and free(0) is legal (and a no-op) under both Standards.
Richard
" Vijay Kumar R Zanvar" < 6 *********** @ globaledgesoft.com>写道:
"Vijay Kumar R Zanvar" <vi***********@globaledgesoft.com> wrote:
以下是我对新闻组中发布的问题的回复,
其中,有人说我的建议是错误的,没有说明在哪里以及为什么。我转向clc。
Following is my reply to a question posted in a newsgroup,
in which, a person said my advice was wrong without
saying where and why. I turn to clc.
vector = malloc(0);
当大小为0时,每个编译器都可以自由定义malloc()的行为。通常,编译器记录它是如何做的。
两个可能性是:
当请求的大小为零时
*返回一个NULL指针。
vector=malloc(0);
Each compiler is free to define the behaviour of malloc () when
the size is 0. Usually, the compiler documents how it does.
Two possibilies are:
When the requested size is zero
* return a NULL pointer.
这是正确的。
*假设一些非零大小
这不是;或者更确切地说,这是误导。 malloc(0)允许返回
(在C89中)任何唯一指针;在C99中,它被允许表现得好像
它是一些非零大小_除了指针一定不能是
dereferenced_。当_just_假设
某些非零大小时,该限制不存在。
在任何一种情况下,使用返回值malloc()都可能导致未定义的行为。
This is correct.
* Assume some non-zero size
This is not; or rather, it is misleading. malloc(0) is allowed to return
(in C89) just any unique pointer; in C99, it is allowed to behave as if
it were some non-zero size _except that the pointer must not be
dereferenced_. That restriction is not present when you _just_ assume
some non-zero size.
In either case, using the return value malloc() can cause undefined
behaviour.
嗯,不。不完全的。使用它_as如果它指向一些可用的对象_
会导致未定义的行为。
在C89中,如果得到非null结果,我不确定是否,例如,
将它与任何其他相同类型或空指针进行相等比较
调用UB;我不能确定它是否应该是一个_valid_
唯一指针,如果它不是,那么即使是比较也是UB。如果是,
比较必须有效。
在C99中,如果得到非null结果,那么结果必须是有效的
指针,并且可以被视为任何指针_except_,它不能被取消引用
。例如,你可以比较它的相等性,你可以将它传递给一个函数(可能不会解析它,但是......),你可以分配
它是一个指针变量。如果你得到一个空指针,这两个标准也是如此。
Well, no. Not quite. Using it _as if it pointed to some usable object_
causes undefined behaviour.
In C89, if you get a non-null result, I''m not sure whether, e.g.,
comparing it for equality with any other same-type or null pointer
invokes UB; I can''t determine whether it is supposed to be a _valid_
unique pointer, and if it isn''t, even the comparison is UB. If it is,
the comparison must work.
In C99, if you get a non-null result, that result must be a valid
pointer, and can be treated as any pointer _except_ that it cannot be
dereferenced. For example, you can compare it for equality, you can pass
it to a function (which may then not deref it, but...), you can assign
it to a pointer variable. This is also true if you get a null pointer,
under either Standard.
address = malloc(1);
address=malloc(1);
这没关系。
This is OK.
当然,虽然没用,但我认为这不是
代码。
Albeit useless, of course, but I presume that was not the point of the
code.
location = NULL;
免费(指针);
免费(向量);
免费(地址);
location=NULL;
free(pointer);
free(vector);
free(address);
如果相应的mallocs成功,那就没问题。
If the corresponding mallocs were successful, then this is OK.
即使不是,这也没关系。一个不成功的malloc()返回一个null
指针,而free(0)在两个标准下都是合法的(和一个no-op)。
Richard
Even if not, this is OK. An unsuccessful malloc() returns a null
pointer, and free(0) is legal (and a no-op) under both Standards.
Richard
" Richard Bos" < rl*@hoekstra-uitgeverij.nl>在消息新闻中写道:40 **************** @ news.individual.net ...
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message news:40****************@news.individual.net...
" Vijay Kumar R Zanvar" < 6 *********** @ globaledgesoft.com>写道:
"Vijay Kumar R Zanvar" <vi***********@globaledgesoft.com> wrote:
以下是我对新闻组中发布的问题的回复,
其中,有人说我的建议是错误的,没有说明在哪里以及为什么。我转向clc。
Following is my reply to a question posted in a newsgroup,
in which, a person said my advice was wrong without
saying where and why. I turn to clc.
vector = malloc(0);
vector=malloc(0);
每个编译器都可以自由地定义malloc()的行为时
大小为0.通常,编译器记录它是如何做的。
两个可能性是:
当请求的大小为零时
*返回一个NULL指针。
Each compiler is free to define the behaviour of malloc () when
the size is 0. Usually, the compiler documents how it does.
Two possibilies are:
When the requested size is zero
* return a NULL pointer.
这是正确的。
This is correct.
*假设一些非零大小
这不是;或者更确切地说,这是误导。 malloc(0)被允许返回
(在C89中)任何唯一的指针;在C99中,允许它表现得好像它是一些非零的大小_除了指针不能被取消引用_。当_just_假设某些非零大小时,该限制不存在。
This is not; or rather, it is misleading. malloc(0) is allowed to return
(in C89) just any unique pointer; in C99, it is allowed to behave as if
it were some non-zero size _except that the pointer must not be
dereferenced_. That restriction is not present when you _just_ assume
some non-zero size.
在任何一种情况下,使用返回值malloc()都可能导致未定义的行为。
In either case, using the return value malloc() can cause undefined
behaviour.
嗯,不。不完全的。使用它_as如果它指向一些可用的对象_
会导致未定义的行为。
在C89中,如果你得到一个非null的结果,我不确定是否,比如
比较它与任何其他同类型或空指针相等
调用UB;我无法确定它是否应该是一个_valid_
唯一指针,如果它不是,那么即使比较是UB。如果是,则比较必须有效。
在C99中,如果得到非null结果,那么结果必须是有效的指针,并且可以被视为任何指针_except_ that它不能被取消引用。例如,你可以比较它是否相等,你可以将它传递给一个函数(它可能不会解析它,但是......),你可以将它指定给一个指针变量。如果在任一标准下获得空指针,也是如此。
Well, no. Not quite. Using it _as if it pointed to some usable object_
causes undefined behaviour.
In C89, if you get a non-null result, I''m not sure whether, e.g.,
comparing it for equality with any other same-type or null pointer
invokes UB; I can''t determine whether it is supposed to be a _valid_
unique pointer, and if it isn''t, even the comparison is UB. If it is,
the comparison must work.
In C99, if you get a non-null result, that result must be a valid
pointer, and can be treated as any pointer _except_ that it cannot be
dereferenced. For example, you can compare it for equality, you can pass
it to a function (which may then not deref it, but...), you can assign
it to a pointer variable. This is also true if you get a null pointer,
under either Standard.
address = malloc(1);
address=malloc(1);
这没关系。
This is OK.
虽然当然没用,但我认为这不是
代码的重点。
Albeit useless, of course, but I presume that was not the point of the
code.
location = NULL;
免费(指针);
免费(矢量);
免费(地址) ;
location=NULL;
free(pointer);
free(vector);
free(address);
如果相应的mallocs成功,那就没问题。
If the corresponding mallocs were successful, then this is OK.
即使没有,也没关系。一个不成功的malloc()返回一个null
指针,free和(0)在两个标准下都是合法的(和一个no-op)。
Richard
Even if not, this is OK. An unsuccessful malloc() returns a null
pointer, and free(0) is legal (and a no-op) under both Standards.
Richard
非常感谢你。这清除了我的怀疑。
Vijay
Thank you very much. This clears my doubt.
Vijay
这篇关于回顾我的回答的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!