473,396 Members | 2,023 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,396 software developers and data experts.

new operator and memory allocation

Hi all,

Just wondering exactly how memory is allocated using the new operator?
More specifically I am interested in how the memory is calculated for
globable variables? I recently stumbled into a problem which corrected
my way of thinking.

Now, people probably won't be able to answer much to that question, so I
will give an example. This is example uses the qt library qstring.h, but
I assume this would be similar to the C++ string.h and don't think this
should be OT.

e.g. I have a class header:

class AClass {
public:
AClass( QString id );
private:
QString identifier;
int aInt;
};

And implementation:
AClass::AClass( QString id ) {
identifier = id;
aInt = 0;
}

Now, I thought the memory would be allocated on the stack for identifier
and aInt, however it appears that QString isn't a type, and so aInt gets
memory but there is no guarantee for identifier. This means the memory
address of identifer can potentially get used elsewhere if the function
where identifier was created returns. Not a desirable effect.

So as it turns out I use the * and new operator and it quite obviously
is fine.

So, how does C++ handle allocation in such situations?

Thanks and sorry for the length of the post. Hopefully this is
understandable.

Lionel.
Jul 23 '05 #1
5 1667
Lionel wrote:
Hi all,

Just wondering exactly how memory is allocated using the new operator?
More specifically I am interested in how the memory is calculated for
globable variables? I recently stumbled into a problem which corrected
my way of thinking.

Now, people probably won't be able to answer much to that question, so I
will give an example. This is example uses the qt library qstring.h, but
I assume this would be similar to the C++ string.h and don't think this
should be OT.
Well, the particulars *are* OT, but the general concepts behind the
problem you're seeing are not, so I'll continue:

First of all, in C++, it's <string> and, like qstring.h, it's a *header*
not a library. This is a fundamental distinction you need to understand.

e.g. I have a class header:

class AClass {
public:
AClass( QString id );
private:
QString identifier;
int aInt;
};

And implementation:
AClass::AClass( QString id ) {
identifier = id;
aInt = 0;
}
BTW, write this using initializer lists:

AClass::AClass(QString id) : identifier(id), aInt(0) {}
Now, I thought the memory would be allocated on the stack for identifier
and aInt, however it appears that QString isn't a type, and so aInt gets
If QString *isn't* a type, there would be no chance of this compiling.
memory but there is no guarantee for identifier. This means the memory
address of identifer can potentially get used elsewhere if the function
where identifier was created returns. Not a desirable effect.

So as it turns out I use the * and new operator and it quite obviously
is fine.

So, how does C++ handle allocation in such situations?


Have you looked at the <qstring.h> header? Apparently the only data
members it contains are two pointers to underlying objects (assumedly
there is memory allocated to them in the QString constructors).

What's probably happening is that you're geting a shallow copy of `id'
in your constructor, and when `id' goes out of scope, it's taking its
underlying data members with it.

You really need to know How Things Work before working with this kind of
stuff.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #2
Artie Gold wrote:
Lionel wrote:
Hi all,

Just wondering exactly how memory is allocated using the new operator?
More specifically I am interested in how the memory is calculated for
globable variables? I recently stumbled into a problem which corrected
my way of thinking.

Now, people probably won't be able to answer much to that question, so
I will give an example. This is example uses the qt library qstring.h,
but I assume this would be similar to the C++ string.h and don't think
this should be OT.

Well, the particulars *are* OT, but the general concepts behind the
problem you're seeing are not, so I'll continue:

First of all, in C++, it's <string> and, like qstring.h, it's a *header*
not a library. This is a fundamental distinction you need to understand.


Understood :). For some reason I often have compilation problems when I
just go #include <string>. I know I should figure out why as #include
<string.h> is deprecated . . . it may be qt related in this case, I'm
not sure.

e.g. I have a class header:

class AClass {
public:
AClass( QString id );
private:
QString identifier;
int aInt;
};

And implementation:
AClass::AClass( QString id ) {
identifier = id;
aInt = 0;
}

BTW, write this using initializer lists:

AClass::AClass(QString id) : identifier(id), aInt(0) {}


I'm stuck in a Java way of programming, which in this instance I still
prefer for readability. I have read that the above method can be more
efficient, but seriously how much difference does it make?

Now, I thought the memory would be allocated on the stack for
identifier and aInt, however it appears that QString isn't a type, and
so aInt gets

If QString *isn't* a type, there would be no chance of this compiling.
memory but there is no guarantee for identifier. This means the memory
address of identifer can potentially get used elsewhere if the
function where identifier was created returns. Not a desirable effect.

So as it turns out I use the * and new operator and it quite obviously
is fine.

So, how does C++ handle allocation in such situations?

Have you looked at the <qstring.h> header? Apparently the only data
members it contains are two pointers to underlying objects (assumedly
there is memory allocated to them in the QString constructors).

What's probably happening is that you're geting a shallow copy of `id'
in your constructor, and when `id' goes out of scope, it's taking its
underlying data members with it.

You really need to know How Things Work before working with this kind of
stuff.


I understand what you are saying and it is as I thought. C++ works
slightly differently than what I was expecting. I was hoping that
declaring a global variable would allocate the memory in the scope that
class, then passing in the parameter id as above, would pass by value.
This clearly isn't the case.

Thanks for the reply.

Lionel.
Jul 23 '05 #3
Lionel wrote:
Artie Gold wrote:
[snip]
Understood :). For some reason I often have compilation problems when I
just go #include <string>. I know I should figure out why as #include
<string.h> is deprecated . . . it may be qt related in this case, I'm
not sure.
<string.h> is a C header for manipulation of C-style `strings', i.e.
null-terminated sequences of chars. <string> contains the necessary
declarations for std::string, part of the standard C++ library.

e.g. I have a class header:

class AClass {
public:
AClass( QString id );
private:
QString identifier;
int aInt;
};

And implementation:
AClass::AClass( QString id ) {
identifier = id;
aInt = 0;
}
BTW, write this using initializer lists:

AClass::AClass(QString id) : identifier(id), aInt(0) {}

I'm stuck in a Java way of programming, which in this instance I still
prefer for readability. I have read that the above method can be more
efficient, but seriously how much difference does it make?

The difference is that initialization is initialization and assignment
is assignment. In most cases, there will be very little run time
difference between the two -- but `saying what you mean' is usually good
advice for programming.

Mentioning Java as you do, remember that in Java, (almost) everything is
really a pointer; (almost) nothing is allocated on `the stack'. In C++,
there's a definite distinction between the two, which has further
implications (particularly as regards polymorphism).

Now, I thought the memory would be allocated on the stack for
identifier and aInt, however it appears that QString isn't a type,
and so aInt gets


If QString *isn't* a type, there would be no chance of this compiling.
memory but there is no guarantee for identifier. This means the
memory address of identifer can potentially get used elsewhere if the
function where identifier was created returns. Not a desirable effect.

So as it turns out I use the * and new operator and it quite
obviously is fine.

So, how does C++ handle allocation in such situations?


Have you looked at the <qstring.h> header? Apparently the only data
members it contains are two pointers to underlying objects (assumedly
there is memory allocated to them in the QString constructors).

What's probably happening is that you're geting a shallow copy of `id'
in your constructor, and when `id' goes out of scope, it's taking its
underlying data members with it.

You really need to know How Things Work before working with this kind
of stuff.

I understand what you are saying and it is as I thought. C++ works
slightly differently than what I was expecting. I was hoping that
declaring a global variable would allocate the memory in the scope that
class, then passing in the parameter id as above, would pass by value.
This clearly isn't the case.


Well, it *does* pass by value. It's just that the value passed really
only contains two pointers that point somewhere else.

The other thing to note is the difference between a language where free
store allocation/deallocation is manual as opposed to a garbage
collected language; in the former not only can things be changed behind
your back (the shallow copy problem, with which I'm sure you're
familiar) but they can *disappear entirely*.

I would recommend going to http://www.accu.org and finding a book about
C++ appropriate for your background and learning style.
Thanks for the reply.

You're welcome.

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Jul 23 '05 #4
QString is not a type? What is it then, a tomato?
"Lionel" <ch***********@hotmail.com> wrote in message
news:d2***********@bunyip2.cc.uq.edu.au...
Hi all,

Just wondering exactly how memory is allocated using the new operator?
More specifically I am interested in how the memory is calculated for
globable variables? I recently stumbled into a problem which corrected my
way of thinking.

Now, people probably won't be able to answer much to that question, so I
will give an example. This is example uses the qt library qstring.h, but I
assume this would be similar to the C++ string.h and don't think this
should be OT.

e.g. I have a class header:

class AClass {
public:
AClass( QString id );
private:
QString identifier;
int aInt;
};

And implementation:
AClass::AClass( QString id ) {
identifier = id;
aInt = 0;
}

Now, I thought the memory would be allocated on the stack for identifier
and aInt, however it appears that QString isn't a type, and so aInt gets
memory but there is no guarantee for identifier. This means the memory
address of identifer can potentially get used elsewhere if the function
where identifier was created returns. Not a desirable effect.

So as it turns out I use the * and new operator and it quite obviously is
fine.

So, how does C++ handle allocation in such situations?

Thanks and sorry for the length of the post. Hopefully this is
understandable.

Lionel.

Jul 23 '05 #5
Lionel <ch***********@hotmail.com> wrote in message news:<d2***********@bunyip2.cc.uq.edu.au>...

Just wondering exactly how memory is allocated using the new operator?
More specifically I am interested in how the memory is calculated for
globable variables? I recently stumbled into a problem which corrected
my way of thinking.

Now, people probably won't be able to answer much to that question, so I
will give an example. This is example uses the qt library qstring.h, but
I assume this would be similar to the C++ string.h and don't think this
should be OT.

e.g. I have a class header:

class AClass {
public:
AClass( QString id );
private:
QString identifier;
int aInt;
};

And implementation:
AClass::AClass( QString id ) {
identifier = id;
aInt = 0;
}

Now, I thought the memory would be allocated on the stack for identifier
and aInt, however it appears that QString isn't a type, and so aInt gets
memory but there is no guarantee for identifier. This means the memory
address of identifer can potentially get used elsewhere if the function
where identifier was created returns. Not a desirable effect.

So as it turns out I use the * and new operator and it quite obviously
is fine.


(1) Your example has nothing to do with the new operator. Can you
provide an example that might be relevant to te question you're trying
to ask?

(2) Objects will be constructed for both the "identifier" and the
"aInt" members of any AClass objects you create. Other than not being
compileable because you did not include the appropriate headers, the
above code is correct. Please make sure you post compileable code in
the future.

(3) Getting into OT details, std::string and QString differ in the
fact that a std::string object is always fully-constructed, but a
QString object can be partially constructed (that is, a comparison
with QString::null will return true). Attempting operations on a
constructed-but-not-initialized QString object will bring you grief.
Perhaps this is the source of confusion that has lead to your
question?

(4) You didn't show how you used the new operator to solve your
(unspecified) problem, but I suspect it may not be fine if you don't
use delete to destroy the object at the appropriate time.

--
smw
Jul 23 '05 #6

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

Similar topics

11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
22
by: BekTek | last post by:
Hi.. I'm so sorry about that I've postes so many questions recently.. :) I'm still confused about the differences between malloc and operator new.. I know that when we work with class object and...
6
by: Vinu | last post by:
Hi, I am maintaining a C++ project which is a server which continuously receives requeste from clients. I have noticed that we overload the new operator and in it then call malloc to allocate...
7
by: Sean | last post by:
Can someone help me see why the following "operator=" overloading doesn't work under g++? and the error message is copied here. I see no reason the compiler complain this. Thanks, $ g++...
3
by: toton | last post by:
Operator overloading has a sort syntax rather than member function call for stack based memory allocation. like complex<int> c1,c2,c3; c3= c1+c2; How the same can be applied to heap based...
15
by: mangesh | last post by:
This code is from c++ faq in section 11 : void someCode() { char memory; void* p = memory; Fred* f = new(p) Fred(); f->~Fred(); // Explicitly call the destructor for the placed object }
2
by: bharath.donnipad | last post by:
Hi All, I was going through a c++ manual in msdn site "http://msdn2.microsoft.com/en-us/library/kewsb8ba.aspx". There there was a small note on usage of new operator : "When new is used to...
5
by: sam_cit | last post by:
Hi Everyone, I was just wondering, about the overloaded assignment operator for user defined objects. It is used to make sure that the following works properly, obj1 = obj; so the...
13
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one...
30
by: Angel Tsankov | last post by:
Hello! Does the C++ standard define what happens when the size argument of void* operator new(size_t size) cannot represent the total number of bytes to be allocated? For example: struct S {...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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
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,...

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.