Solved

Memory allocation in thread

Posted on 2000-04-18
9
169 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:mathought
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

744 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

12 Experts available now in Live!

Get 1:1 Help Now