![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
Which area of memory does new use?
I'm trying to get a clear understanding of what can and can't be returned from a function. If I do dynamic memory allocation with new inside a function is the memory that's allocated part of the stack or a different area where it is retained after the function ends?
I've tested this and it seems that the memory and its contents (if any) are retained after the function ends but I'm trying to understand exactly what location of memory it is in. I'm assuming it's not the stack since it isn't popped out at the function's end. |
|
|
|
|
|
#2 |
|
Programmer
|
you are right.the memo you got by 'new' is not located in stack,but in heap where is the place you can get memo dynamicly
|
|
|
|
|
|
#3 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
new, somewhere down the line, calls the OS memory manager to allocate a page/segment/etc of memory or perhaps tack on to an existing one. The pointer that is returned points to a location on the "heap", which is more or less everything in the program's address space that isn't the code or stack. The address is some value that the OS has mapped to a physical memory address, which is why you don't get a fault. You have to call delete to tell the memory manager to free the memory.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
So basically, main() has access to heap memory and functions work in stack memory space but functions access heap memory when they dynamically request memory. Is that right?
|
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
That last post sounds so grammatically incorrect.
![]() |
|
|
|
|
|
#6 |
|
Programmer
|
all functions (include main()) work in stack.we can dynamically alocate some memo in heap area,and return a pointer in stack. with this pointer we can manipulate the alocated memo.
|
|
|
|
|
|
#7 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
|
#8 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
Any code, main or another function, can access an area of memory and work with it as long as it knows where to look. This could be a pointer (heap memory) or an offset from the current stack frame base address (stack memory). To further elaborate on the stack memory, check out http://www.unixwiz.net/techtips/win32-callconv-asm.html
That gives you a rather thorough explanation of stack operation and the common calling conventions (at least in Win32, not sure which *nix uses). As you can see, everything is accessed via a pointer, except in the case of stack memory the pointer is actually the EBP register plus/minus an offset.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I've also dropped a generic explanation of stack operations in the forum a couple of times. You'd have to search. The stack is one thing from the viewpoint of the raw machine, and that same thing is MOSTLY relied upon by languages, but it is NOT a requirement of C/C++ that things must be done in the way they are frequently done. All explanations of parameter passing, etc., should bear at least a mild caveat.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#10 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
It's certainly not a requirement to use a stack and implementations do vary, but there lies the distinction between what the OP needs to know and what the complete answer is. He specifically mentioned stack and seemed to need an introductory crash course. It is still a valid and neccessary point, however, one of your trademarks, DaWei
![]()
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|