全局变量的问题(内存分配) [英] Problems With Global Variables (memory allocations)

查看:49
本文介绍了全局变量的问题(内存分配)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨。


请考虑以下代码。


================ ================================== ================ =======

#include< stdio.h>

#include< string.h>


/ *对于这些变量链接器仅在

内存中分配4个字节,如

DB 77 -w

DB 00

DB 00

DB 00

DB 61 -a

DB 00

DB 00

DB 00

* /


char a [] =" w";


char b [] =" a";


int main(int argc,char ** argv)

{


printf("%s",a);

/ *

复制7bytes,不应导致数据被覆盖为

声明了一个

作为char []''welc''被复制(因为已被分配4个字节)到一个但是

'' ome''写的

到b,因为它与它相邻*

/


strcpy(a,welcome);


printf("%s",b); / *输出''ome''


返回0;

}


====== ============================================ ====== ====================

i几乎所有编译器都尝试过它,并且所有编译器都产生相同的输出。

我的观点是

为什么char []只分配4bytes,因为它有空间

无限数量的字节。

解决方案

raashid bhatt< ra ********** @ gmail.comwrites:


请考虑以下代码。



< snip>


char a [] =" w";


char b [] =" a";


int main(int argc,char ** argv)

{

strcpy(a,welcome);


printf("%s",b); / *输出''ome''


返回0;

}


====== ============================================ ====== ====================

i几乎所有编译器都尝试过它,并且所有编译器都产生相同的输出。

我的观点是

为什么char []只分配了4bytes,因为它有空间

无限数量的字节。



所有编译器都应该为两个字符留出空间:w和最后一个null

字节。由于对齐,任何额外的(在你的情况下额外的2个字节)是一个

的事故。


在C中,像a和b这样的数组不能成长以适应你放入的内容

他们。你必须说他们吃了多大的声明(并且在

C90中,这个大小必须是一个常数整数表达式)。写[]是

只是编译器的一个简写,说为初始化器保留足够的空间




-

Ben。


raashid bhatt说:


嗨。


请考虑以下代码。


===================== ============================= ===================== ==

#include< stdio.h>

#include< string.h>


char a [] =" w";


char b [] =" a";


int main(int argc,char ** argv )

{


printf("%s",a);

/ *

复制7bytes,不应该导致数据被覆盖

声明为char []''welc''复制(已分配

4字节)到a''ome''写入b,因为b与它相邻* /



不,a只有两个字节es分配给它。它们是[0]和[1]。


strcpy(a,welcome);



你刚刚超过缓冲区,所以程序的行为现在是

undefined。


i几乎所有编译器都尝试过它,并且所有编译器都产生相同的输出。



由于行为未定义,任何行为都是合法的,并且编译器没有义务相互产生不同的输出;也没有

编译器是否有义务生成与每个

其他相同的输出。


我的观点是
为什么char []只分配4bytes



否。在你的示例代码中,你定义了两个数组,每个两个字节大小,

不是四个字节。


,因为它有无限数量字节的空间。



没有空间可用于无限数量的字节。即使你可以在单个电子上输入一个字节(你也不能)。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


8月26日凌晨3点20分,Richard Heathfield< r ... @ see.sig.invalidwrote:
< blockquote class =post_quotes>
>


不,a只分配了两个字节。它们是[0]和[1]。


strcpy(a,welcome);



编号在您的示例代码中,您定义了两个数组,每个字节大小为两个字节,

不是四个字节。



理查德在我拆解程序时没有分配两个字节

查看.data部分


DB 77 -w = byte1

DB 00 =字节2

DB 00 =字节3

DB 00 =字节4

DB 61-另一个变量即b

DB 00

DB 00

DB 00和b


Hi.

Consider the Following Code.

================================================== =======================
#include <stdio.h>
#include <string.h>

/* For these Variables The Linker Only Allocates 4 Bytes each in the
memory Like
DB 77 -w
DB 00
DB 00
DB 00
DB 61 -a
DB 00
DB 00
DB 00
*/

char a[] = "w";

char b[] = "a";

int main(int argc, char **argv)
{

printf("%s",a);
/*
Copy 7bytes which should not cause data to be overwritten as a have
declared a
as a char[] ''welc'' copied(as a has been allocated 4 bytes) to a but
''ome'' Get written
to b as b is adjacent to it*
/

strcpy(a, "welcome");

printf("%s", b); /* output ''ome''

return 0;
}

================================================== ==========================

i tried it with nearly all compilers and all produced the same output.
my point is
why for a char[] only 4bytes are allocated while as it has space for
unlimited num of bytes.

解决方案

raashid bhatt <ra**********@gmail.comwrites:

Consider the Following Code.

<snip>

char a[] = "w";

char b[] = "a";

int main(int argc, char **argv)
{
strcpy(a, "welcome");

printf("%s", b); /* output ''ome''

return 0;
}

================================================== ==========================

i tried it with nearly all compilers and all produced the same output.
my point is
why for a char[] only 4bytes are allocated while as it has space for
unlimited num of bytes.

All compilers should make space for two characters: the ''w'' and a null
byte at the end. Any extra (in your case the extra 2 bytes) are an
accident due to alignment.

In C, arrays like a and b can''t grow to accommodate what you put in
them. You have to say how big they are ate the declaration (and in
C90 that size must be a constant integer expression). Writing [] is
simply a short hand to the compiler that says "reserve enough space
for the initialiser".

--
Ben.


raashid bhatt said:

Hi.

Consider the Following Code.

================================================== =======================
#include <stdio.h>
#include <string.h>

char a[] = "w";

char b[] = "a";

int main(int argc, char **argv)
{

printf("%s",a);
/*
Copy 7bytes which should not cause data to be overwritten as a have
declared a as a char[] ''welc'' copied(as a has been allocated
4 bytes) to a but ''ome'' Get written to b as b is adjacent to it*/

No, a has just TWO bytes allocated to it. They are a[0] and a[1].

strcpy(a, "welcome");

You just overran the buffer, so the behaviour of the program is now
undefined.

i tried it with nearly all compilers and all produced the same output.

Since the behaviour is undefined, any behaviour is legal, and there is no
obligation on compilers to produce different outputs from each other; nor
is there any obligation on compilers to produce the same outputs as each
other.

my point is
why for a char[] only 4bytes are allocated

No. In your example code, you defined two arrays, each two bytes in size,
not four bytes.

while as it has space for unlimited num of bytes.

Nothing has space for an unlimited number of bytes. Not even if you could
fit a byte on a single electron (which you can''t).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


On Aug 26, 3:20*am, Richard Heathfield <r...@see.sig.invalidwrote:

>

No, a has just TWO bytes allocated to it. They are a[0] and a[1].

strcpy(a, "welcome");


No. In your example code, you defined two arrays, each two bytes in size,
not four bytes.


Richard Two bytes arent allocated when i disassemble the program
looking at .data section

DB 77 -w = byte1
DB 00 = byte 2
DB 00 = byte 3
DB 00 = byte 4
DB 61 -a another variable ie b
DB 00
DB 00
DB 00 and same 4 bytes for b


这篇关于全局变量的问题(内存分配)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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