dynamic-memory-allocation

How can i find the size of a dynamically allocated array in C?

最后都变了- 提交于 2019-12-10 22:31:56
问题 I've made an array that is dynamically allocated by a cycle. And then a cycle that reads the numbers out of the array but i need to know the size of the array. The array is correct and fully working and has correct values in it. I defined the array like this: int *array; Now when i want to use this it wont work because im using a pointer: int size = sizeof(array)/sizeof(array[0]); How can i fix it so it works with my pointer? 回答1: I assume you are allocating the array using one of new or

Allocating memory inside loop vs outside loop

荒凉一梦 提交于 2019-12-10 21:11:56
问题 Is there noticeable performance penalty for allocating LARGE chunks of heap memory in every iteration of loop? Of course I free it at the end of each iteration. An alternative would be to allocate once before entering the loop, repeatedly using it in all iterations, and eventually freeing it up after exiting the loop. See the code below. // allocation inside loop for(int i = 0; i < iter_count; i++) { float *array = new float[size](); do_something(array); delete []array; } // allocation

How to initialize a 2D array in which the size is determined by argc and argv?

ⅰ亾dé卋堺 提交于 2019-12-10 20:29:08
问题 I am working on code which will keep track of each time a specific element in an array is accessed. The array itself will be allocated dynamically based off of the inputs by the user, and so no functions that I have seen are what I'm looking for. To be more specific, how do I dynamically allocate the rows and columns of an array, and then initialize every element to 0? Ex. ./SIM A B int* array_columns = malloc(atoi(argv[1]) * sizeof(int)); int* array_rows = malloc(atoi(argv[2]) * sizeof(int))

C++ performance: checking a block of memory for having specific values in specific cells

南笙酒味 提交于 2019-12-10 09:56:40
问题 I'm doing a research on 2D Bin Packing algorithms. I've asked similar question regarding PHP's performance - it was too slow to pack - and now the code is converted to C++. It's still pretty slow. What my program does is consequently allocating blocks of dynamic memory and populating them with a character 'o' char* bin; bin = new (nothrow) char[area]; if (bin == 0) { cout << "Error: " << area << " bytes could not be allocated"; return false; } for (int i=0; i<area; i++) { bin[i]='o'; } (their

Critical error detected c0000374 - C++ dll returns pointer off allocated memory to C#

只愿长相守 提交于 2019-12-10 02:47:51
问题 I have a c++ dll which serving some functionality to my main c# application. Here i try to read a file, load it to memory and then return some information such as the Pointer to loaded data and count of memory blocks to c#. The Dll reads file to memory successfully but on the return to the main application, program crashes due to Heap Corruption(Critical error detected c0000374). The code is quite simple and straightforward and I have done some similar things before with no problem, However i

C++: Static pointers, static objects and dynamic memory allocation

爷,独闯天下 提交于 2019-12-10 00:58:46
问题 Consider the below code segment: #include <iostream> using namespace std; class p { public: int* q; p() { q = new int(100); } ~p(){ delete q; } }; static p* p1 = new p(); static p p2; int main() { // your code goes here std::cout << *(p1->q); std::cout << *(p2.q); delete p1; } p1 and p2 are static vars, they have to stored in static segment. since p1 is a pointer, is only the pointer address stored in static segment or even the object it points to? p2 is a normal static object, but it

dynamic memory in QList

穿精又带淫゛_ 提交于 2019-12-09 19:06:56
问题 I don't have much experience with QT and this problem came out today. QList<int> memList; const int large = 100000; getchar(); for (int i=0; i<large; i++) { memList.append(i); } cout << memList.size() << endl; getchar(); for (int i=0; i<large; i++) { memList.removeLast(); } cout << memList.size() << endl; getchar(); After first loop when I check memory usage it goes up as new elements are appended to the memList but after removing them within second loop the memory usage stays at the same

Exception thrown: read access violation. **dynamicArray** was 0x1118235. occurred

别说谁变了你拦得住时间么 提交于 2019-12-08 15:33:37
问题 #include "pch.h" #include <iostream> #include <string> using namespace std; int **dynamicArray ; int ROWS, COLUMNS; //--------------------------------- int input_matrix(int ROWS, int COLUMNS) { //--------------------------------------- //memory allocated for elements of rows. int **dynamicArray = new int *[ROWS]; //memory allocated for elements of each column. for (int i = 0; i < ROWS; i++) dynamicArray[i] = new int [COLUMNS]; //free the allocated memory for (int i = 0; i < ROWS; i++) delete[

STL within embedded system with very limited memory

﹥>﹥吖頭↗ 提交于 2019-12-08 15:19:12
问题 I'm currently in the process of building an embedded system, using an ARM Cortex M3 processor, with 64 KB of SRAM. At the moment, I'm looking for a way to ensure deterministic performance with STL containers, which includes ensuring that I cannot end up running out of memory at run-time. I'm primarily concerned with how STL containers perform dynamic memory allocation. Although I can utilize a custom allocator to have these structures get memory from a pool which I set aside, I would need to

Problem allocating derived class array with new

烂漫一生 提交于 2019-12-08 09:19:16
问题 I have a simple program $ cat a.cpp #include <iostream> class MyClass { public: virtual void check() { std::cout << "Inside MyClass\n"; } }; class MyClass2: public MyClass { public: int* a; virtual void check() { std::cout << "Inside MyClass2\n"; } }; int main() { MyClass *w, *v; w = new MyClass2[2]; v = new MyClass2; std::cout << "Calling w[0].check\n"; w[0].check(); std::cout << "Calling v->check\n"; v->check(); std::cout << "Calling w[1].check\n"; w[1].check(); } $ g++ a.cpp $ ./a.out