我可以在分配的内存上做任何事吗? [英] Can I do ANYTHING on allocated memory?

查看:73
本文介绍了我可以在分配的内存上做任何事吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下任何一种情况是否构成未定义的行为?一旦我分配内存,

我可以用它做任何我想做的事情(例如,使用内存分配为

char *,作为我存储整数的内存)?


#include< stdio.h>

#include< stdlib.h>


int main (无效){

char * a;

int * x,* y;

a =(char *)malloc(2 * sizeof( int));

//这里是'我想知道的部分

x =(int *)a;

y = (int *)a + sizeof(int);

* x = 10;

* y = 20;

printf("% d%d \ n",* x,* y);

返回0;

}


-

世界上很容易接受世界的观点;孤独轻松

住在我们自己之后;但伟大的人就是在人群中间的人。

保持完美的甜蜜独处的孤独。

Ralph Waldo Emerson,自力更生1841年/> http://pinpoint.wordpress.com/

Does any of the following constitute undefined behavior? Once I allocate memory,
can I do with it whatever I want (as in the example, use memory allocated as
char *, as memory where I store integers)?

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

int main(void) {
char *a;
int *x, *y;
a = (char *) malloc(2 * sizeof(int));
// here''s the part I''m wondering about
x = (int *) a;
y = (int *) a + sizeof(int);
*x = 10;
*y = 20;
printf ("%d %d\n", *x, *y);
return 0;
}

--
"It is easy in the world to live after the world''s opinion; it easy in solitude
to live after our own; but the great man is he who in the midst of the crowd
keeps with perfect sweetness the independence of solitude."
Ralph Waldo Emerson, Self-reliance 1841
http://pinpoint.wordpress.com/

推荐答案



" Sourcerer" < en **** @ MAKNIgmail.com在消息中写了

"Sourcerer" <en****@MAKNIgmail.comwrote in message

以下任何一种情况是否构成未定义的行为?一旦我分配了

内存,我可以随心所欲地使用它(例如,使用内存

分配为char *,作为存储整数的内存)?


#include< stdio.h>

#include< stdlib.h>


int main (无效){

char * a;

int * x,* y;

a =(char *)malloc(2 * sizeof( int));

//这里是'我想知道的部分

x =(int *)a;

y = (int *)a + sizeof(int);

* x = 10;

* y = 20;

printf("% d%d \ n",* x,* y);

返回0;

}
Does any of the following constitute undefined behavior? Once I allocate
memory, can I do with it whatever I want (as in the example, use memory
allocated as char *, as memory where I store integers)?

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

int main(void) {
char *a;
int *x, *y;
a = (char *) malloc(2 * sizeof(int));
// here''s the part I''m wondering about
x = (int *) a;
y = (int *) a + sizeof(int);
*x = 10;
*y = 20;
printf ("%d %d\n", *x, *y);
return 0;
}



你正在使用C型系统快速和松散地玩耍。

它可以帮助你避免对齐问题和其他恶意,但是

用于向后兼容和某些低 - 等级操作,C还给你

你可以自由地搞砸它。

技术上我认为你没关系因为指向char可以转换为

任何其他类型。这是从没有空格的时候的宿醉,因此使用了char * s代替。

但是你投了一个短*,然后回来,它本来是

实施定义的行为。在绝大多数机器上int *和

short * s是相同的,而演员阵容是无操作。然而,只有可能

,short *会抛出一些int *需要的东西,所以符合要求的b $ b实现可以做任何它想要的响应。

You are playing fast and loose with the C type system.
It is there to help you avoid alignment problems and other nasties, however
for backwards compatibility and certain low-level operations, C also gives
you the freedom to mess about with it.
Technically I think you are OK because pointers to char can be converted to
any other type. This is a hangover from the days when there were no void
pointers, so char *s were used instead.
However had you cast to a short *, and back, it would have been
implentation-defined behaviour. On the vast majority of machines int * and
short *s are the same and the cast is a no op. However it is just possible
that the short * throws some bits away which an int * needs, so a conforming
implementation could do anything it wants in response.


在文章< eo ********** @ ss408.t-com.hr>中,

Sourcerer< en * ***@MAKNIgmail.com写道:
In article <eo**********@ss408.t-com.hr>,
Sourcerer <en****@MAKNIgmail.comwrote:

>一旦我分配了内存,我可以随心所欲地使用它(如示例所示,使用内存分配为
char *,作为存储整数的内存)?
>Once I allocate memory,
can I do with it whatever I want (as in the example, use memory allocated as
char *, as memory where I store integers)?



你没有把它分配为char *:

You didn''t allocate it as char *:


a =(char *)malloc( 2 * sizeof(int));
a = (char *) malloc(2 * sizeof(int));



您刚刚分配了一些内存,并且(不必要地)将其转换为char *

,然后再将其分配给。 malloc()返回的内存并不是
有类型。


你可以存储你喜欢的任何东西,只要它适合你和你

尊重对齐规则。

You just allocated some memory, and (unnecessarily) cast it to char *
before assigning it to a. The memory returned by malloc() doesn''t
have a type.

You can store whatever you like in it, provided that it fits and you
respect the alignment rules.


x =(int *)a;
x = (int *) a;



罚款。


但你在这里有一个错误:

Fine.

But you have a mistake here:


y =(int *)a + sizeof(int);
y = (int *) a + sizeof(int);



您已将a转换为整数指针,然后将sizeof(int)添加到

。如果sizeof(int)是4,那么你刚刚添加了四个int大小,

即16字节。添加指针会增加

指向对象大小的单位,所以你想要


y =(int *)(a + sizeof( int));



y =(int *)a + 1;


- Richard

-

在一些字母表中需要考虑多达32个字符

- 1963年的X3.4。

You have converted a to an integer pointer, then added sizeof(int) to
it. If sizeof(int) is 4, you have just added four int sizes to it,
i.e. 16 bytes. Adding to a pointer adds in units of the size of the
pointed-to object, so you want

y = (int *) (a + sizeof(int));
or
y = (int *) a + 1;

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.


文章< lK ********************* @ bt .com>,

Malcolm McLean< re ******* @ btinternet.comwrote:
In article <lK*********************@bt.com>,
Malcolm McLean <re*******@btinternet.comwrote:

>你在玩使用C型系统快速而松散。
>You are playing fast and loose with the C type system.



不,除了他添加的错误,他正在使用指针

正确。

No, apart from his mistake with the addition, he is using pointers
correctly.


>技术上我认为你没事,因为指向char的指针可以转换为
任何其他类型。这是从没有无效指针的日子开始的宿醉,所以使用char * s代替。
>Technically I think you are OK because pointers to char can be converted to
any other type. This is a hangover from the days when there were no void
pointers, so char *s were used instead.



void *已将char *替换为通用指针类型,但你不能在$ * b b上对void *指针进行算术运算。 char *(或unsigned char *)是用于以字节为单位访问内存的

类型,它是C存储的单位。


C保证无效*和char *具有相同的表示形式,但

OP不依赖于此。


- Richard

- -

考虑在一些字母表中需要多达32个字符

- 1963年的X3.4。

void * has replaced char * as the generic pointer type, but you can''t
do arithmetic on void * pointers. char * (or unsigned char *) is the
type used to access memory as bytes, which are the units of C storage.

C guarantees that void * and char * have the same representation, but
the OP is not relying on that.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.


这篇关于我可以在分配的内存上做任何事吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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