473,836 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL list problems

I have a stl list and i have a problem allocating memory for it. I am
using the malloc operator in order to get memory for it and I have
tried using placement new but it still isnt setting up the list
correctly.

The solution im looking for is something as follows

myObj->std::list<tObj = (std::list<tObj >
*)malloc(sizeof (std::list<tObj >));
SetupListWithou tGettingNewMemo ry(myObj->std::list<tObj >);
myObj->myList->insert(somethi ng);

Jul 31 '06 #1
2 1754
gi**********@gm ail.com wrote:
I have a stl list and i have a problem allocating memory for it. I am
using the malloc operator in order to get memory for it and I have
tried using placement new but it still isnt setting up the list
correctly.

The solution im looking for is something as follows

myObj->std::list<tObj = (std::list<tObj >
*)malloc(sizeof (std::list<tObj >));
SetupListWithou tGettingNewMemo ry(myObj->std::list<tObj >);
myObj->myList->insert(somethi ng);
What you're looking for is probably the "placement new" operator. You
will need to include <memoryheader and use something like

void* buffer = malloc(sizeof(s td::list<tObj>) );
myObj->myList = new (buffer) std::list<tObj> ;
myObj->myList->insert(somethi ng);

The funny thing, of course, is that while you will be allocating the
memory for the list itself, you're still allowing the list to allocate
memory for the objects it creates. In order to take control over that,
you need to learn to use "allocators ". You can also overload the 'new'
and 'delete' for your class ('tObj') and those things will be called
when another object of 'tObj' type is about to be created...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 31 '06 #2
Hello,

Victor Bazarov wrote:
gi**********@gm ail.com wrote:
>I have a stl list and i have a problem allocating memory for it. I
am using the malloc operator in order to get memory for it and I have
tried using placement new but it still isnt setting up the list
correctly.
you need to learn to use "allocators ". You can also overload the
'new' and 'delete' for your class ('tObj') and those things will be
called when another object of 'tObj' type is about to be created...
But overloading new and delete for tObj won't help anything here. The
tObj instances are usually put into objects of a class used for the
nodes, which will be allocated by the allocator object given by default
or by the user to the list object at construction time. All you can do
is learning to use allocators.

There is no sensible reason to use malloc in this way. If you write why
you cannot live with the defaults, perhaps you can get better advice.

Bernd Strieder

Aug 1 '06 #3

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

Similar topics

35
3004
by: Moosebumps | last post by:
Does anyone here find the list comprehension syntax awkward? I like it because it is an expression rather than a series of statements, but it is a little harder to maintain it seems. e.g. you could do: result = for element in list: if element == 'blah':
5
2817
by: Max Bentz | last post by:
How may I generate a numbered list (using <ol><li>, etc), which contains more than 1000 elements? My list is ok till 999, then it continues with 000, 001, 002, etc. Thanks for a tip Max
14
5639
by: Dave | last post by:
Hello all, After perusing the Standard, I believe it is true to say that once you insert an element into a std::list<>, its location in memory never changes. This makes a std::list<> ideal for storing vertices of an arbitrary n-ary tree where a vertex contain pointers to its parent / children. These parent / child vertices need to stay put if we've got pointers to them somewhere! Am I correct in my assertion?
2
1292
by: 50295 | last post by:
Hi! Here's a followup to my earlier post http://tinyurl.com/dthhs (Full URL: http://groups-beta.google.com/group/comp.lang.javascript/browse_frm/thread/dfff447eb6e5afd7/) In the code below, I've used lists to implement a drop-down menu, and it works well in IE, but is sort of bugg in NN/FF. You'll notice that when moving right down the list to the last item ("Asia"), the list will sometimes fail to collapse, if you exit fom the...
19
3580
by: William Wisnieski | last post by:
Hello Everyone, I have a main form with a datasheet subform that I use to query by form. After the user selects two criteria on the main form and clicks the cmdShowResults button on the main form, the subform returns the records based on the two criteria. The criteria used on the main form are values selected in two list boxes. When the user clicks on the first list box (lstCollege), it returns values in the second list box...
4
2549
by: Andy M | last post by:
ALERT There is a person by the name of Mike Cox who's trying to turn this mailing list into a Big-8 newsgroup. Many of you know that this and most of the other postresql mailing lists are already gated to Google Groups and a small number of private news servers. If Mike Cox succeeds, this list will be available as a newsgroup on a great many more ISPs and NSPs, which could be a good or a bad thing. For one, e-mail addresses on this
9
1716
by: ogerchikov | last post by:
I have 2 classes, A, B and B is a child of A. and I have a function which processes a list of A. void func(list<A> alist) { // processing list of A } The problem is func() won't able to handle a list of B even B is a child of A.
45
18928
by: Zytan | last post by:
This returns the following error: "Cannot modify the return value of 'System.Collections.Generic.List<MyStruct>.this' because it is not a variable" and I have no idea why! Do lists return copies of their elements? Why can't I change the element itself? class Program { private struct MyStruct
11
76323
by: dave18 | last post by:
Hello all! I found a solution to my original question, but there's still so much I don't understand about it, I thought I'd give this forum a try. At the very least, maybe it will help someone else who got stumped like I did. It seems so simple... binding a DataGridView to a List<T>. These are the two general problems that I kept running into: (1) When the data in the list updated, the data on the screen did not update. (2) When I...
8
718
by: dmp | last post by:
What are Linked list? Please somebody show some ready made programs of linked list
0
9827
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, 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...
0
10862
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, 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...
0
10560
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 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...
1
10607
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,...
0
9389
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, 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...
0
5838
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4468
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
2
4033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3119
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 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...

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.