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. 5 1695
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
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.
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 nice and working allocation deallocation
routines. However, I don't want to loose the nice extras of new operator,
like - constructor calling, typecasting the result, keeping the array size,
etc.
For another bunch of reasons, outside this scope I...
|
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 use operator new/delete, the
ctor and dtor
get called as expected, but malloc and free do not..
But Herbal Sutter mentioned in his greate book 'exceptional c++' about free
storage
|
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 memory.
Specifically the code is something like this.
|
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++ copyconstructor1.cpp
#copyconstructor1.cpp: In function `int main()':
#copyconstructor1.cpp:86: no match for `sample& = sample' operator
#copyconstructor1.cpp:53: candidates are: sample sample::operator=(sample&)
...
|
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 memory allocation?
like complex<int> * c1,*c2,*c3;
i still want to do something like c3 = c1+c2 ; rether than *c3 =
*c1+*c2;
| |
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
}
|
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 allocate memory for a C++ class object, the
object's constructor is called after the memory is allocated."
So, I got a doubt and quickly executed below program. Its o/p is : base
|
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 overloaded operator function performs the necessary copy of
obj's member in to obj1. Can't the same be done using copy
|
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 implement operator new/delete I can't see a way to
indicate, on delete, how many objects must be destroyed (or how big
the space is) - alternatively I can't figure out what are the alignment
requirements so that the implementation, after calling my...
|
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
{
char a;
};
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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...
|
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,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |