dynamic-memory-allocation

How to allocate memory to int (*a)[2]?

好久不见. 提交于 2019-12-08 08:24:52
问题 How do I allocate memory to integer type (*a)[2] ? I am comfortable doing it with **a, *a etc. etc., also *a[2], but this looks different. Can somebody help out ? Thanks in advance. 回答1: Same as for any pointer type, say you have int (*a)[2]; a pointer a to arrays of 2 int , then you allocate a = malloc(number_of_rows * sizeof *a); to get a block of number_of_rows * (2 * sizeof (int)) bytes. You then access that with a[i][j] with 0 <= i < number_of_rows and 0 <= j < 2 . 来源: https:/

Dynamic Allocation in Template Class Constructor

三世轮回 提交于 2019-12-08 04:32:28
问题 I am working on a stack class and have two constructors. One of interest is this one. template <typename T> stack<T>::stack( const int n) { capacity = n ; size = 0 ; arr = new T [capacity] ; } I am calling it inside main like this. stack<int> s1(3) ; The program compiles fine but i get this run time error. 1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall stack<int>::~stack<int>(void)" (??1?$stack@H@@QAE@XZ) referenced in function _main 1>main.obj : error LNK2019:

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

纵饮孤独 提交于 2019-12-07 09:31:58
问题 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

how do you delete an object allocated with placement new

感情迁移 提交于 2019-12-07 07:09:51
问题 there are quite a few faces for the new operator in c++, but I'm interested in placement new. Suppose you allocate memory at a specific memory location int memoryPool[poolSize*sizeof(int)]; int* p = new (mem) int; //allocates memory inside the memoryPool buffer delete p; //segmentation fault How can I correctly deallocate memory in this case? What if instead of built-in type int I would use some class called myClass? myClass memoryPool[poolSize*sizeof(myClass )]; myClass * p = new (mem)

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

ぐ巨炮叔叔 提交于 2019-12-07 05:21:21
问题 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.

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

杀马特。学长 韩版系。学妹 提交于 2019-12-07 03:29:09
问题 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

Dynamic Allocation in Template Class Constructor

谁说我不能喝 提交于 2019-12-06 15:46:33
I am working on a stack class and have two constructors. One of interest is this one. template <typename T> stack<T>::stack( const int n) { capacity = n ; size = 0 ; arr = new T [capacity] ; } I am calling it inside main like this. stack<int> s1(3) ; The program compiles fine but i get this run time error. 1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall stack<int>::~stack<int>(void)" (??1?$stack@H@@QAE@XZ) referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall stack<int>::stack<int>(int)" (??0?$stack@H@@QAE@H@Z)

Are there any other semi/portable ways to dynamically allocate memory?

五迷三道 提交于 2019-12-06 05:49:28
this: char *buf = NULL; scanf("%ms", &buf); will get a dynamically allocated char buffer. I know this is limited to code compiled with Gcc (and specifically version 2.7 of glibc). I am also aware that the "correct" and portable way is to just use malloc() (and friends) to get the memory. I'm curious however, are there any other portable or semi-portable * implementations out there for getting dynamically allocated memory? I find tricks and tips of memory allocation in C a hard topic to "Google". Note: This is not "required" for anything, so there are no limitations on answers. * semi-portable

Why is deleted memory unable to be reused

霸气de小男生 提交于 2019-12-06 01:37:11
问题 I am using C++ on Windows 7 with MSVC 9.0, and have also been able to test and reproduce on Windows XP SP3 with MSVC 9.0. If I allocate 1 GB of 0.5 MB sized objects, when I delete them, everything is ok and behaves as expected. However if I allocate 1 GB of 0.25 MB sized objects when I delete them, the memory remains reserved (yellow in Address Space Monitor) and from then on will only be able to be used for allocations smaller than 0.25 MB. This simple code will let you test both scenarios

Sized Deallocation Feature In Memory Management in C++1y

血红的双手。 提交于 2019-12-05 23:09:52
问题 Sized Deallocation feature has been proposed to include in C++1y. However I wanted to understand how it would affect/improve the current c++ low-level memory management ? This proposal is in N3778 , which states following about the intent of this. With C++11 , programmers may define a static member function operator delete that takes a size parameter indicating the size of the object to be deleted. The equivalent global operator delete is not available. This omission has unfortunate