allocation

Dynamic allocation of 2D array within function (using pointers to return adress of allocated object)

天大地大妈咪最大 提交于 2019-12-29 08:24:38
问题 I'd /ike to know, how to pass pointers to dynamically allocated arrays using function arguments. This function is supposed to allocate array 10x10 (checks skipped for simplicity sake). Is this possible? What am i doing wrong? Thanks in advance. int array_allocate2DArray ( int **array, unsigned int size_x, unsigned int size_y) { array = malloc (size_x * sizeof(int *)); for (int i = 0; i < size_x; i++) array[i] = malloc(size_y * sizeof(int)); return 0; } int main() { int **array; array

char** memory allocation using new operator

雨燕双飞 提交于 2019-12-24 15:24:13
问题 I'm truely baffled by this throwing an error.... char** results = new char*[numRes]; //this is where it breaks for(int i = 0; i < numRes; i++) { results[i] = new char[64]; } It's throwing a corruption of the heap error. but surely it should work? Im assigning 4 char* to a list of character pointers so I can pass them into functions etc. I looked around everywhere but they all seem to be showing the malloc and free... Im using them in classes so I want to stick to c++ new and delete. Could

.NET deletes pinned allocated buffer

ぐ巨炮叔叔 提交于 2019-12-24 12:08:07
问题 I have the following code to allocate buffer uns16 m_rawBuffer = new uns16[m_rawBufferSize]; pin_ptr<uns16> ptrAcqBuffer = m_rawBuffer; Although it is pin_ptr from time to time GC modifies ptrAcqBuffer. From the document I see A pinning pointer is an interior pointer that prevents the object pointed to from moving on the garbage-collected heap. That is, the value of a pinning pointer is not changed by the common language runtime. This is required when you pass the address of a managed class

The cost of memory allocation in a loop in C

江枫思渺然 提交于 2019-12-24 10:55:38
问题 Is there a significant difference between allocating an array inside or outside a loop in terms of cost of time? I use many arrays in functions inside a loop in my program, should I pass all the arrays as function parameters to increase the performance although it decreases the readability? For example: #include <stdlib.h> #define N 1000000 void foo() { int* array = (int*)malloc(N*sizeof(int)); /* Do something with the array */ free(array); } int main() { int i; for(i=0; i<1000000; i++) foo()

stl vector reserve

对着背影说爱祢 提交于 2019-12-24 09:49:36
问题 I was trying to pre-allocate a vector of integer like this vector<int> tmp_color; tmp_color.reserve(node_obj[node].c_max); for(int tmp_color_idx = 0; tmp_color_idx < node_obj[node].c_max; tmp_color_idx++) tmp_color[tmp_color_idx] = tmp_color_idx; where node_obj[node].c_max is > 0 (I checked). size of tmp_color appears to be zero anyways, after the for loop. If there something wrong with the code? Thanks 回答1: If you want to make assignments in the loop as you wrote I suggest the following way:

How do I know when I ought to free strings in C returned by library functions?

时光毁灭记忆、已成空白 提交于 2019-12-24 04:56:12
问题 Which strings ought I to free in C on my own, using free() ¹? My state of knowledge: char a[256]; : no char *a = "abcdefg"; : no char *a = malloc(15L); : yes a string returned by getenv() : no strings returned by Windows functions²: ??? ¹ or LocalFree()/ GlobalFree() / VirtualFree() ² in particular by GetCommandLineW() 回答1: This will always be mentioned in the documentation for any API you use that returns strings (or other data larger than a single simple value such as an integer). Yes, this

Linux C/C++ allocate/deallocate memory in dynamic library

主宰稳场 提交于 2019-12-23 20:31:30
问题 I have to split my application into several logical modules. mainapp : module1.so module2.so module3.so and so on Where each module is an *.so library, which will be loaded during runtime. Each module shares the same interface and will return some array of data. For example: int *ptr = module1->getIntData(); Is it OK, to free/delete this memory on mainapp side? int *ptr = module1->getIntData(); delete ptr; //(or free(ptr)) What about a malloc/free implementations. Is it possible, that library

Fixed allocation std::vector

99封情书 提交于 2019-12-23 12:25:44
问题 I'm an embedded software developer and as such I can't always use all the nice C++ features. One of the most difficult things is avoiding dynamic memory allocation as it is somewhat universal with all STL containers. The std::vector is however very useful when working with variable datasets. The problem though is that the allocation(e.g. std::reserve ) isn't done at initialization or fixed. This means that memory fragmentation can occur when a copy occurs. It would be great to have every

Clang complains: “pointer is initialized by a temporary array”

橙三吉。 提交于 2019-12-23 11:52:18
问题 I have an array of (pointers to) arrays of different lengths, which I learned I could define using compound literals: const uint8_t *const minutes[] = { (const uint8_t[]) {END}, (const uint8_t[]) {1, 2, 3, 4, 5 END}, (const uint8_t[]) {8, 9, END}, (const uint8_t[]) {10, 11, 12, END}, ... }; gcc accepts this just fine, but clang says: pointer is initialized by a temporary array, which will be destroyed at the end of the full-expression . What does this mean? The code seems to be working, but

Local variables construction and destruction with optimizer involved

五迷三道 提交于 2019-12-23 09:48:36
问题 If I have this code: class A { ... }; class B { ... }; void dummy() { A a(...); B b(...); ... } I know that variables a and b will be destroyed in reverse allocation order ( b will be destroyed first, then a ); but can I be sure that the optimizer will never swap the allocation and construction of a and b ? Or I must use volatile to enforce it? 回答1: The only guarantees are that any observable side effects (that is, reads and writes to volatile objects and calls to I/O functions) of the