关于内存分配的问题?? [英] Question about memory allocation??

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

问题描述

大家好,


在GCC中运行以下程序时,我非常沮丧。


main()

{

char * ptr1;

char arr [1];

int i;

char * ptr2;


ptr1 =(char *)malloc(1);

ptr2 =(char *)malloc(1);

printf(" \\\
& ptr1:%u \ n",& ptr1); / *我得到了输出

喜欢,000053

printf(" \ n address of arr:%u \ n",arr); / *显示000052

printf(我的地址:i:%u \ n,& i); / *显示000045

printf(" \ n& ptr2:%u \ n",& ptr2); / *显示000041

}


**为什么内存从下到上分配? (按顺序

ptr2,i,arr和ptr1)


**整数只占用4个字节,占用000045到000048。 />

** 000049到000051之间的内存发生了什么?


有谁能告诉我为什么会发生这种魔法?

提前致谢,

Sethu

解决方案

se ***** @ gmail.com 写道:


大家好,


在GCC中运行以下程序时,我非常沮丧。



这是不可能的。你使用GCC编译你的程序,你在

中运行它无论你使用什么操作系统。


main()



旧式定义。最好明确:int main(void)


{

char * ptr1;

char arr [ 1];

int i;

char * ptr2;


ptr1 =(char *)malloc(1);

ptr2 =(char *)malloc(1);



除非你知道你在做什么,否则不要施放malloc的结果。你不是
。摆脱没有它的警告的正确方法

包括< stdlib.h> ;.


printf( " \ n& ptr1:%u \ n",& ptr1);



如果你使用printf,你需要一个合适的原型。包含< stdio.h> ;.

修复后,%u是unsigned int的格式说明符。

不是任何类型指针的格式说明符。打印

指针的方法是将它们转换为void *,然后使用printf'的%p格式

说明符。


/ *我有输出

喜欢,000053



/ *评论需要匹配* /。


printf(" \ n address of arr:%u \ n",arr); / *显示000052

printf(我的地址:i:%u \ n,& i); / *显示000045

printf(" \ n& ptr2:%u \ n",& ptr2); / *显示000041

}


**为什么内存从下到上分配? (按顺序

ptr2,i,arr和ptr1)



因为这是你的操作系统的工作方式。至于你的

操作系统为什么这样工作,可能是因为恰好是你的处理器类型最有效的方法,它可能是为了
与其他系统的兼容性,可能是因为它更容易实现,或者可能是由于其他许多原因造成的。


**整数只占用4个字节,它占用000045到000048.



这是可能的,但极不可能使用的地址是

实际上是45到48.更有可能的是你因为你对编译器撒谎而误导了b $ b输出。你得到不同的地址

与%p?


** 000049到000051之间的内存发生了什么?



它没有被使用。


有谁能告诉我为什么这个魔法会发生?



这几乎不是魔术。这是因为你的编译器决定浪费三个字节的内存比处理因使用它而引起的问题更好的想法。这些问题从处理器到处理器,甚至从操作系统到操作系统都有所不同,但是一般来说,如果

编译器不确保某些类型的变量未存储

在特定地址。哪些类型和地址再次是特定于您的处理器和操作系统的



se ***** @ gmail.com 说:


各位大家好,


在GCC中运行以下程序时,我非常困惑。


main()

{

char * ptr1;

char arr [1];

int i;

char * ptr2;


ptr1 =(char *)malloc(1);



未定义的行为:你忘记了malloc的原型。在标题为malloc的主题中查看最近的

comp.lang.c讨论。


ptr2 =(char *)malloc(1 );


printf(" \\\
& ptr1:%u \ n",& ptr1); / *我有输出

喜欢,000053



未定义的行为:(a)你忘记了printf的原型,(b)你

在期望unsigned int时为printf指定了一个指针值。每个

这些都足以让程序以任何方式表现



**为什么内存从下到上分配? (按顺序

ptr2,i,arr和ptr1)



为什么不应该?
< blockquote class =post_quotes>
**整数只占用4个字节,占用000045到000048.


** 000049到000051之间的内存发生了什么??



编译器通常会在给定(通常是小)整数的

的倍数的边界上对齐指针。


有谁能告诉我为什么这个魔法会发生?



当你违反C规则时,任何事情都可能发生。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。


se ***** @ gmail.com 写道:


大家好,


在GCC中运行以下程序时,我非常沮丧。



我不明白为什么。 (拧紧在这种情况下通常意味着它破了,

,这是一件非常糟糕的事情。)


main ()



`int main(void)`是首选;对于C99,`int`是/必要/。


{

char * ptr1;

char arr [1];

int i;

char * ptr2;


ptr1 =(char *)malloc(1) ;



你应该#include< stdlib.hif你正在进行商业化。并且`(char *)`是
不必要(和不明智的)。我也会使用`malloc(sizeof(* ptr1))`因为这是一般安全模式的一部分。


ptr2 =(char *)malloc(1);


printf(" \\\
& ptr1:%u \ n",& ptr1); / *我有输出

喜欢,000053



你应该#include< stdio.hif你正在打印。


你不走运。 `& ptr1`是指针值,而不是整数:你

应该使用'%p`格式并强制转换为`(void *)`以确保安全

便携性。


否则你会得到未定义的行为:关于你的代码的所有赌注

的行为都已关闭。


printf(" \ n地址:arr:%u \ n",arr); / *显示000052

printf(我的地址:i:%u \ n,& i); / *显示000045

printf(" \ n& ptr2:%u \ n",& ptr2); / *显示000041



同上。


}


**为什么从底部到顶部分配内存? (按顺序

ptr2,i,arr和ptr1)



为什么不呢?放置顺序是编译器的业务。它可以按照它认为方便的顺序分配
。也许它正在堆栈上分配

。其中向下生长。谁在乎? [1]


**整数只占用4个字节,占用000045到000048.


**发生了什么事到000049到000051之间的内存?



我预计它会被对齐问题吃掉。


有谁可以告诉我为什么这样做魔法发生了?



这是C.有/没有魔法/(除非未定义

行为允许)。

编译器将按方向分配局部变量,并按指示分配高效率。通常你不需要关心。


[1] /有些/人关心。通常他们有义务使用一些具体的实施,并确切知道他们为什么要关心。


-

Chris" ; Perikles胜利 Dollin

我们没有时间找到我们想知道的一切。

- James Blish,/一个Cy钹的冲突/


Hi everybody,

While running the following program in GCC, i''m very much screwed.

main()
{
char *ptr1;
char arr[1];
int i;
char *ptr2;

ptr1 = (char *)malloc(1);
ptr2 = (char *)malloc(1);
printf("\n & ptr1 : %u\n", &ptr1); /* I got outputs
like, 000053

printf("\n address of arr : %u\n", arr); /* shows 000052

printf("\n address of i : %u\n", &i); /* shows 000045

printf("\n & ptr2 : %u\n", &ptr2); /* shows 000041
}

**Why the memory is allocated from bottom to top?? ( in the order
ptr2, i , arr, and ptr1)

**Integer takes only 4-bytes, it occupies 000045 to 000048.

**What happened to the memory between 000049 to 000051 here??

Can anybody tell me why this magic is happened??

Thanks in advance,
Sethu

解决方案

se*****@gmail.com wrote:

Hi everybody,

While running the following program in GCC, i''m very much screwed.

That''s not possible. You compile your program with GCC, you run it in
whatever operating system you are using.

main()

Old-style definition. Better to be explicit: int main(void)

{
char *ptr1;
char arr[1];
int i;
char *ptr2;

ptr1 = (char *)malloc(1);
ptr2 = (char *)malloc(1);

Don''t cast malloc''s result unless you know what you''re doing. You
don''t. The proper way to get rid of the warning that you get without it
is to include <stdlib.h>.

printf("\n & ptr1 : %u\n", &ptr1);

If you use printf, you need a proper prototype. Include <stdio.h>.
After fixing that, %u is the format specifier for unsigned int. It is
not the format specifier for any type of pointer. The way to print
pointers is to convert them to void *, and then use printf''s %p format
specifier.

/* I got outputs
like, 000053

/* comments require a matching */.

printf("\n address of arr : %u\n", arr); /* shows 000052

printf("\n address of i : %u\n", &i); /* shows 000045

printf("\n & ptr2 : %u\n", &ptr2); /* shows 000041
}

**Why the memory is allocated from bottom to top?? ( in the order
ptr2, i , arr, and ptr1)

Because that is the way your operating system works. As for why your
operating system works that way, it could be because that happens to be
the most efficient method for your processor type, it could be for
compatibility with other systems, it could be because it was easier to
implement, or it could be for any number of other reasons.

**Integer takes only 4-bytes, it occupies 000045 to 000048.

It''s possible but extremely unlikely that the addresses used are
actually 45 through 48. What''s more likely is that you get misleading
output because you lied to the compiler. Do you get different addresses
with %p?

**What happened to the memory between 000049 to 000051 here??

It''s not used.

Can anybody tell me why this magic is happened??

It''s hardly magic. It''s because your compiler decided it would be a
better idea to waste three bytes of memory than to deal with the
problems caused by using it. These problems vary from processor to
processor, and even from operating system to operating system, but
generally speaking, programs can crash or run much more slowly if the
compiler does not make sure variables of certain types are not stored
at specific addresses. Which types and which addresses are again
specific to your processor and operating system.


se*****@gmail.com said:

Hi everybody,

While running the following program in GCC, i''m very much screwed.

main()
{
char *ptr1;
char arr[1];
int i;
char *ptr2;

ptr1 = (char *)malloc(1);

Undefined behaviour: you forgot the prototype for malloc. See recent
comp.lang.c discussion in the thread entitled "malloc".

ptr2 = (char *)malloc(1);
printf("\n & ptr1 : %u\n", &ptr1); /* I got outputs
like, 000053

Undefined behaviour: (a) you forgot the prototype for printf, and (b) you
gave a pointer value to printf when it was expecting an unsigned int. Each
of these is sufficient cause for the program to behave in any way
whatsoever.

**Why the memory is allocated from bottom to top?? ( in the order
ptr2, i , arr, and ptr1)

Why shouldn''t it be?

**Integer takes only 4-bytes, it occupies 000045 to 000048.

**What happened to the memory between 000049 to 000051 here??

Compilers will often align pointers on boundaries that are multiples of a
given (typically small) integer.

Can anybody tell me why this magic is happened??

When you break the rules of C, anything can happen.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


se*****@gmail.com wrote:

Hi everybody,

While running the following program in GCC, i''m very much screwed.

I don''t see why. ("Screwed" in this context usually means "it broke,
and this was a Very Bad Thing".)

main()

`int main(void)` is preferred; and the `int` is /necessary/ for C99.

{
char *ptr1;
char arr[1];
int i;
char *ptr2;

ptr1 = (char *)malloc(1);

You should #include <stdlib.hif you''re mallocating. And the `(char*)` is
unnecessary (and unwise). I''d also use `malloc(sizeof(*ptr1))` because that''s
part of a general safe pattern.

ptr2 = (char *)malloc(1);
printf("\n & ptr1 : %u\n", &ptr1); /* I got outputs
like, 000053

You should #include <stdio.hif you''re printfing.

You were unlucky. `&ptr1` is a pointer value, not an integer: you
should use the `%p` format and cast to `(void*)` to ensure safe
portability.

Otherwise you get Undefined Behaviour: all bets about your code''s
behaviour are off.

printf("\n address of arr : %u\n", arr); /* shows 000052

printf("\n address of i : %u\n", &i); /* shows 000045

printf("\n & ptr2 : %u\n", &ptr2); /* shows 000041

Dittoes.

}

**Why the memory is allocated from bottom to top?? ( in the order
ptr2, i , arr, and ptr1)

Why not? The order of placement is the compiler''s business. It can
allocate in whatever order it finds convenient. Maybe it''s allocating
on a "stack" which "grows downwards". Who cares? [1]

**Integer takes only 4-bytes, it occupies 000045 to 000048.

**What happened to the memory between 000049 to 000051 here??

I expect it''s been eaten by alignment issues.

Can anybody tell me why this magic is happened??

This is C. There is /no magic/ (except as permitted by Undefined
Behavior).

The compiler will allocate local variables as convenient and
efficient as directed. Usually you don''t need to care.

[1] /Some/ people care. Usually they''re obliged to use some specific
implementation and know exactly why they care.

--
Chris "Perikles triumphant" Dollin
"We did not have time to find out everything we wanted to know."
- James Blish, /A Clash of Cymbals/


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

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