dynamic-memory-allocation Questions

1

Solved

As shown here, the behavior of allocate(0) is unspecified. So, what will happen if I call allocate_at_least(0) according to C++23 standard? Is the behavior implementation-defined, or will it be tre...

1

The code below allows you to see size of variable being deleted: #include <iostream> #include <stdlib.h> using namespace std; struct P { static void operator delete(void* ptr, std::si...
Charlenacharlene asked 13/7, 2023 at 0:48

4

Solved

#include <iostream> using namespace std; class base { int a; public: base() {a =0;} }; class derv :public base { int b; public: derv() {b =1;} }; int main() { base *pb = new der...
Extraterritorial asked 6/1, 2012 at 0:58

7

I have an Animal class with a virtual destructor, and a derived class Cat. #include <iostream> struct Animal { Animal() { std::cout << "Animal constructor" << std::end...
Anthropomorphize asked 15/5, 2023 at 11:18

1

Solved

Many answers on this site mentions that delete() calls destructor. But the sample code below seems to call delete() inside the destructor. What is the correct usage of delete() when object is initi...

2

Solved

I have problem with passing array of struct to gpu kernel. I based on this topic - cudaMemcpy segmentation fault and I wrote sth like this: #include <stdio.h> #include <stdlib.h> stru...
Hedwig asked 6/5, 2015 at 16:55

5

Solved

This is a minimal working example for the problem I am facing in my real code. #include <iostream> namespace Test1 { static const std::string MSG1="Something really big message&q...

31

Solved

What are the stack and heap? Where are they located physically in a computer's memory? To what extent are they controlled by the OS or language run-time? What is their scope? What determines their...

5

Solved

EDIT: A better title for this would be: polymorphism for large collections of objects without individual heap allocations. Suppose that I have a base class Animal with virtual functions and some de...

0

I have a very complex mathematical algorithm that's been in my code for years. Today, I did some high-level performance tests and found that this algorithm is eating a significant bit of the CPU ti...
Savadove asked 24/9, 2022 at 9:12

13

Solved

I have to do an assignment for my class and it says not to use Static arrays, only Dynamic arrays. I've looked in the book and online, but I don't seem to understand. I thought Static was created a...

3

Solved

Im not clearly sure what is the difference between those 2. My professor wrote that **array is same as *array[] and we were presented an example where he used **array (so after classes I tried exch...
Formulate asked 16/11, 2015 at 23:17

4

Solved

I have a c++ dll which serving some functionality to my main c# application. Here i try to read a file, load it to memory and then return some information such as the Pointer to loaded data and cou...
Adulate asked 5/5, 2014 at 11:18

8

Solved

I know that manual dynamic memory allocation is a bad idea in general, but is it sometimes a better solution than using, say, std::vector? To give a crude example, if I had to store an array of n ...
Enrollee asked 8/3, 2013 at 12:35

4

Solved

I am developing a C++ application, where the program run endlessly, allocating and freeing millions of strings (char*) over time. And RAM usage is a serious consideration in the program. This resul...

6

Solved

I'm new to C and heap memory, still struggling to understand dynamic memory allocation. I traced Linux system calls and found that if I use malloc to request a small amount of heap memory, then mal...
Mobocracy asked 23/9, 2020 at 13:34

7

Solved

I'm trying to understand the under-the-hood difference between these two char declarations: char* str1; char str2[10]; I understand that using char* gives a pointer to the first character in str1,...
Nimrod asked 29/9, 2021 at 12:47

5

I am sure this has been asked a million times but I couldn't find an answer that explained it. I have been told to never do this but I haven't really understood why. Why doesn't something like this...
Femme asked 16/4, 2021 at 13:9

1

Solved

Arrays of any type are implicit-lifetime objects, and it is possible to to begin the lifetime of implicit-lifetime object, without beginning the lifetime of its subobjects. As far as I am aware, th...
Grommet asked 21/3, 2021 at 10:13

2

When implementing certain data structures in C++ one needs to be able to create an array that has uninitialized elements. Because of that, having buffer = new T[capacity]; is not suitable, as new ...

3

Solved

I am aware of the differences between free and delete in C++. But one thing I never understood is why in C malloc/free can allocate de-allocate both single 'objects' and arrays but in C++ we need t...

1

Solved

I noticed that the string capacities in C++ follow this pattern: initial string size is 15 for any string that is larger than a particular size 'block', the capacity is doubled. Here are the stri...
Decile asked 29/9, 2020 at 5:8

4

Here, in this question, it's stated that there is no realloc-like operator or function in c++. If you wish to resize an array, just just std::vector instead. I've even seen Stroustrup saying ...
Quicken asked 14/8, 2020 at 7:29

4

Solved

I was told by an instructor that p = (int*)malloc(5 * sizeof(int)) is NOT dynamic memory allocation and that p=(int*)malloc(n * sizeof(int)) is dynamic memory allocation. The instructor was talking...
Mcmillan asked 3/8, 2020 at 12:12

7

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....
Fagin asked 7/2, 2011 at 13:38

© 2022 - 2024 — McMap. All rights reserved.