473,396 Members | 1,866 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.

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>));
SetupListWithoutGettingNewMemory(myObj->std::list<tObj>);
myObj->myList->insert(something);

Jul 31 '06 #1
2 1733
gi**********@gmail.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>));
SetupListWithoutGettingNewMemory(myObj->std::list<tObj>);
myObj->myList->insert(something);
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(std::list<tObj>));
myObj->myList = new (buffer) std::list<tObj>;
myObj->myList->insert(something);

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**********@gmail.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
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...
5
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
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...
2
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,...
19
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...
4
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...
9
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...
45
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...
11
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...
8
by: dmp | last post by:
What are Linked list? Please somebody show some ready made programs of linked list
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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
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
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.