存储分配? [英] storage allocation?

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

问题描述




是否在C ++中定义了两个相同类型的数组在

之后定义了一个在内存中是连续的?


喜欢说:


int a1 [10];

int a2 [10];


意味着(a1 + 10 == a2)?


用C ++思考似乎意味着如此但我不认为它对GCC来说是真实的/>
3.3.6。


例如,查看TIC ++第二版第3章的练习18。在

我的GCC 3.3.6案例中,我得到了这个:


================ br />
#include< iostream>

使用命名空间std;


int main()

{

int a1 [10];

int a2 [10];


cout<< a1<< endl;

cout<< a2<< endl;

cout<< (a2-a1)<< endl;

}

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

输出:

0xbfb9c0f0

0xbfb9c0c0

-12

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

给出了什么?


谢谢,

Tim。

Hi,

Is it defined in C++ that 2 arrays of the same type defined one after the
other are contiguous in memory?

like saying:

int a1[10];
int a2[10];

implies (a1 + 10 == a2)?

Thinking in C++ seems to imply so but I''m not finding it to be true with GCC
3.3.6 .

For example, check the exercise 18 of chapter 3 of TIC++ second edition. in
my case with GCC 3.3.6, I get this:

================
#include <iostream>
using namespace std;

int main()
{
int a1[10];
int a2[10];

cout << a1 << endl;
cout << a2 << endl;
cout << (a2 - a1) << endl;
}
================
output:
0xbfb9c0f0
0xbfb9c0c0
-12
================

What gives?

Thanks,
Tim.

推荐答案

Timothee Groleau写道:
Timothee Groleau wrote:


是否在C ++中定义了2个相同类型的数组在
其他在记忆中是连续的吗?


不,除非它们本身都是数组的元素。

喜欢说:

int a1 [10];
int a2 [10];

暗示(a1 + 10 == a2)?


No.

用C ++思考似乎意味着这一点,但我不认为GCC 3.3.6是真的。

例如,查看TIC ++第二版第3章的练习18。
在我的GCC 3.3.6案例中,我得到了这个:

== ==============
#include< iostream>
使用命名空间std;

int main()
{
int a1 [10];
int a2 [10];

cout<< a1<< endl;
cout<< a2<< endl;
cout<< (a2-a1)<< endl;
}
================
输出:
0xbfb9c0f0
0xbfb9c0c0
-12 <什么给出了?
Hi,

Is it defined in C++ that 2 arrays of the same type defined one after the
other are contiguous in memory?
No, unless they are themselves both elements of an array.
like saying:

int a1[10];
int a2[10];

implies (a1 + 10 == a2)?
No.
Thinking in C++ seems to imply so but I''m not finding it to be true with
GCC 3.3.6 .

For example, check the exercise 18 of chapter 3 of TIC++ second edition.
in my case with GCC 3.3.6, I get this:

================
#include <iostream>
using namespace std;

int main()
{
int a1[10];
int a2[10];

cout << a1 << endl;
cout << a2 << endl;
cout << (a2 - a1) << endl;
}
================
output:
0xbfb9c0f0
0xbfb9c0c0
-12
================

What gives?




所以这本书说的最后一行必须是10?我希望不是。



So the book says that the last line must be 10? I hope not.


这取决于你的编译器,我在vc6.0中得到了这个:

------- -------------------

0012ff24

0012ff4c

10

--------------------------

运行上面的程序。
It does depends on your compiler,i have got this in vc6.0:
--------------------------
0012ff24
0012ff4c
10
--------------------------
run the program above.


Timothee Groleau写道:
Timothee Groleau wrote:
在C ++中是否定义了两个相同类型的数组在内存中连续的其他数组之后定义了一个?

喜欢说:

int a1 [10];
int a2 [10];

暗示(a1 + 10 == a2)?


不,结果取决于具体情况。

在一个区块内:

{

int a1 [10];

int a2 [10];

}

a1,a2将在堆栈中分配,并满足内存对齐

要求。

堆栈帧将取决于编译器和硬件平台。


但是对于类或结构

A级
{

公开:

int a1 [10];

int a2 [10];

}


A obj; //对象的内存将从堆中分配。


ADDRESS(obj.a1)+ 0x28 = ADDRESS(obj.a2)


int * p_a1 = obj.a1;

int * p_a2 = obj.a2;


* p_a1 + 10 == * p_a2; //这里是真的,而不是obj.a1 + 10 == obj.a2。

用C ++思考似乎意味着如此但我不认为GCC是真的
3.3.6。

例如,查看TIC ++第二版第3章的练习18。在我使用GCC 3.3.6的情况下,我得到了这个:

================
#include< iostream> ;
使用命名空间std;

int main()
int a1 [10];
int a2 [10];

cout<< a1<< endl;
cout<< a2<< endl;
cout<< (a2-a1)<< endl;
}
================
输出:
0xbfb9c0f0
0xbfb9c0c0
-12
================

是什么给出的?
Is it defined in C++ that 2 arrays of the same type defined one after the
other are contiguous in memory?

like saying:

int a1[10];
int a2[10];

implies (a1 + 10 == a2)?
No, the result will depend on the context.
In one block:
{
int a1[10];
int a2[10];
}
a1, a2 will be allocated in stack, and meet memory alignment
requirement.
the stack frame will depend on the compiler and hardware platform.

But for class or struct
class A
{
public:
int a1[10];
int a2[10];
}

A obj; //the memory of object will be allocated from heap.

ADDRESS( obj.a1) + 0x28 = ADDRESS( obj.a2)

int *p_a1 = obj.a1;
int *p_a2 = obj.a2;

*p_a1 + 10 == *p_a2 ; // here it is true, not obj.a1+10 == obj.a2.

Thinking in C++ seems to imply so but I''m not finding it to be true with GCC
3.3.6 .

For example, check the exercise 18 of chapter 3 of TIC++ second edition. in
my case with GCC 3.3.6, I get this:

================
#include <iostream>
using namespace std;

int main()
{
int a1[10];
int a2[10];

cout << a1 << endl;
cout << a2 << endl;
cout << (a2 - a1) << endl;
}
================
output:
0xbfb9c0f0
0xbfb9c0c0
-12
================

What gives?




见上面的解释。



see the above explain.


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

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