The Flying Dutchman
The Flying Dutchman

Reputation: 592

Memory allocation for containers in C++

I have a question on memory allocation for containers on C++.

Look at the pseudocode for a multi threaded application (assume it is in c++). I declare vector object in the main method. Then I run a thread and pass this object to the thread. The thread runs in another processor. Now, I insert 100000 elements into the vector.

typedef struct myType
{
    int a;
    int b;
}myType;

ThreadRoutine()
{
    Run Thread in processor P;
    insert 1000000 elements into myTypeObject
}

int main()
{
    std::vector<myType> myTypeObject;
    CALLTHREAD and pass myTypeObject
} 

I want to know where the memory will be allocated for the 100000 elements: -From the main itself -From the thread

The reason I ask this is because, I want to run the thread in a different processor. And my machine is a NUMA machine. So if the memory is allocated from the thread, it will be in the local memory bank of the thread. But if the memory is allocated from main, it will be allocated from the local memory bank of the main thread.

By my intuition, I would say that the memory is allocated only in the threads. Please let me know your thoughts.

Upvotes: 0

Views: 272

Answers (1)

justin
justin

Reputation: 104698

the reallocation(s) will be called from ThreadRoutine() -- so, whichever thread calls that (the secondary in your example).

of course, you could reserve on the main thread before passing it around, if you want to avoid resizing on the secondary thread.

Upvotes: 1

Related Questions