dynamic-memory-allocation

Creation of Dynamic Array of Dynamic Objects in C++

天大地大妈咪最大 提交于 2020-01-31 05:38:26
问题 I know how to create a array of dynamic objects. For example, the class name is Stock. Stock *stockArray[4]; for(int i = 0 ; i < 4;i++) { stockArray[i] = new Stock(); } How do you change this to dynamic array of dynamic objects? What I tried: Stock stockArrayPointer = new Stock stock[4]; It doesn't work and the error is "The value of Stock** cannot be used to initalize an entity of type Stock. Second question is after the creation of dynamic array of dynamic objects, what is the syntax to

difference between new[ ] / delete [ ] vs new / delete in C++ [duplicate]

﹥>﹥吖頭↗ 提交于 2020-01-30 08:08:04
问题 This question already has answers here : Is delete[] equal to delete? (6 answers) Closed 4 years ago . I have a very quick question: What is the difference between new[ ] / delete [ ] vs new / delete in C++ when it comes to Dynamic memory? Is new[ ] / delete [ ] not belong to Dynamic memory? 回答1: new allocates memory for a single item and calls its constructor, and delete calls its destructor and frees its memory. new[] allocates memory for an array of items and calls their constructors, and

Program getting struck near malloc

半腔热情 提交于 2020-01-26 03:55:08
问题 I have use the code like below char *es_data; fp_input = fopen(inp_path, "rb"); fseek(fp_input, 0, SEEK_END); file_size = ftell(fp_input); fseek(fp_input, 0, SEEK_SET); es_data = (char*)malloc(file_size); fread(es_data, 1, file_size, fp_input); I have a file of 185mb, i.e., file_size = 190108334 bytes. For this file, malloc is crashing, and program is getting struck at this stage. If i use any other file of lower size, it works fine. What can I do ? 回答1: You should test at least that fopen

Is initializer evaluated after memory allocation in new expression?

╄→尐↘猪︶ㄣ 提交于 2020-01-24 02:58:13
问题 Consider the code auto p = new T( U(std::move(v)) ); The initializer is then U(std::move(v)) . Let's assume that T( U(std::move(v)) ) does not throw. If the initializer is evaluated after the underlying memory allocation, the code is then strong-exception-safe. Otherwise, it is not. Had memory allocation thrown, v would have already been moved. I'm therefore interested in the relative order between memory allocation and initializer evaluation. Is it defined, unspecified, or what? 回答1: Yes,

Small objects allocator

不问归期 提交于 2020-01-23 06:16:56
问题 Has anybody used SmallObjectAllocator from Modern C++ Design by Andrei Alexandrescu in a big project? I want to implement this allocator but I need some opinions about it before using it in my project. I made some tests and it seems very fast, but the tests were made in a small test environment. I want to know how fast it is when are lots of small objects(like events, smart pointers, etc) and how much extra memory it uses. 回答1: I suggest you ask Rich Sposato. He has done extensive work on

Calling free on a pointer twice

独自空忆成欢 提交于 2020-01-22 07:09:34
问题 I have been taught in lectures, that calling free() on a pointer twice is really, really bad. I know that it is good practice, to set a pointer to NULL , right after having freed it. However, I still have never heard any explanation as to why that is. From what I understand, the way malloc() works, it should technically keep track of the pointers it has allocated and given you to use. So why does it not know, whether a pointer it receives through free() has been freed yet or not? I would love

Calling free on a pointer twice

喜欢而已 提交于 2020-01-22 07:09:19
问题 I have been taught in lectures, that calling free() on a pointer twice is really, really bad. I know that it is good practice, to set a pointer to NULL , right after having freed it. However, I still have never heard any explanation as to why that is. From what I understand, the way malloc() works, it should technically keep track of the pointers it has allocated and given you to use. So why does it not know, whether a pointer it receives through free() has been freed yet or not? I would love

Memory allocation on GPU for dynamic array of structs

喜欢而已 提交于 2020-01-19 13:09:47
问题 I have problem with passing array of struct to gpu kernel. I based on this topic - cudaMemcpy segmentation fault and I wrote sth like this: #include <stdio.h> #include <stdlib.h> struct Test { char *array; }; __global__ void kernel(Test *dev_test) { for(int i=0; i < 5; i++) { printf("Kernel[0][i]: %c \n", dev_test[0].array[i]); } } int main(void) { int n = 4, size = 5; Test *dev_test, *test; test = (Test*)malloc(sizeof(Test)*n); for(int i = 0; i < n; i++) test[i].array = (char*)malloc(size *

How to detect memory leak when memory allocation number isn't always same?

孤人 提交于 2020-01-15 09:52:48
问题 I got a memory leak (55 bytes) on my program like this. I am using C++, MFC, Visual Studio 2010. Detected memory leaks! {13497} normal block at 0x0E44C248, 55 bytes long. Data: 44 3A 5C 46 44 41 53.... the problem is, the memory allocation number "13497" is not always same. it's always different number if I run the program again. I wanted to find where I didn't release the memory before the exit, with _crtBreakAlloc, but it seems not possible to break on a memory allocation number. I used

How to detect memory leak when memory allocation number isn't always same?

浪尽此生 提交于 2020-01-15 09:52:35
问题 I got a memory leak (55 bytes) on my program like this. I am using C++, MFC, Visual Studio 2010. Detected memory leaks! {13497} normal block at 0x0E44C248, 55 bytes long. Data: 44 3A 5C 46 44 41 53.... the problem is, the memory allocation number "13497" is not always same. it's always different number if I run the program again. I wanted to find where I didn't release the memory before the exit, with _crtBreakAlloc, but it seems not possible to break on a memory allocation number. I used