all,
If you use boost pool library, how would you replace the following statement:
MyStruct *someStruct = (MyStruct *) calloc(numOfElements, sizeof(MyStruct));
If it was for one element, I would do:
boost::object_pool<MyStruct> myPool;
MyStruct *someStruct = myPool.malloc();
but since "numOfElements" is a variable, I have the feeling executing a loop of malloc() is not a good idea?
I'd say you need to use pool_alloc interface:
static pointer allocate(size_type n);
static pointer allocate(size_type n, pointer);
static void deallocate(pointer ptr, size_type n);
Sample from http://www.boost.org/doc/libs/1_47_0/libs/pool/doc/interfaces.html
void func()
{
std::vector<int, boost::pool_allocator<int> > v;
for (int i = 0; i < 10000; ++i)
v.push_back(13);
} // Exiting the function does NOT free the system memory allocated by the pool allocator
// You must call
// boost::singleton_pool<boost::pool_allocator_tag, sizeof(int)>::release_memory()
// in order to force that
Related
I am using a embedded board with FreeRTOS.
In a task, I defined two structs and use pvPortMalloc to allocate memory. (One struct is a member in the other)
Besides, I pass the address of struct to some functions.
However, there are some issues about freeing the memory using vPortFree.
The following is my code (test_task.c):
/* Struct definition */
typedef struct __attribute__((packed)) {
uint8_t num_parameter;
uint32_t member1;
uint8_t member2;
uint8_t *parameter;
}struct_member;
typedef struct __attribute__((packed)) {
uint16_t num_member;
uint32_t class;
struct_member *member;
}struct_master;
I define a global struct and an array below.
uint8_t *arr;
struct_master master:
Function definition:
void decode_func(struct_master *master, uint8_t *arr)
{
master->member = pvPortMalloc(master->num_member);
for(int i = 0; i < scr->num_command; ++i){
master->member[i].parameter = pvPortMalloc(master->member[i].num_parameter);
do_something();
}
}
The operation task is shown in the following.
At the end of task, I would like to free memory:
void test_task()
{
decode_func( &master, arr);
do_operation();
vPortFree(master.member);
for (int i = 0; i < master.num_member; ++i)
vPortFree(master.member[i].parameter);
hTest_task = NULL;
vTaskDelete(NULL);
}
It is ok to free master.member.
However, when the program tried free master.member[i].parameter,
it seems that freeing had been executed before and software just reset automatically.
Does anyone know why it happened like that?
At the very first glance, the way you allocate for members is wrong in the decode_func.
I assume that master->num_member indicates the number of struct members that master should contain.
master->member = pvPortMalloc(master->num_member);
should be corrected to,
master->member = pvPortMalloc(master->num_member * sizeof(struct_member));
Again, in the same function the loop seems a bit suspicious as well.
for(int i = 0; i < scr->num_command; ++i){
master->member[i].parameter = pvPortMalloc(master->member[i].num_parameter);
do_something();
}
I'm not sure what src->num_command indicates, but naturally I reckon the loop should execute until i < master->num_member. I assume your loop should be updated as follows as well,
for(int i = 0; i < master->num_member; ++i){
master->member[i].parameter = pvPortMalloc(master->member[i].num_parameter * sizeof(uint8_t));
do_something();
}
While doing the freeing of memory, make sure you free the contained members first before freeing the container structure. Therefore you should first free all the parameters and then the member, so change that order in test_task function as well.
Also make sure that before doing vTaskDelete(NULL); you must deallocate all the resources consumed by test_task, otherwise there will be a resource leak. vTaskDelete(NULL) will simply mark the TCB of that particular task as ready to be deleted so that at some time later the idle task will purge the TCB related resources.
Generally, when you free an object, the contents of the object are destroyed and you can't access them anymore. So when you want to free nested allocations like this, you need to free the inner allocations first and only free the outer (master) allocation afterwards. In other words:
for (int i = 0; i < master.num_member; ++i)
vPortFree(master.member[i].parameter);
vPortFree(master.member);
free the parameters first and then the containing member array.
struct A { int x; };
int main() {
int A::* pt = &A::x;
return 0;
}
what does int A::* mean exactly? I have never seen C++ syntax like this.
Just like other traits, you specify the template argument and use the value member.
std::is_member_object_pointer<decltype(pa) >::value
what does int A::* mean exactly?
That is a type declaration of a member object pointer to an int member of the class A.
As the unmanaged C++ is feel free to locate to different object.
Such as the pointer to an array
int pt[50];
int* pointer = pt;
We can directly use *pointer to get the first value of the element in the array.
Thus, we can also use *(pointer++) in order to point to the second element.
However, if it is possible to directly use ^(pointer+5) to get the sixth element of the array?
The example is as follow.
array<int>^ pt = gcnew array<int>(50);
int^ pointer = pt;
How can I use the pointer as a medium to access different element in the array?
here is a slightly different approach that may be of use ...
It uses interior pointer arithmetic (to traverse an array).
Hope this helps.
using namespace System;
ref class Buf
{
// ...
};
int main()
{
array<Buf^>^ array_of_buf = gcnew array<Buf^>(10);
// Create a Buf object for each array position
for each (Buf^ bref in array_of_buf)
{
bref = gcnew Buf();
}
// create an interior pointer to elements of the array
interior_ptr<Buf^> ptr_buf;
// loop over the array with the interior pointer
// using pointer arithmetic on the interior pointer
for (ptr_buf = &array_of_buf[0]; ptr_buf <= &array_of_buf[9]; ptr_buf++)
{
// dereference the interior pointer with *
Buf^ buf = *ptr_buf;
// use the Buf class
}
}
reference: C++/CLi the visual c++ language for .net (pg 99)
here is another example from: https://msdn.microsoft.com/en-us/library/y0fh545k.aspx
// interior_ptr.cpp
// compile with: /clr
using namespace System;
ref class MyClass {
public:
int data;
};
int main() {
MyClass ^ h_MyClass = gcnew MyClass;
h_MyClass->data = 1;
Console::WriteLine(h_MyClass->data);
interior_ptr<int> p = &(h_MyClass->data);
*p = 2;
Console::WriteLine(h_MyClass->data);
// alternatively
interior_ptr<MyClass ^> p2 = &h_MyClass;
(*p2)->data = 3;
Console::WriteLine((*p2)->data);
}
This is the code.
int main()
{
unique_ptr <int> p {nullptr};
int val = 100;
p = &val; // Not working - compilation error
p = move(&val); // Not working - compilation error
cout << *p;
return 0;
}
What is the correct way?
Only dynamically allocated objects should be assigned to unique_ptrs, because the unique_ptr may try to delete the object.
As for the actual question, the reset() function of unique_ptr is used to reassign the pointer.
With unique_ptr::reset:
p.reset(&val);
Of course in this particular case this will result in undefined behavior when p goes out of scope and it tries to delete the int, but that's another matter.
i'm using vc++ 2013 express edition.
I'm studying some new feature of c++11 like std::async, and std::future.
I have a class Foo, with an std::shared_ptr<std::vector<unsigned int> >.
In Foo ctor i use std::make_shared to allocate in the heap the vector;
From Foo.h
Class Foo{
public:
Foo();
private:
std::shared_ptr<std::vector<unsigned int> > testVector;
unsigned int MAX_ITERATIONS = 800000000;
void fooFunction();
}
From Foo.cpp
Foo::Foo(){
testVector = std::make_shared<std::vector<unsigned int> >();
//fooFunction(); this take about 20 sec
std::async(std::launch::async, &Foo::fooFunction, this).get(); // and this about the same!!
}
void Foo:fooFunction(){
for (unsigned int i = 0; i < MAX_ITERATIONS; i++){
testVector->push_back(i);
}
}
Th problem is i can't see any gain between calling std::async(std::launch::async, &Foo::fooFunction, this).get(); and fooFunction();
Why??
Any help will be appreciated.
Best regards
std::async returns a std::future.
Calling get() on the future will make it wait until the result is available, then return it. So, even if it is run asynchronously, you are doing nothing but waiting for the result.
std::async doesn't magically parallelize the for loop in Foo::fooFunction.