Created
April 19, 2018 02:05
-
-
Save amjames/11132ff24ff7a1c5c18c3015a2009c82 to your computer and use it in GitHub Desktop.
Example of using std::vector(begin, begin+size) constructor.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Memory used after allocating lg_array: 800 | |
Memory used after creating sm_vector: 840 | |
sm_vector = [ 0, 1, 2, 3, 4,] | |
sm_vector (after modify) = [ 100, 101, 102, 103, 104,] | |
First 5 elements of the original lg_array = [ 0, 1, 2, 3, 4,] | |
size of allocation lg_array: 800 | |
size of allocation sm_vector.data(): 40 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I am using a library here to let me track memory allocated but that is just to show that | |
// This vector constructor does actually cause new memory to be allocated and the elements are copied | |
#include <iostream> | |
#include "umpire/ResourceManager.hpp" | |
#include "umpire/Allocator.hpp" | |
#include "umpire/TypedAllocator.hpp" | |
int main(int, char**) { | |
auto& rm = umpire::ResourceManager::getInstance(); | |
umpire::Allocator alloc = rm.getAllocator("HOST"); | |
umpire::TypedAllocator<double> vector_allocator(alloc); | |
// create a double* 100 elements and fill it with 1, 2, 3... | |
size_t big_sz = 100; | |
double* lg_array = (double*)alloc.allocate(100*sizeof(double)); | |
for(size_t i = 0; i < big_sz; i++){ | |
lg_array[i] = 1.0 * i; | |
} | |
// Note the ammount of memory currently allocated | |
std::cout<< "Memory used after allocating lg_array: " << alloc.getCurrentSize() << "\n"; | |
// Use the vector(begin, end) constructor (passing our custom allocator to track the allocations) | |
std::vector< double, umpire::TypedAllocator<double> > sm_vector(lg_array, lg_array+5, vector_allocator); | |
// Note memory for 5 more doubles has been allocated! | |
std::cout<< "Memory used after creating sm_vector: " << alloc.getCurrentSize() << "\n"; | |
// The elements of the vector have the same value initally | |
std::cout<< "sm_vector = ["; | |
for(auto& elm: sm_vector){ | |
std::cout << " "<< elm << ","; | |
} | |
std::cout << "]\n"; | |
// but if they are modified... | |
for(auto&& elm: sm_vector){ | |
elm += 100; | |
} | |
std::cout << "sm_vector (after modify) = ["; | |
for(auto& elm: sm_vector){ | |
std::cout << " "<< elm << ","; | |
} | |
std::cout << "]\n"; | |
// The original elements in the lg_array are not, differnt memory no connection between the two. | |
std::cout << "First 5 elements of the original lg_array = ["; | |
for(size_t i = 0; i < 5; i++){ | |
std::cout << " "<< lg_array[i] << ","; | |
} | |
std::cout << "]\n"; | |
std::cout << "size of allocation lg_array: " << rm.getSize(lg_array) << "\n"; | |
std::cout << "size of allocation sm_vector.data(): " << rm.getSize(sm_vector.data()) << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment