473,394 Members | 1,811 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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::resizeImage( 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 1851
Dougan wrote:
I've seen code that allocates an object on the stack and then saves a
class reference to it.

example:
void ScribbleArea::resizeImage( 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::resizeImage( 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::resizeImage( 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::operator = (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
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...
17
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
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...
22
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...
13
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...
16
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...
148
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
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...
87
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.