一个malloc问题 [英] A malloc question
问题描述
下面指针会发生什么?
SomeStruct * p;
p = malloc(100 * sizeof(SomeStruct)); / *没有演员* /
返回((void *)(p + 1)); / *将返回的指针指向
到第二个结构? * /
对我来说似乎无法保证。
/为什么茶
What happens to the pointer below?
SomeStruct *p;
p = malloc(100*sizeof(SomeStruct)); /* without a cast */
return((void *)(p+1)); /* will the returned pointer point
to the 2nd struct? */
Seems to me there is no guarantee it will.
/Why Tea
推荐答案
为什么喝茶écrit:
Why Tea a écrit :
下面的指针会发生什么变化?
SomeStruct * p;
p = malloc(100 * sizeof(SomeStruct)); / *没有演员* /
返回((void *)(p + 1)); / *将返回的指针指向
到第二个结构? * /
对我来说似乎无法保证。
/为什么茶
What happens to the pointer below?
SomeStruct *p;
p = malloc(100*sizeof(SomeStruct)); /* without a cast */
return((void *)(p+1)); /* will the returned pointer point
to the 2nd struct? */
Seems to me there is no guarantee it will.
/Why Tea
在我看来,它必须指向p + 1。由于p是指向SomeStruct的指针
,p + 1指向表格中的第二个结构
。
In my opinion it must point to p+1. Since p is a pointer
to SomeStruct, p+1 points to the second structure
in the table.
为什么Tea写道:
Why Tea wrote:
下面的指针会发生什么变化?
SomeStruct * p;
p = malloc(100 * sizeof(SomeStruct)); / *没有演员* /
返回((void *)(p + 1)); / *将返回的指针指向
到第二个结构? * /
对我来说似乎无法保证。
What happens to the pointer below?
SomeStruct *p;
p = malloc(100*sizeof(SomeStruct)); /* without a cast */
return((void *)(p+1)); /* will the returned pointer point
to the 2nd struct? */
Seems to me there is no guarantee it will.
你好,
指针算术根据指针的类型工作
`p''
是'SomeStruct''。
因此,返回的指针应该指向
中的2'结构
动态
分配数组。
---
问候
-PJP
http://www.cdacbangalore.in/~prasad
Hello there,
The pointer arithmatic works according to the `type'' of the pointer
`p''
that is `SomeStruct''.
So, the returned pointer should point to the 2''nd struct in the
dynamically
allocated array.
---
Regards
-PJP
http://www.cdacbangalore.in/~prasad
2006年12月28日星期四,jacob navia写道:
On Thu, 28 Dec 2006, jacob navia wrote:
为什么喝茶écrit:
Why Tea a écrit :
>下面的指针会发生什么变化?
SomeStruct * p;
p = malloc(100 * sizeof(SomeStruct)); / *没有演员* /
返回((void *)(p + 1)); / *将返回的指针指向第二个结构? * /
对我来说似乎无法保证。
/为什么茶
>What happens to the pointer below?
SomeStruct *p;
p = malloc(100*sizeof(SomeStruct)); /* without a cast */
return((void *)(p+1)); /* will the returned pointer point
to the 2nd struct? */
Seems to me there is no guarantee it will.
/Why Tea
在我看来它必须指向p + 1。由于p是SomeStruct的指针
,p + 1指向表格中的第二个结构
。
In my opinion it must point to p+1. Since p is a pointer
to SomeStruct, p+1 points to the second structure
in the table.
当然假设malloc()的有效声明在范围内(例如
通过#include< stdlib.h>) ,内存分配成功。否则,
行为未定义。
Emil
Assuming of course that a valid declaration of malloc() is in scope (e.g.
via #include <stdlib.h>), and the memory allocation succeeded. Otherwise,
the behavior is undefined.
Emil
这篇关于一个malloc问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!