dynamic-memory-allocation

Mixing operator new[] and placement new with ordinary delete[]

谁说胖子不能爱 提交于 2019-11-29 09:13:42
Just out of curiosity, is the following legal? X* p = static_cast<X*>(operator new[](3 * sizeof(X))); new(p + 0) X(); new(p + 1) X(); new(p + 2) X(); delete[] p; // Am I allowed to use delete[] here? Or is it undefined behavior? Similarly: X* q = new X[3](); (q + 2)->~X(); (q + 1)->~X(); (q + 0)->~X(); operator delete[](q); I'm pretty sure both give UB. §5.3.4/12 says the array form of a new expression may add some arbitrary amount of overhead to the amount of memory allocated. The array delete can/could then do something with the extra memory it expects to be there, but isn't since you didn't

free allocated memory in 2D dynamic memory allocation array in C++

末鹿安然 提交于 2019-11-29 08:51:27
I recently posted a question about this, but this is a different question. I created a 2D arrays using dynamic memory allocation, after the matrix is used, we need to free the memory by delete it and I dont understand why we cant just use delete [] matrix to delete it instead of the method in the codes below int **matrix; // dynamically allocate an array matrix = new int *[row]; for (int count = 0; count < row; count++) matrix[count] = new int[col]; // free dynamically allocated memory for( int i = 0 ; i < *row ; i++ ) { delete [] matrix[i] ; delete [] matrix ; } Because the problem is because

Assembly x86 brk() call use

喜欢而已 提交于 2019-11-29 07:48:17
i am trying to dynamically allocate memory into the heap and then assign values in those memory addresses. I understand how to allocate the memory but how would i assign for example the value in a register to that first dynamic memory address? This is what i have so far:` push rbp mov rbp, rsp ;initialize an empy stack to create activation records for the rest of the subroutines mov rax, 0x2d ;linux system call for brk() mov rbx, 0x0 ;to get the adress of the first adress we are allocating we must have 0 in rbx int 0x80 ;calls the linux operating system kernel for assistance mov [brk

Can a memory block allocated by using operator new/malloc persist beyond end of program execution? [duplicate]

送分小仙女□ 提交于 2019-11-29 05:51:53
Possible Duplicate: When you exit a C application, is the malloc-ed memory automatically freed? This question came to my mind when i was reading about how compulsory it is to use delete/free respectively when it comes to dynamic memory allocation in C/C++. I thought if the memory allocation persisted beyond the termination of my program execution, then yes it is compulsory; otherwise, why do i have to worry about freeing up the allocated space? Isn't the OS going to free it up automatically with process termination? How right am i? My question is that can int *ip = new int(8); persist beyond

How are malloc and free implemented?

送分小仙女□ 提交于 2019-11-29 01:46:31
I want to implement my own dynamic memory management system in order to add new features that help to manage memory in C++. I use Windows (XP) and Linux (Ubuntu). What is needed to implement functions like 'malloc' and 'free'? I think that I have to use lowest level system calls. For Windows, I have found the functions: GetProcessHeap, HeapAlloc, HeapCreate, HeapDestroy and HeapFree. For Linux, I have not found any system calls for heap management. On Linux, malloc and free are system calls, are not they? Thanks Edit: C++ does not provide garbage collector and garbage collector is slow. Some

new operator for memory allocation on heap

。_饼干妹妹 提交于 2019-11-28 22:22:16
问题 I was looking at the signature of new operator. Which is: void* operator new (std::size_t size) throw (std::bad_alloc); But when we use this operator, we never use a cast. i.e int *arr = new int; So, how does C++ convert a pointer of type void* to int* in this case. Because, even malloc returns a void* and we need to explicitly use a cast. 回答1: There is a very subtle difference in C++ between operator new and the new operator. (Read that over again... the ordering is important!) The function

Malloc function (dynamic memory allocation) resulting in an error when it is used globally

耗尽温柔 提交于 2019-11-28 20:52:48
#include<stdio.h> #include<string.h> char *y; y=(char *)malloc(40); // gives an error here int main() { strcpy(y,"hello world"); } error: conflicting types for 'y' error: previous declaration of 'y' was here warning: initialization makes integer from pointer without a cast error: initializer element is not constant warning: data definition has no type or storage class warning: passing arg 1 of `strcpy' makes pointer from integer without cast Now the real question is, can't we make the dynamic memory allocation globally? Why does it show an error when I use malloc globally? And the code works

Pimpl idiom without using dynamic memory allocation

孤人 提交于 2019-11-28 16:43:11
we want to use pimpl idiom for certain parts of our project. These parts of the project also happen to be parts where dynamic memory allocation is forbidden and this decision is not in our control. So what i am asking is, is there a clean and nice way of implementing pimpl idiom without dynamic memory allocation? Edit Here are some other limitations: Embedded platform, Standard C++98, no external libraries, no templates. Warning: the code here only showcases the storage aspect, it is a skeleton, no dynamic aspect (construction, copy, move, destruction) has been taken into account. I would

Using realloc inside a function [duplicate]

心不动则不痛 提交于 2019-11-28 13:08:44
This question already has an answer here: C Programming: malloc() inside another function 8 answers My apologies, I know many related questions have already been asked, so I will keep it very simple. Despite some years of programming I cannot find the correct syntax for resizing and modifying an array (or several) inside a function. For example, say I want a function to fill an array with a set of "n" numbers, where "n" is defined within the array: int main(int argc, char *argv[]) { float *data = NULL int n = myfunction(data); for(i=0;i<n;i++) printf("%f\n",data[i]); free(data); } int

Default capacity of std::string?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 07:28:01
问题 When I create a std::string using the default constructor, is ANY memory allocated on the heap? I'm hoping the answer does not depend on the implementation and is standardized. Consider the following: std::string myString; 回答1: Unfortunately, the answer is no according to N3290. Table 63 Page 643 says: data() a non-null pointer that is copyable and can have 0 added to it size() 0 capacity() an unspecified value The table is identical for C++03. 回答2: It is implementation dependent. Some string