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
Solved

Memory allocation in thread

Posted on 2000-04-18
9
178 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

839 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