Solved

Memory allocation in thread

Posted on 2000-04-18
9
182 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I could not build boost code, 10 106
Safe conversion? 4 75
gdb doesn't stop on breakpoint 2 113
What is sub-make ? 2 87
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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 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