dynamic-memory-allocation

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

人盡茶涼 提交于 2019-12-05 22:20:19
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 size is between 1kb and 30kb for my datasets) Then the program checks different combinations of 'x'

Can i allocate memory faster by using multiple threads?

点点圈 提交于 2019-12-05 16:58:16
If i make a loop that reserves 1kb integer arrays, int[1024], and i want it to allocate 10000 arrays, can i make it faster by running the memory allocations from multiple threads? I want them to be in the heap. Let's assume that i have a multi-core processor for the job. I already did try this, but it decreased the performance. I'm just wondering, did I just make bad code or is there something that i didn't know about memory allocation? Does the answer depend on the OS? please tell me how it works on different platforms if so. Edit: The integer array allocation loop was just a simplified

Why does memory allocated from inside a DLL become invalid after FreeLibrary()?

时光总嘲笑我的痴心妄想 提交于 2019-12-05 12:30:04
I had this bug today which turned out to be because I use a string allocated from inside my DLL after calling FreeLibrary() . This is a simple example reproducing the crash. This goes in DLL: void dllFunc(char **output) { *output = strdup("Hello"); // strdup uses malloc } This is in the EXE that loads the DLL: void exeFunc() { char *output; dllFunc(&output); std::string s1 = output; // This succeeds. FreeLibrary(dll); std::string s2 = output; // This crashes with access violation. } I read the documentation of FreeLibrary() but I couldn't find anything about memory becoming invalid after it's

Example of memory leak in c++ (by use of exceptions)

自作多情 提交于 2019-12-05 08:37:44
In C ++ How to program there is a paragraph that say: A common programming practice is to allocate dynamic memory, assign the address of that memory to a pointer, use the pointer to manipulate the memory and deallocate the memory with delete when the memory is no longer needed. If an exception occurs after successful memory allocation but before the delete statement executes, a memory leak could occur. The C++ standard provides class template unique_ptr in header to deal with this situation. Any on could introduce me a real example that exception occur and memory will leak like this post ? Sam

What alignment issues limit the use of a block of memory created by malloc?

不羁的心 提交于 2019-12-05 06:09:31
I am writing a library for various mathematical computations in C. Several of these need some "scratch" space -- memory that is used for intermediate calculations. The space required depends on the size of the inputs, so it cannot be statically allocated. The library will typically be used to perform many iterations of the same type of calculation with the same size inputs, so I'd prefer not to malloc and free inside the library for each call; it would be much more efficient to allocate a large enough block once, re-use it for all the calculations, then free it. My intended strategy is to

Small objects allocator

本秂侑毒 提交于 2019-12-05 05:40:27
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. Andrei Alexandrescu I suggest you ask Rich Sposato . He has done extensive work on Loki's small object allocator, including testing and benchmarks. Have you considered using

Is Big-O of the C++ statement 'delete [] Q;' O(1) or O(n)?

家住魔仙堡 提交于 2019-12-05 05:01:29
Title is self-explanatory. Very easy question. I think it's O(n) but wanted to verify before my final tomorrow. The short answer is... it depends. If Q is a pointer to an array of objects that have destructors, then delete[] Q will need to call all of those destructors. This will call O(n) destructors, where n is the number of elements in the array. On the other hand, if Q points to an array of objects that don't have destructors (for example, int s or simple struct s), then no destructors need to be called, which takes only O(1) time. Now note that those destructors don't have to run in O(1)

Reading all content from a text file - C

妖精的绣舞 提交于 2019-12-05 02:25:53
问题 I am trying to read all content from a text file. Here is the code which I wrote. #include <stdio.h> #include <stdlib.h> #define PAGE_SIZE 1024 static char *readcontent(const char *filename) { char *fcontent = NULL, c; int index = 0, pagenum = 1; FILE *fp; fp = fopen(filename, "r"); if(fp) { while((c = getc(fp)) != EOF) { if(!fcontent || index == PAGE_SIZE) { fcontent = (char*) realloc(fcontent, PAGE_SIZE * pagenum + 1); ++pagenum; } fcontent[index++] = c; } fcontent[index] = '\0'; fclose(fp)

realloc() invalid old size

强颜欢笑 提交于 2019-12-04 23:46:51
问题 I am doing an exercise for fun from KandR C programming book. The program is for finding the longest line from a set of lines entered by the user and then prints it. Here is what I have written (partially, some part is taken from the book directly):- #include <stdio.h> #include <stdlib.h> int MAXLINE = 10; int INCREMENT = 10; void copy(char longest[], char line[]){ int i=0; while((longest[i] = line[i]) != '\0'){ ++i; } } int _getline(char s[]){ int i,c; for(i=0; ((c=getchar())!=EOF && c!='\n'

Heap memory allocation

六月ゝ 毕业季﹏ 提交于 2019-12-04 16:33:34
问题 If I allocate memory dynamically in my program using malloc() but I don't free the memory during program runtime, will the dynamically allocated memory be freed after program terminates? Or if it is not freed, and I execute the same program over and over again, will it allocate the different block of memory every time? If that is the case, how should I free that memory? Note: one answer I could think of is rebooting the machine on which I am executing the program. But if I am executing the