All Questions
66 questions
-2
votes
1
answer
123
views
Why is there a heap-after-use error here? I am trying to solve a Leetcode problem in C++. I don't see any dangling pointers or refs to freed memory
I am trying to solve LeetCode 2487: Remove Nodes From Linked List, and I’ve implemented a solution in C++ that first reverses the list, then removes nodes that have a greater value node to their left (...
0
votes
1
answer
56
views
C++ Primer 5th Ed - Stanley Lipmann: Question on shared_ptr.unique() used in conjunction with shared_ptr.reset()
First we have a shared_ptr defined as below:
shared_ptr<int> p(new int(42));
According to the text below:
The reset member is often used together with unique to
control changes to the object ...
0
votes
0
answers
68
views
How does free() deallocate a block of Memory when only given the Start Address? [duplicate]
I'm currently working on my own Implementation of an Array-like Datatype for learning purposes.
My Problem is understanding how the free() functions knows how much to deallocate since I only pass in ...
-1
votes
1
answer
166
views
Partially deallocating structs/classes in C++
Is there a way to partially deallocate structs/classes in C++? I was trying to build some sort of a new pointer in C++ that point to RefCount
template<typename T>
struct RefCount {
unsigned ...
1
vote
2
answers
191
views
Guaranteed alignment of adress allocated by new expression
I am compiling the code with gcc 11.4 with -m32 -std=c++20 on Linux
alignof(max_align_t) == 16
__STDCPP_DEFAULT_NEW_ALIGNMENT__ == 16
What is the guaranteed alignment that I should expect from new ...
1
vote
2
answers
98
views
local variables created by a data function by a dynamic object (heap) are stored in the heap or stack?
In C++, if I instantiate an object dynamically and it is stored in the heap, when this object calls one of its data functions which creates local variables, are these local variables stored in the ...
0
votes
1
answer
115
views
CrtIsValidHeapPointer(block) error in C++ -- deletion of a memory
Basically, in the code there is a class 'Board' including pointers called head and tail from a struct object 'slot' pointing to dynamically allocated lists which are in the property of a slot's class ...
-1
votes
1
answer
145
views
Copy constructors & Dynamic Memory C++ [closed]
Recently I've started working on my first OOP project and after having written the program I am trying to optimize it for code efficiency. I want to place the parts of the program that are being ...
2
votes
1
answer
153
views
Why am I getting errors freeing memory allocated by apriltags image_u8_create() and stored in 2D vector of pointers in C++?
I am attempting to detect apriltags in image streams. Because the images come in from multiple sources at a high rate, detecting the tags in the image callback will take too long, causing images to be ...
1
vote
0
answers
413
views
How does the time complexity of dynamic memory allocation in C++ depend on allocation size?
A previous post leaves the answer to this question as unresolved. Yet, my intuition tells me that larger allocations will take longer because it will take longer for underlying allocation strategies ...
1
vote
5
answers
1k
views
Problem in Deleting the elements of an array allocated with new[]
This question is similar to Problem with delete[], how to partially delete the memory?
I understand that deleting an array after incrementing its pointer is not possible as it loses the track of how ...
2
votes
2
answers
428
views
In C++, is it beneficial if each thread allocates the memory that it will later (in a different parallel region) write to?
void reserve1(vector<vector<int>>& vec, size_t siz) {
for (auto& v : vec) {
v.reserve(siz);
}
}
void reserve2(vector<vector<int>>& vec, size_t siz) {
#...
0
votes
1
answer
562
views
A class with a pointer pointing to another class as a member variable and pushing it into vector
using namespace std;
class B {
public:
B() :m_i(0), m_Name("") {};
B(const int num, const string& name) :m_i(num), m_Name(name) {};
void showInfo() {
cout << ...
4
votes
2
answers
901
views
Does the synthesized destructor destroy the memory allocated on the heap?
I have a class without a destructor and a constructor like this:
class Foo {
public:
Foo(int a) : p(new int(a)) {}
private:
int *p;
};
{
Foo a(4);
}
After this block of code will the ...
0
votes
2
answers
225
views
Initialization of values before constructor
Problem:
I implemented this new opeator for my class.
void* Objects::MemoryObject::operator new(size_t size, Memory::BaseAllocator* allocator) {
Objects::MemoryObject* newObject = static_cast<...