Solved

Memory allocation in thread

Posted on 2000-04-18
9
171 Views
Last Modified: 2010-04-02
Can I allocate memory in thread by doing new or are there any consequences of that?
Something like that:

cObject *obj;
obj = new cObject;
.
.
.
delete obj;


Thanks
0
Comment
Question by:Yulia
9 Comments
 
LVL 1

Expert Comment

by:MT_MU
ID: 2729480
Yes you can - there should be no consequences (other that you will allocate memory).
0
 
LVL 6

Accepted Solution

by:
DrDelphi earned 50 total points
ID: 2729514
You should be able to allocate all the memory you like within a new thread. the only possible issues you might have are:

1.Deallocating that memory (every new has a coresponding delete)

2. Synchronization. (watch that the new thread isn't stepping on another's memory)


Good luck!!
0
 
LVL 22

Expert Comment

by:nietod
ID: 2730218
This is no difference (that you need to worry about) in allocating memory in a multi-threaded situation compared to a single threaded one.  

Synchronization is an issue with any shared resource.  If a block of memory is shared between threads then it does need to be synched.  But that is true even iof the memory was not dynamically allocated.
0
 
LVL 86

Expert Comment

by:jkr
ID: 2730366
>>Can I allocate memory in thread by
>>doing new or are there any
>>consequences of that?

The consequences are simply an 'unwanted' synchronization with memory allocations in other threads, as access to the global heap (where 'new' actually gets the memory from) is protected by the allocator using a sychronization object.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:mathought
ID: 2732395
Since every executable code in windows belong to a certain therad, narturally you can allocate memory without a problem from within threads.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2732495
I don't think that is a natural consequence.  For example the allocation process must be handled differently (to some extent) in a mult-threaded environment, or your get problems.  So for example if you used the single threaded RTL in a multithreaded program (which actually you can do), the allocations will not always work correctly.
0
 

Expert Comment

by:simmvar
ID: 2734103
Hi Yulia

Instead of allocating memory using new try using ThreadAllocTls functions which allocate memory which are local to u r thread.  Please go thro the msdn manuals on that.  U can do this only when u r going to allocate memory which is strictly local to u r thread and windows will manage the synchronisation
Hope it works

simmvar
0
 

Expert Comment

by:simmvar
ID: 2734104
Hi Yulia

Instead of allocating memory using new try using ThreadAllocTls functions which allocate memory which are local to u r thread.  Please go thro the msdn manuals on that.  U can do this only when u r going to allocate memory which is strictly local to u r thread and windows will manage the synchronisation
Hope it works

simmvar
0
 
LVL 22

Expert Comment

by:nietod
ID: 2734146
That is for a very specific case!  In general you do not want to use ThreadAllocTls().   Most allocations will be with new or just local objects on the stack.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now