When we use STL which memory space it will use whither it is stack or
heap or data segment
How to make STL to create in heap?
How to make whole container to create in heap?
I think container uses stack is it correct ?
I am using double linked list so in place of it I want to use STL for
#include<stdio. h>
#include<iostre am>
#include<vector >
using namespace std;
void fun();
void fun1();
vector<int> root;// I want to create this in heap
void main()
{
fun();
fun1();
}
void fun1()
{
printf("%d\n",r oot[1]);
}
void fun()
{
root.push_back( 55);
root.push_back( 66); 4 3687
sandeep wrote: When we use STL which memory space it will use whither it is stack or heap or data segment How to make STL to create in heap? How to make whole container to create in heap? I think container uses stack is it correct ?
Technically, Standard C++ doesn't discuss heap vs. stack. It refers to
"automatic storage" (which may be on the stack) and "free store" (which
may be a heap or heaps), and it allows compiler implementors to do
things differently for particular architectures in which, e.g., a stack
is not the most efficient implementation.
That being said, all containers use the free store for their containees
(unless you create an allocator to do differently), though the
container itself (e.g., head and tail pointers for std::list) might be
either automatic or dynamically allocated. Consider:
#include <vector>
void Foo()
{
std::vector<int > v1;
std::vector<int >* v2 = new std::vector<int >();
// ...
delete v2;
}
Here v1 and v2 will allocate their data on the free store, but the
housekeeping elements of v1 are automatic (in your case, on the stack)
while those of v2 are on the free store (though the pointer v2 itself
is still an automatic object).
I am using double linked list so in place of it I want to use STL for
Is this a completely separate question? #include<stdio. h> #include<iostre am> #include<vector > using namespace std; void fun(); void fun1(); vector<int> root;// I want to create this in heap
First, this is not a doubly linked list. std::vector is essentially a
smart array, which means that its insertions and deletions are
expensive. If you need a linked list, you might consider std::list
(compare http://www.sgi.com/tech/stl/List.html).
Second, don't use global variables. Reducing variable scope as much as
possible helps make programs more understandable and robust.
May I suggest you get a good C++ book such as _Accelerated C++_ by
Koenig and Moo?
void main()
int main(). See http://www.parashift.com/c++-faq-lit....html#faq-29.3.
{ fun(); fun1();
} void fun1() { printf("%d\n",r oot[1]); }
Prefer iostreams for type safety, etc. See http://www.parashift.com/c++-faq-lit....html#faq-15.1.
void fun() {
root.push_back( 55); root.push_back( 66);
}
Cheers! --M
On 2006-06-05 13:34, sandeep wrote: #include<stdio. h> #include<iostre am> #include<vector > using namespace std;
Generally not a good idea to use using namespace std, see the FAQ for
more details: http://www.parashift.com/c++-faq-lit....html#faq-27.5
void fun(); void fun1(); vector<int> root;// I want to create this in heap
Not going to answer your question since mlimber have already done so,
but would just like to point out that you have managed to declare your
vector in such a way that most implementations/architectures would place
it neither on the stack nor the heap. Global variables and static
variables are special since they exist for the whole duration of the
application and are thus normally stored in their own area of memory.
Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
Erik Wikström wrote: On 2006-06-05 13:34, sandeep wrote: #include<stdio. h> #include<iostre am> #include<vector > using namespace std;
Generally not a good idea to use using namespace std, see the FAQ for more details: http://www.parashift.com/c++-faq-lit....html#faq-27.5
Note, however, that there are other schools of thought. For instance,
Sutter and Alexandrescu say in _C++ Coding Standards_, item 59 (italics
in original): "You can and should use namesapce using declarations and
directives liberally /in your implementation files after #include
directives/ and feel good about it. Despite repeated assertions to the
contrary, namespace using declarations and directives are not evil and
they do not defeat the purposes of namespaces. Rather, they are what
make namespaces usable."
Cheers! --M
mlimber wrote: Erik Wikström wrote: On 2006-06-05 13:34, sandeep wrote: #include<stdio. h> #include<iostre am> #include<vector > using namespace std;
Generally not a good idea to use using namespace std, see the FAQ for more details: http://www.parashift.com/c++-faq-lit....html#faq-27.5
Note, however, that there are other schools of thought. For instance, Sutter and Alexandrescu say in _C++ Coding Standards_, item 59 (italics in original): "You can and should use namesapce using declarations and directives liberally /in your implementation files after #include directives/ and feel good about it. Despite repeated assertions to the contrary, namespace using declarations and directives are not evil and they do not defeat the purposes of namespaces. Rather, they are what make namespaces usable."
Watch for conflicts if you do that. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Dave |
last post by:
Hello all,
I'm pondering why the default underlying container for std::priority_queue<>
is std::vector<>. It would seem that inserts are liable to happen anywhere,
which would make std::list<> a superior alternative. Why factors am I not
considering here? Why in the general case would std::vector<> be best?
Thanks,
Dave
|
by: Johan |
last post by:
Hi,
Where can I find coding example in C++ for homogeneous container.
John
|
by: Irish |
last post by:
Hello all :)
Hopefully someone can shed some light on this problem I'm having. I'm
trying to declare a variable to a type of class I've defined (which is
a minHeap), but actually instantiate it later when I get a command from
the user. This is my class and if you look down a lil you can see what
I'm trying to do in my main(). I want to ask for the size from the
user, but when I try to I can't get my code to compile. Here is the
class...
|
by: Don Hames |
last post by:
I have a windows application that has a split container in the client area.
In the left panel, I added controls via the designer in VS 2005. In the right
panel, I want to dynamically create and display other forms that I created in
the project. Is this possible? Can the panel2 of a split container be the
container for a dynamically created form? How else can I create a series of
other windows (forms) and place them in the right panel of the...
|
by: Saber |
last post by:
Public Class User
Private _id, _P, _L, _R As Integer
Private _balance As Int64
Private _name, _family, _tel1, _tel2, _mobile, _city, _accountNumber As
String
Private _password, _email, _ficheNumber, _postalAddress As String
Private _province, _bank As String
Private _isSpecial, _photo As Boolean
Public stk As New Stack
| |
by: Eniac |
last post by:
*argh* ... *pull hairs*
I've recently started developing from ASP to ASP.NET
The switch was fairly smooth since i had done some VB.net before ...
then came...FORMS! :)
I find it astounding at how difficult it has become to control a form,
something that was so dead easy in ASP.
|
by: Anjo Gasa |
last post by:
Let's say I have:
std::vector<Object> rgObjects;
rgObjects itself is declared as a local variable and hence on the
stack. But what about as I add elements to rgObjects:
Object newObject( 3, 4 );
rgObjects.push_back( newObject );
|
by: John Harrison |
last post by:
This is from SGI's FAQ, its the justification for why list<T>::size() is
linear time in their library (and in gcc library too since their code is
based on SGI)
<quote>
Why is list<>::size() linear time?
The size() member function, for list and slist, takes time proportional
to the number of elements in the list. This was a deliberate tradeoff.
The only way to get a constant-time size() for linked lists would be to
|
by: Goran |
last post by:
Hi @ all!
Again one small question due to my shakiness of what to use...
What is better / smarter?
private:
vector<MyClass_t* itsVector;
OR...
|
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...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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: 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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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...
| |