Click here to Skip to main content
15,915,703 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: MessageBoxW and converting to LPCWSTR Pin
Milton Karimbekallil4-Sep-06 23:35
Milton Karimbekallil4-Sep-06 23:35 
QuestionUse of Select Option in Socket. Pin
uday kiran janaswamy4-Sep-06 22:47
uday kiran janaswamy4-Sep-06 22:47 
AnswerRe: Use of Select Option in Socket. Pin
_AnsHUMAN_ 4-Sep-06 23:16
_AnsHUMAN_ 4-Sep-06 23:16 
QuestionDifference in Heap and Stack. Pin
uday kiran janaswamy4-Sep-06 22:41
uday kiran janaswamy4-Sep-06 22:41 
AnswerRe: Difference in Heap and Stack. Pin
_AnsHUMAN_ 4-Sep-06 23:03
_AnsHUMAN_ 4-Sep-06 23:03 
GeneralRe: Difference in Heap and Stack. Pin
Steve S4-Sep-06 23:19
Steve S4-Sep-06 23:19 
GeneralRe: Difference in Heap and Stack. Pin
uday kiran janaswamy4-Sep-06 23:32
uday kiran janaswamy4-Sep-06 23:32 
GeneralRe: Difference in Heap and Stack. Pin
_AnsHUMAN_ 5-Sep-06 1:12
_AnsHUMAN_ 5-Sep-06 1:12 
Steve S wrote:
I always thought memory was memory!

In a computer there is only one variable storage area, the main computer RAM. But, for the purpose of organization and performance, it is divided into several areas.

When you C/C++ program is started, memory is requested to the operating system to create the heap and the stack.
Both these memory areas use what we could call the heap (not the stack). They are accessed by dereferencing pointers, like in the code below:
struct S {
int x;
int y;
int z;
};

int main(void) {
S* var_s;
var_s=new S;
if (!var_s) return __LINE__;
var_s->y=20;
}

In this code, the structure S will have a total of 12 bytes (in Win32), because each integer takes 4 bytes. The new will return some relatively unpredictable address in memory, which is not that important. The only important value to check for is zero, which means the requested memory is not available (that's why the "if" statement is there).

When your program starts, another area of memory is allocated, the stack. This memory is just like any other, but it is considered must more volatile, and besides it is also used where to go to when functions terminate. The stack is considered to be memory like "remember this for a moment while I do something else, and then I will get back to this".

1) The address of the variable var_s is always known by using the stack pointer, which is a physical register of the processor.

2) The stack is very dense. Everything is packed together, and only the memory area after the stack pointer is free. Everything else from the start of the stack up to the stack pointer is densely occupied.

3) All the relative addresses of things stored in the stack are predictable. For example, while inside main, the return address could also be known, independently from where your program came from, by reading memory addresses 5000 to 5003 (4 bytes before the address of var_s). In reality, when main begins there is more stuff added to the stack which makes the var_s ocuppy addresses further from the main return address, but still it is possible to check all variables and function return addresses by navigating through the stack.

4) Although everything is accessible in the stack, by navigating in it, new things can be memorized only after previously memorized things. The stack can contain no holes, and that is the main diference to the heap. So, only the most recent things can be forgotten go release memory for new things. This means that you cannot forget the address o contents 5000 and used for storing other things, unless you forget about var_s first.

Somethings seem HARD to do, until we know how to do them.
Wink | ;-) _AnShUmAn_

GeneralRe: Difference in Heap and Stack. Pin
Steve S5-Sep-06 1:45
Steve S5-Sep-06 1:45 
GeneralRe: Difference in Heap and Stack. Pin
_AnsHUMAN_ 5-Sep-06 2:09
_AnsHUMAN_ 5-Sep-06 2:09 
QuestionAbout the drag&drop Pin
lj_wml4-Sep-06 22:28
lj_wml4-Sep-06 22:28 
AnswerRe: About the drag&drop Pin
Michael Dunn5-Sep-06 12:46
sitebuilderMichael Dunn5-Sep-06 12:46 
GeneralRe: About the drag&drop Pin
lj_wml5-Sep-06 16:02
lj_wml5-Sep-06 16:02 
Questionmake setup Pin
milleusi4-Sep-06 22:08
milleusi4-Sep-06 22:08 
AnswerRe: make setup Pin
Hamid_RT5-Sep-06 2:36
Hamid_RT5-Sep-06 2:36 
GeneralRe: make setup Pin
milleusi5-Sep-06 7:19
milleusi5-Sep-06 7:19 
GeneralRe: make setup Pin
Hamid_RT5-Sep-06 20:35
Hamid_RT5-Sep-06 20:35 
QuestionNeed help on socket progrmming Pin
Kiran Pinjala4-Sep-06 21:55
Kiran Pinjala4-Sep-06 21:55 
AnswerRe: Need help on socket progrmming Pin
Frank K5-Sep-06 1:24
Frank K5-Sep-06 1:24 
Questioncan i post a Completion MSG from one IOCP to another? Pin
houpainansheng4-Sep-06 21:40
houpainansheng4-Sep-06 21:40 
Questionhow to save data in database Pin
With_problem4-Sep-06 21:02
With_problem4-Sep-06 21:02 
AnswerRe: how to save data in database Pin
Hamid_RT5-Sep-06 0:59
Hamid_RT5-Sep-06 0:59 
GeneralRe: how to save data in database Pin
With_problem5-Sep-06 19:11
With_problem5-Sep-06 19:11 
GeneralRe: how to save data in database Pin
Hamid_RT5-Sep-06 20:35
Hamid_RT5-Sep-06 20:35 
Questionregarding list box in vc++ Pin
haasini4-Sep-06 20:38
haasini4-Sep-06 20:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.