指针/内存分配 [英] pointer / memory allocation

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

问题描述

如果我有以下代码:

int* p;
int w=10;
p=&10;

wp的内存分配将仅在堆栈上吗?

Will the memory allocation for both w and p will be on stack only?

如果我错了,请纠正我,因为我是C ++的新手,请让我知道何时使用指针动态分配内存而不是在堆栈上分配内存真的有用吗?

Please correct me if im wrong as I''m a newbie to C++, and please let me know when it is really useful to allocate memory dynamically using a pointer rather than allocating on the stack?

推荐答案

kumar sanghvi写道:
kumar sanghvi wrote:

int* p; //declared pointer to int (statement does nothing)<br />int w=10; //declares and initializes an int to 10, but it won''t exist unless you use it<br />p=&10; //this is an error, you probably meant p=&w?


p=&w如果使用了p,则可以:将(如果还没有)放在栈上(以前可能存放在寄存器中),然后将p设置为该地址(例如lea esi, dword ptr[esp+24]),但是如果p上的操作足够简单(对于特定的实现,不是每个理论上足够简单的操作序列)都可以在编译时进行评估,则可能会将其优化为怪异和/或令人恐惧的事物.

但是可以认为它们在堆栈中是很好的,编译器将保证在您每次进行此假设时该假设都是正确的(并且,如果不这样做,也就没有关系了) .如果您想知道的话,它们肯定不在堆中.


请注意:如果您真的想写一些大篇幅的文章,可以避免写一些大篇幅的文章以避免它们写得太长了.想知道每个小细节,您可以在 comp.lang.c ++ [ ^ ]上问同样的问题,他们将很乐意告诉您每一个学究的小细节.而且我不保证本文中的任何内容都是真实的,使用信息时需要您自担风险(等).


p=&w would do, assuming p is used: give (if it didn''t have one yet) w a place on the stack (previously it would probably reside in a register), and set p to that address (eg lea esi, dword ptr[esp+24]), but if the operations on p are simple enough (for the specific implementation, not every theoretically simple enough sequence of operations) to evaluate at compile time it may be optimized to something weird and/or scary.

But it''s fine to think of them as being on the stack, the compiler will guarantee that this assumption is true whenever you make it (and if you don''t make it, it doesn''t matter). They''re definitely not on the heap, if that''s what you were wondering about.


notes: many simplifications and omissions were made to avoid writing a king-sized post, if you really want to know every little detail you could ask the same question on comp.lang.c++[^], they will be happy to tell you every pedantic little detail. And I don''t guarantee that anything in this post was actually true, use the information at your own risk (etc etc)






kumar sanghvi写道:
kumar sanghvi wrote:

<br />int *p;   //With this statement, you just declared a pointer variable (which will point to an integer)<br />int w=10; //You declared an integer and initialized it. The storage for this variable is on the stack.<br />p=&w;    //You made the integer pointer you earlier declared, point to the integer that you just created on the stack.



w和p的内存分配都将打开仅堆栈...?



the memory allocation for both w and p will be on stack only ...?



是的,在这种情况下,指针指向堆栈上的内存位置.



Yes, in this case, the pointer is pointing to a memory location, which is on the stack.

kumar sanghvi写道:
kumar sanghvi wrote:

当使用指针动态分配内存比在堆栈上分配内存真正有用时..?

when it is really useful to allocate memory dynamically using pointer than allocating on the stack..?



指针更有用当您需要动态分配内存时(通常您不知道需要多少内存).



Pointers are more useful when you need to allocate memory dynamically (typically you don''t know how much memory you are going to need).


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

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