我对程序在内存中的样子有点困惑,我的教授告诉我堆栈和堆在堆栈处于较低的内存地址时相互增长.
使用该映像困扰我的第一件事是,如果堆从高到低增长,那么如果我在堆上分配了一个数组,那么第二个元素的指针在int值中是否应该小于指向第一个元素的指针?这会令人困惑
我做了一点研究,我发现了这个数字(注意我的问题集中在linux上)
布局http://www.cyberplusindia.com/blog/wp-content/uploads/2008/10/memorysegment.gif
好吧,所以数据和未初始化的数据位于文本段之后,位于程序内存的开头,然后是堆后跟堆栈,最后是命令行参数和环境.
但是如果堆栈从高地址变为低,那么如果我在堆栈上分配了一个数组,那么如果我指向第一个元素的指针的值也会低于指向第二个元素的指针?这是不是意味着堆栈也会从低到高增长?
所以我的问题是Linux中进程的正确内存布局是什么?以及共享库的内存来自何处(在进程的地址空间中)
带指针示例的简单代码:
#include <iostream>
int data[5];
int main()
{
using std::cout;
using std::endl;
int stack = 0;
short *sptr = reinterpret_cast<short *> ( &stack );
int *iptr = new int[5];
cout << "Starting static tests"
<< "\nPointer to first element " << data
<< "\nPointer to second element " << &data[1]
<< "\nstarting stack test " << sptr
<< "\nsecond short " << &sptr[1]
<< "\nStarting heap test " << iptr
<< "\nsecond int …Run Code Online (Sandbox Code Playgroud) 我正在为Linux上的套接字编写C++包装器.我可以将读/写连接到http服务器,我的poll函数可以完美地用于读取,但由于某种原因它不能用于写入.我尝试过使用gdb,fd.revents当fd.events为时,看起来轮询设置为0POLLOUT
民意调查码:
/**
*@brief assigns request.reply based on fd.revents
*/
static void translate_request(mizaru::PollRequest &request, pollfd &fd)
{
assert( (fd.revents & POLLHUP) == 0);
assert( (fd.revents & POLLERR) == 0);
assert( (fd.revents & POLLNVAL) == 0);
switch(fd.revents)
{
case (POLLIN | POLLOUT) :
request.reply = mizaru::POLL_REPLY_RW;
break;
case POLLIN :
request.reply = mizaru::POLL_REPLY_READ;
break;
case POLLOUT :
request.reply = mizaru::POLL_REPLY_WRITE;
default :
request.reply = 0;
}
}
/**
* @fills in fd.events based on request.request
* and fd.fd …Run Code Online (Sandbox Code Playgroud)