new-operator

“delete [] pointer” does not delete all the memory array? [duplicate]

∥☆過路亽.° 提交于 2019-12-11 03:38:51
问题 This question already has answers here : Can a local variable's memory be accessed outside its scope? (20 answers) Dynamically allocating an array of objects (7 answers) Closed 5 years ago . I'm pretty new to C++. I have to delete the pointer and memory allocation, once I complete the cycle. I am using new() for memory allocation and delete at the end to free the data. The program is as follows: int main() { float *ptr; ptr = new float[16]; for(int i=0; i<16; i++) { ptr[i] = 10.0f+i; cout <<

Global new operator overloading

只愿长相守 提交于 2019-12-11 03:14:02
问题 I have read about new and delete overloading for memory tracking in How_To_Find_Memory_Leaks I defined these global operators: inline void* __cdecl operator new( unsigned int size, const char *file, int line ) { void* ptr = malloc( size ); AddTrack((DWORD)ptr, size, file, line); return ptr; } inline void* __cdecl operator new( unsigned int size, void* ptr, const char *file, int line ) { return ptr; } It works well with new and new[] operators, but i have a problem with placement new ( second

Putting restrictions in overloading new and delete

倖福魔咒の 提交于 2019-12-11 02:28:27
问题 Is it possible to put some restrictions in overloading operators new and delete? My overloaded new is linked in a different file to my test program. The scenario is: if(condition is satisfied) call overloaded new else call the actual new defined in new.h 回答1: There are three ways to provide an operator new. replacing one or more of the four non placement default operators new, providing overload to the default operator new (thus with an additional parameter, those may be called with the

Generic Memory Pool - How to? - Design Issue

爱⌒轻易说出口 提交于 2019-12-11 02:25:09
问题 I am creating my own memory pool for small and very frequently used objects. I am good with the allocation and d-allocation itself. Here is layout of my Pool class CPool { unsigned int m_uiBlocks; unsigned int m_uiSizeOfBlock; unsigned int m_uiFreeBlocks; unsigned int m_uiInitialized; unsigned char *m_pMemStart; unsigned char *m_pNext; public: CPool(); ~CPool(); void CreatePool(size_t sizeOfEachBlock, unsigned int numOfBlocks); void DestroyPool(); unsigned char* AddrFromIndex(unsigned int i)

Is hiding a member in the base class a bad smell in the code?

爷,独闯天下 提交于 2019-12-11 01:59:50
问题 To me, it sounds more like an exception to hide a member in the base class in order to hack something instead of refactoring a bad design... Maybe I am wrong. But... Is it a code smell to hide a member in the base class with 'new' keyword? 回答1: It depends on the intent. If the purpose is to clarify the concrete return type, for example, then it might not be unreasonable. The classic example here is DbConnection . It is useful that SqlConnection re-declares CreateCommand etc, by returning

allocation of a pointers to fixed size arrays

你离开我真会死。 提交于 2019-12-11 01:32:10
问题 I have 2 doubts regarding basics of pointers usage. With the following code int (*p_b)[10]; p_b = new int[3][10]; // ..do my stuff delete [] p_b p_b is pointing to an array of 3 elements, each having fixed-size length of 10 int. Q1: How to declare p_b if I want that each element be a pointer to a fixed array size? Basically I want the following p_b[0] = pointer to a fixed-array size of 10 p_b[1] = pointer to a fixed-array size of 10 // ... and so on I was thinking to int (** p_b)[10] but then

An error is issued by gcc relative to parsing type-id in a new expression

僤鯓⒐⒋嵵緔 提交于 2019-12-10 20:13:32
问题 This program #include <cstddef> int main() { const std::size_t N1 = 2; const std::size_t N2 = 3; int ( **p )[N1] = new ( int ( *[N2] )[N1] ); } does not compile using the compiler C++ gcc HEAD 10.0.0 20190. The compiler issues error prog.cc: In lambda function: prog.cc:8:40: error: expected '{' before ')' token 8 | int ( **p )[N1] = new ( int ( *[N2] )[N1] ); | ^ prog.cc: In function 'int main()': prog.cc:8:34: error: no match for 'operator*' (operand type is 'main()::<lambda()>') 8 | int ( *

Operator new and bad_alloc on linux

笑着哭i 提交于 2019-12-10 20:12:36
问题 On Linux, malloc doesn't necessarily return a null pointer if you're out of memory. You might get back a pointer and then have the OOM killer start eating processes if you're really out of memory. Is the same true for c++'s operator new or will you get the bad_alloc exception? 回答1: The same is true for operator new, alas :^( 回答2: It's a kernel function rather than a language function - and you can control it with the vm.overcommit_memory and vm.overcommit_ratio sysctls. They're visible in the

C++ new & delete and functions

一世执手 提交于 2019-12-10 19:17:38
问题 This is a bit unclear to me... So, if I have a function: char *test(int ran){ char *ret = new char[ran]; // process... return ret; } and then call it multiple times: for(int i = 0; i < 100000000; i++){ char *str = test(rand()%10000000+10000000); // process... // delete[] str; // do i have to delete it here? } So the question is, do I have to use delete[] for each new[] call? 回答1: You don't have to. But if you don't delete memory you reserved with 'new' you will start running out of memory

Omitting c# new from jagged array initialization

旧城冷巷雨未停 提交于 2019-12-10 17:11:00
问题 From: http://msdn.microsoft.com/en-us/library/2s05feca.aspx Notice that you cannot omit the new operator from the elements initialization because there is no default initialization for the elements: int[][] jaggedArray3 = { new int[] {1,3,5,7,9}, new int[] {0,2,4,6}, new int[] {11,22} }; What does it mean? Why is it ok to omit new in: int[] arrSimp = { 1, 2, 3 }; int[,] arrMult = { { 1, 1 }, { 2, 2 }, { 3, 3 } }; but not possible in: int[][,] arrJagg = {new int[,] { { 1, 1} }, new int[,] { {