memory-management

Dynamically Growing an Array in C++

こ雲淡風輕ζ 提交于 2020-01-22 02:20:10
问题 I have an array of pointers of CName objects. I have the following constructor which initializes my array to size one. Then when I add an object I grow the array by 1 and add the new object. It compiles fine, however when I try to print them I just get segmentation fault error. Can you look and see if I'm doing anything wrong? //constructor Names_Book::Names_Book() { grow_factor = 1; size = 0; cNames = (CName**)malloc(grow_factor * sizeof(CName*)); cNames[0] = NULL; } void Names_Book:

Memory leak with Mat in convertTo-function

可紊 提交于 2020-01-21 20:05:38
问题 I have some trouble with managing memory in my function. Valgrid says I'm having a memory leak after the convert-function. Could it be because of the data is not being properly released? I've tried to use temp-pointers, but my program either crashes or is not working properly. Have someone encountered this problem before? this->images.push_back(new cv::Mat()); //ID cv::threshold(*this->images[MASK], *this->images[ID], 0.0, 1.0, cv::THRESH_BINARY); this->images[ID]->convertTo(*this->images[ID]

Dynamic allocation of memory

我的梦境 提交于 2020-01-21 11:47:29
问题 Lets consider following two codes First: for (int i=0;i<10000000;i++) { char* tab = new char[500]; delete[] tab; } Second: for (int i=0;i<10000000;i++) { char tab[500]; } The peak memory usage is almost the same, but the second code runs about 20 times faster than the first one. Question Is it because in first code array is stored on heap, and in the second one array is stored on stack? 回答1: Is it because in first code array is stored on heap, and in the second one array is stored on stack?

Dynamic allocation of memory

北城以北 提交于 2020-01-21 11:47:26
问题 Lets consider following two codes First: for (int i=0;i<10000000;i++) { char* tab = new char[500]; delete[] tab; } Second: for (int i=0;i<10000000;i++) { char tab[500]; } The peak memory usage is almost the same, but the second code runs about 20 times faster than the first one. Question Is it because in first code array is stored on heap, and in the second one array is stored on stack? 回答1: Is it because in first code array is stored on heap, and in the second one array is stored on stack?

Why retain count in negative value? [duplicate]

十年热恋 提交于 2020-01-21 11:39:07
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: NSString retain Count Is it possible that any object has its retain count in negative value ? I have this code NSString *str = [[NSString alloc] initWithString:@"Hello World"]; NSLog(@"String Retain Count: %i", [str retainCount]); this will return the retain count -1. Why this happened ? also I have done like this NSString *str = [[NSString alloc] init] still its return negative value in retain count . How this

iOS Download & Parsing Large JSON responses is causing CFData (store) leaks

非 Y 不嫁゛ 提交于 2020-01-21 10:20:33
问题 The first time a user opens my app I need to download lots of data. I get all of this data from the server in JSON form. Depending on the user, these JSON files can be anywhere from 10kb - 30mb each, and there are 10+ of them. I have no problems doing this when the JSONs have no more than 500 or so records, but like I said some have 10,000+ records and can be up to 30mb in size. When downloading the larger JSONs, my app allocs a ton of memory, until I eventually get memory warnings and the

What does zero-sized array allocation do/mean?

☆樱花仙子☆ 提交于 2020-01-21 07:25:18
问题 Looking at some example code and come across some zero-size array allocation. I created the following code snippet to clarify my question This is valid code: class T { }; int main(void) { T * ptr = new T[0]; return 0; } What is its use? Is ptr valid? Is this construct portable? 回答1: 5.3.4 in the C++ Standard: 6 Every constant-expression in a direct-new-declarator shall be an integral constant expression (5.19) and evaluate to a strictly positive value. The expression in a direct-new

What does zero-sized array allocation do/mean?

若如初见. 提交于 2020-01-21 07:24:28
问题 Looking at some example code and come across some zero-size array allocation. I created the following code snippet to clarify my question This is valid code: class T { }; int main(void) { T * ptr = new T[0]; return 0; } What is its use? Is ptr valid? Is this construct portable? 回答1: 5.3.4 in the C++ Standard: 6 Every constant-expression in a direct-new-declarator shall be an integral constant expression (5.19) and evaluate to a strictly positive value. The expression in a direct-new

What does deleting a pointer mean?

怎甘沉沦 提交于 2020-01-21 06:33:12
问题 Is deleting a pointer same as freeing a pointer (that allocates the memory)? 回答1: Deleting a pointer (or deleting what it points to, alternatively) means delete p; delete[] p; // for arrays p was allocated prior to that statement like p = new type; It may also refer to using other ways of dynamic memory management, like free free(p); which was previously allocated using malloc or calloc p = malloc(size); The latter is more often referred to as "freeing", while the former is more often called

What does deleting a pointer mean?

早过忘川 提交于 2020-01-21 06:33:09
问题 Is deleting a pointer same as freeing a pointer (that allocates the memory)? 回答1: Deleting a pointer (or deleting what it points to, alternatively) means delete p; delete[] p; // for arrays p was allocated prior to that statement like p = new type; It may also refer to using other ways of dynamic memory management, like free free(p); which was previously allocated using malloc or calloc p = malloc(size); The latter is more often referred to as "freeing", while the former is more often called