Solved

Memory allocation in thread

Posted on 2000-04-18
9
186 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

Industry Leaders: 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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

707 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