473,507 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

memory leak?

1 int longFunction() {
2 string x1 ("eee");
3 string x2 = x1;
4 x2 = "fff";

....

Please correct me if any of this is wrong:

At the end of line 3, x2 will have some memory M associated with it (on
the heap/ stack - depends on implementation of copy str of string.

What happens to M at the end of line 4? Apologize if this is a stupid
question.

Jul 23 '05 #1
8 1934
"TechCrazy" <Te*******@hotpop.com> schrieb:
1 int longFunction() {
2 string x1 ("eee");
3 string x2 = x1;
4 x2 = "fff";

What happens to M at the end of line 4? Apologize if this is a
stupid question.


If you mean std::string there is no memory leak. The class is
responsible for managing the memory needed by the internal buffer.

In this case the object x1 exists still at the end of line 4. You have
just two local string objects. Both of them will be deleted at the end
of the function.

T.M.
Jul 23 '05 #2
x2 will have a new set of memory for new string.
Try to print the value of x1 and x2 after Line#4. you will find the
result.

Jul 23 '05 #3

"TechCrazy" <Te*******@hotpop.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
1 int longFunction() {
2 string x1 ("eee");
3 string x2 = x1;
4 x2 = "fff";

....

Please correct me if any of this is wrong:

At the end of line 3, x2 will have some memory M associated with it (on
the heap/ stack - depends on implementation of copy str of string.

both x1 and x2 are allocated on the stack, although they may also manage
some of their contents on the heap. But don't bother with implementation
detail that you are not supposed to know.
What happens to M at the end of line 4? Apologize if this is a stupid
question.


This is a valid question!

Stack memory will persist untill the objects run out of scope (in this case,
untill the function returns.)

Regards,
Ben
Jul 23 '05 #4
TechCrazy wrote:

1 int longFunction() {
2 string x1 ("eee");
3 string x2 = x1;
4 x2 = "fff";

....

Please correct me if any of this is wrong:

At the end of line 3, x2 will have some memory M associated with it (on
the heap/ stack - depends on implementation of copy str of string.
( assuming with 'string' you mean 'std::string' )

You are right: it depends on the actual implementation.
But conceptually you are wrong.
x2 has its own memory. And this memory is assigned the very same contents
as x1 gas. x2 and x1 contain the same string "eee". But other then that they
have nothing in common.

What happens to M at the end of line 4? Apologize if this is a stupid
question.


Nothing special. The content of x2 is changed to contain "fff".

Even *iff* your string class uses a form of "reference counted, copy on write"
memory management, all will be done under the hood, such that conceptually you
will note no difference to the above said: For the programmer each string
object holds its own memory, where it stores the characters.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #5

"benben" <be******@hotmail.com> wrote in message
news:42***********************@news.optusnet.com.a u...

"TechCrazy" <Te*******@hotpop.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
1 int longFunction() {
2 string x1 ("eee");
3 string x2 = x1;
4 x2 = "fff";

....


More into detail:

After line 2, some memory m1 (of the size of sizeof(string)) is allocated
from the stack for x1; The object x1, during construction, might allocat
some memory M1 from elsewhere than the stack, most likely from the heap, of
size dependent on the implementation detail.

After line 3, some memory m2 (of the size of sizeof(string)) is allocated
from the stack for x2; Like wise, the object x2, during construction, might
allocat some memory M2 from elsewhere than the stack, most likely from the
heap, of size dependent on the implementation detail.

The memory blocks m1, M1, m2, M2 persist during the execution of subsequent
lines in the function.

When the objects run out of scope, i.e. when the function returns:

x2 is destroyed and m2 is freed. If "string" is a good implementation then
M2 is also freed during destruction of x2; then
x1 is destroyed and m1 is freed. Likewise M1 is also freed during
destruction of x1.

Regards,
Ben
Jul 23 '05 #6
TechCrazy wrote:
1 int longFunction() {
2 string x1 ("eee");
"eee" is allocated in static memory until the end of the program (has
static storage)
x1 is allocated on the stack (has automatic storage)
string's constructor is invoked
"eee" is copied to an internal buffer

Note: Only the string object is assigned on the stack. The internal
buffer may be allocated differently.
3 string x2 = x1;
x2 is allocated on the stack (has automatic storage)
x2 default constructor is invoked
string assigment operator is invoked
the _content_ of x1 is copied to x2
4 x2 = "fff";
"fff" is allocated in static memory (has static storage)
string's assignment operator is called
"fff" is copied to x2, replacing the _content_ of the internal
buffer
....


At the end of the current scope:

x2 destructor is invoked
internal buffer is released
x1 destructor is invoked
internal buffer is released

This is not a memory leak. Each object manages its own internal buffer
and is responsible for copying contents.

Jul 23 '05 #7
>> 3 string x2 = x1;

x2 is allocated on the stack (has automatic storage)
x2 default constructor is invoked
string assigment operator is invoked
the _content_ of x1 is copied to x2


No. The copy constructor is called. What the compiler really does is

string x2(x1);

The assignment operator is never called in initializations.
Jonathan

Jul 23 '05 #8
Jonathan Mcdougall wrote:
3 string x2 = x1;


x2 is allocated on the stack (has automatic storage)
x2 default constructor is invoked
string assigment operator is invoked
the _content_ of x1 is copied to x2


No. The copy constructor is called. What the compiler really does is

string x2(x1);

The assignment operator is never called in initializations.


Yeah, sorry about that. I know about it but my eyes were already
looking at the next statement while typing. ;)

Jul 23 '05 #9

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

Similar topics

8
3394
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
17
4770
by: José Joye | last post by:
Hi, I have implemented a Service that is responsible for getting messages from a MS MQ located on a remote machine. I'm getting memory leak from time to time (???). In some situation, it is...
4
6056
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password";...
20
8024
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
23
4509
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
8
8521
by: Adrian | last post by:
Hi I have a JS program that runs localy (under IE6 only) on a PC but it has a memory leak (probably the known MS one!) What applications are there that I could use to look at the memory usage of...
7
6914
by: Salvador | last post by:
Hi, I am using WMI to gather information about different computers (using win2K and win 2K3), checking common classes and also WMI load balance. My application runs every 1 minute and reports...
3
5291
by: Jim Land | last post by:
Jack Slocum claims here http://www.jackslocum.com/yui/2006/10/02/3-easy-steps-to-avoid-javascript- memory-leaks/ that "almost every site you visit that uses JavaScript is leaking memory". ...
7
15673
by: Ragnar Agustsson | last post by:
Hi all I have been wandering about the best way to sandbox memory leaks in 3rd party libraries when using them from the .Net framework. I have a 3rd party library, written in C++, that leaks a...
22
9303
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
7223
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
7114
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7034
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...
0
7488
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
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,...
0
4702
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...
0
3191
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...
0
1544
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 ...
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.