tag:blogger.com,1999:blog-2349434231090974738.post6536376771060096461..comments2023-10-24T19:15:52.177+01:00Comments on Advanced C++ with Examples: Memory Management with 'new'Zahid Ghadialyhttp://www.blogger.com/profile/11618208626682295272noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-2349434231090974738.post-64774338510171141242010-11-12T17:28:37.723+00:002010-11-12T17:28:37.723+00:00Dinesh - Any dynamic memory you allocate is always...Dinesh - Any dynamic memory you allocate is always in heap. Data which is declared within a function are in the stack. So for example<br /><br />[code]<br />int main()<br />{<br /> int a , b;<br /> ..<br /> ..<br /> int *p = foo( a, b );<br />}<br /><br />int *foo( int a, int b )<br />{<br /> int *p = new int[10];<br /> return p;<br />}<br />[/code]<br /><br />In the above case variable 'a' and 'b' are declared in the stack but pointer 'p' in the function foo is been allocated in the heap. Hence the reason you can return the pointer. You cannot return when the memory is allocated in the stack. As the stack gets cleared by when it leaves the scope.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-36737484745264715592010-11-08T12:05:25.468+00:002010-11-08T12:05:25.468+00:00I think so too.I think so too.Zahid Ghadialyhttps://www.blogger.com/profile/11618208626682295272noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-77515135037678207072010-11-08T10:44:30.917+00:002010-11-08T10:44:30.917+00:00hi in this case memory will still allocate in stac...hi in this case memory will still allocate in stack. Am I correct?dineshhttps://www.blogger.com/profile/06474575114418448442noreply@blogger.com