Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 4th, 2005, 8:51 PM   #1
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
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.
aznluvsmc is offline   Reply With Quote
Old Nov 4th, 2005, 9:01 PM   #2
sinix
Programmer
 
sinix's Avatar
 
Join Date: Nov 2005
Location: China
Posts: 47
Rep Power: 0 sinix is on a distinguished road
Send a message via MSN to sinix
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
sinix is offline   Reply With Quote
Old Nov 4th, 2005, 9:03 PM   #3
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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
Dameon is offline   Reply With Quote
Old Nov 4th, 2005, 9:13 PM   #4
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
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?
aznluvsmc is offline   Reply With Quote
Old Nov 4th, 2005, 9:14 PM   #5
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
That last post sounds so grammatically incorrect.
aznluvsmc is offline   Reply With Quote
Old Nov 4th, 2005, 9:44 PM   #6
sinix
Programmer
 
sinix's Avatar
 
Join Date: Nov 2005
Location: China
Posts: 47
Rep Power: 0 sinix is on a distinguished road
Send a message via MSN to sinix
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.
sinix is offline   Reply With Quote
Old Nov 4th, 2005, 9:50 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
all functions (include main()) work in stack.
Functions are code. They don't live on the stack unless you copy code into memory allocated on the stack (like a buffer). Global variables do not live on the stack. Strings allocated by "char myArray = "Test";" (and similar) do not live on the stack. A pointer returned by new (or malloc) may very well be global, and not on the stack. Please try to avoid giving bum information to a new user.
__________________
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
DaWei is offline   Reply With Quote
Old Nov 4th, 2005, 9:55 PM   #8
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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
Dameon is offline   Reply With Quote
Old Nov 4th, 2005, 9:59 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 4th, 2005, 11:34 PM   #10
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
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
Dameon is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC