473,732 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stack variables that don't die?

I've seen code that allocates an object on the stack and then saves a
class reference to it.

example:
void ScribbleArea::r esizeImage( const QSize &newSize)
{
QImage newImage( newSize, );
*m_image = newImage; // m_image is a QImage
}

Now, when c++ leaves the function, I guess the stack memory for
newImage
is not reclaimed, because the object is still referenced?

If this is correct, when, if ever. is the memory in the stack
reclaimed?

If this function were called many times, woudn't this lead to
leaks in the stack, if that is possible?

Oct 17 '06 #1
7 1876
Dougan wrote:
I've seen code that allocates an object on the stack and then saves a
class reference to it.

example:
void ScribbleArea::r esizeImage( const QSize &newSize)
{
QImage newImage( newSize, );
*m_image = newImage; // m_image is a QImage
}

Now, when c++ leaves the function, I guess the stack memory for
newImage
is not reclaimed, because the object is still referenced?
No, it will be reused, but you have made a copy of it.
If this is correct, when, if ever. is the memory in the stack
reclaimed?
This can't leak memory, you don't use the heap, unless QImage leaks memory.

--
Ian Collins.
Oct 17 '06 #2

Thanks very much, I see now where I was confused.

Oct 17 '06 #3

Ian Collins wrote:
Dougan wrote:
I've seen code that allocates an object on the stack and then saves a
class reference to it.

example:
void ScribbleArea::r esizeImage( const QSize &newSize)
{
QImage newImage( newSize, );
*m_image = newImage; // m_image is a QImage
no, m_image is a pointer and we don't know if thats an allocated
pointer.
newImage is a temporary QImage.
}

Now, when c++ leaves the function, I guess the stack memory for
newImage
is not reclaimed, because the object is still referenced?
No, it will be reused, but you have made a copy of it.
Are you sure? What if the member pointer was never allocated? That
would be a copy into unreserved memory. So between resizes, the
temporary image would be copied into unprotected memory?
Please tell me i'm wrong. Otherwise, thats undefined behaviour and one
very nasty bug to crack.
>
If this is correct, when, if ever. is the memory in the stack
reclaimed?
This can't leak memory, you don't use the heap, unless QImage leaks memory.

--
Ian Collins.
Oct 17 '06 #4
Salt_Peter wrote:
Ian Collins wrote:
>>Dougan wrote:
>>>
Now, when c++ leaves the function, I guess the stack memory for
newImage
is not reclaimed, because the object is still referenced?

No, it will be reused, but you have made a copy of it.


Are you sure? What if the member pointer was never allocated? That
would be a copy into unreserved memory. So between resizes, the
temporary image would be copied into unprotected memory?
Please tell me i'm wrong. Otherwise, thats undefined behaviour and one
very nasty bug to crack.
Who knows? I was assuming the pointer was valid and the OP was simply
replacing the object. Otherwise the operation does invoke UB.
--
Ian Collins.
Oct 17 '06 #5

Ian Collins wrote:
Salt_Peter wrote:
Ian Collins wrote:
>Dougan wrote:
Now, when c++ leaves the function, I guess the stack memory for
newImage
is not reclaimed, because the object is still referenced?
No, it will be reused, but you have made a copy of it.

Are you sure? What if the member pointer was never allocated? That
would be a copy into unreserved memory. So between resizes, the
temporary image would be copied into unprotected memory?
Please tell me i'm wrong. Otherwise, thats undefined behaviour and one
very nasty bug to crack.
Who knows? I was assuming the pointer was valid and the OP was simply
replacing the object. Otherwise the operation does invoke UB.
--
Ian Collins.
After verification and for the record, Qt's recommendation is to have
class ScribbleArea hold a QImage component, not a pointer. Now that
makes sense to me if QImage has the appropriate copy constructor. Hmm.
http://doc.trolltech.com/4.1/widgets...blearea-h.html

To the OP, pointers - unless proven otherwise - point to nothing at all.

Oct 17 '06 #6
Dougan wrote:
I've seen code that allocates an object on the stack and then saves a
class reference to it.
Does it?
>
example:
void ScribbleArea::r esizeImage( const QSize &newSize)
{
QImage newImage( newSize, );
*m_image = newImage; // m_image is a QImage
}
This can't possibly save a reference, unless m_image is a pointer to a
class object which has an overloaded assignment operator (a member or
non-member one). That is to say:

// Suppose this is m_image:
SomeObject *m_image;

// And suppose this the assignment op
void SomeObject::ope rator = (QImage &ref);

Similarly, m_image could be a smart pointer rather than an ordinary
pointer, with the same effect.

The only other way a reference could be taken would be if the
expression *m_image were in fact a reference. That would mean that
m_image is a pointer to a reference. But, you probably know that there
is no such thing in C++: you can't point at a reference.
Now, when c++ leaves the function, I guess the stack memory for
newImage
is not reclaimed, because the object is still referenced?
The C++ language does not have lexical closures, nor does the C++
standard mandate the support for garbage collection. Any use of an
non-static local object whose scope has terminated is undefined
behavior.

The C++ language does require the destructors to be called.
If this is correct, when, if ever. is the memory in the stack
reclaimed?
The destructors, if any, are called when the activation associated with
the enclosing block scope terminates. The memory can be reclaimed any
time after that, and typically that is done right away, since in the
typical C++ implementation, a simple linear stack is in fact used as
the basis for automatic storage.
If this function were called many times, woudn't this lead to
leaks in the stack, if that is possible?
In languages which allow local variables to endure after the
termination of the enclosing block, this problem is taken care of by
garbage collection (or in less mature technology of that ilk, by
reference counting).

Note that in these languages, such as Scheme or Common Lisp, there
aren't any pointers. So the only way you can actually access a variable
in a block scope which has terminated is to invoke a closure which was
created in that block. The closure internally holds a reference to a
piece of code, and a reference to the environment frame which was saved
(the piece of the stack that was "leaked", but not really). That frame
pointer allows the closure to reference the variables.

In these languages, therefore, support for closures is the only reason
why variable bindings survive block scope termination. Good compilers
for these languages analyze and optimize closures. When closures are
not used in a function at all, all of its local storage can be
allocated on a stack, just like in C or C++, which can be a major
performance gain: if you don't use it, you don't pay for it. But even
if closures are present, this optimization may still be possible if the
closures are not escaping: the closures are only passed down into code
but not returned (in jargon, the code uses downward funargs only).
Closures which are not passed anywhere can even be refactored locally
and disappear. E.g. (funcall (lambda (x) (print x)) 42) is the same as
(let ((x 42)) (print x)) which can be hacked all the way down to (print
42).

Oct 17 '06 #7

Ian Collins wrote:
Salt_Peter wrote:
Ian Collins wrote:
>Dougan wrote:
Now, when c++ leaves the function, I guess the stack memory for
newImage
is not reclaimed, because the object is still referenced?
No, it will be reused, but you have made a copy of it.

Are you sure? What if the member pointer was never allocated? That
would be a copy into unreserved memory. So between resizes, the
temporary image would be copied into unprotected memory?
Please tell me i'm wrong. Otherwise, thats undefined behaviour and one
very nasty bug to crack.
Who knows? I was assuming the pointer was valid and the OP was simply
replacing the object. Otherwise the operation does invoke UB.
--
Ian Collins.
After verification and for the record, Qt's recommendation is to have
class ScribbleArea hold a QImage component, not a pointer. Now that
makes sense to me if QImage has the appropriate assignment operator.
Hmm.
http://doc.trolltech.com/4.1/widgets...blearea-h.html

To the OP, pointers - unless proven otherwise - point to nothing at all.

Oct 17 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
7739
by: Andrew | last post by:
Last night I was reading about implementing my own stack. The example given pushes items on and off the stack at the start and end of each procedure (ie. in a std module). What's not so clear is how this would work with class objects. In this case do you have to push the object on the stack at the start of every public procedure etc. in the class and pop it off at the end? I can't see how else you can know which object is active - or...
17
5046
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
4
3625
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
22
3050
by: bitshadow | last post by:
using the following code, i was able to have my compiler seg fault on me when i gave the argument as anythng greater than 20,832,000bytes. In the case of the struct its 868 instances of said structure. The compiler obviously allows VLA however it craps out after the above amount of bytes. I was told i was attempting to put everythng on the stack and not the heap. So i was wondering if anyone can maybe clear it up, is that true? would i...
13
2313
by: gmccallum | last post by:
General Info: A struct is stored on the stack and a class on the heap. A struct is a value type while a class is a reference type. Question: What if a struct contains a string property(variable). The string would be a reference type being contained in a value type. Would this filter up and cause the stack to now be a reference type placed on the heap or would it still be on the stack with a pointer used internally to point to a...
16
4449
by: sarathy | last post by:
Hi all, I need a few clarifications regarding memory allocaion in C++. I apologize for the lengthy explanation. 1. In C++, Objects are allocated in heap. What does heap refer to? Is it an area in RAM/Memory or does it refer to a data structure being used for storing objects. 2. In C++, functions and its local variables go in stack. If local variables that are primitives go in stack, it is OK. But what
148
5548
by: onkar | last post by:
Given the following code & variable i . int main(int argc,char **argv){ int i; printf("%d\n",i); return 0; } here i is allocated from bss or stack ?
13
2116
by: deepak | last post by:
Hi In the following function how the memory 'll be allocated. 1) Will it allocate memory for all the char's together or allocate for first char. then for int then for float and after this only second char in the function gets memory. 2) If i declare variables where the size of al variables gone beyond sizeof stack (assume it is 1000) what 'll happen? voud foo( int a, innt b)
87
5551
by: CJ | last post by:
Hello: We know that C programs are often vulnerable to buffer overflows which overwrite the stack. But my question is: Why does C insist on storing local variables on the stack in the first place? I can see two definite disadvantages with this: 1) deeply nested recursive calls to a function (especially if it defines
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9306
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6030
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3259
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.