Skip to main content
How are we doing? Please help us improve Stack Overflow. Take our short survey
Filter by
Sorted by
Tagged with
1 vote
1 answer
133 views

gcc gives the following warning: warning: storing the address of local variable 'node' in '*root_p' [-Wdangling-pointer=] in this code: treenode_t *nodeInsert(treenode_t ***root_p, int key){ ...
Daniel Muñoz's user avatar
3 votes
1 answer
197 views

At https://en.cppreference.com/w/cpp/utility/functional/function_ref/function_ref.html, there is an overloaded ctor as follows: template<class F> function_ref(F&& f) noexcept; ...
xmllmx's user avatar
  • 44.6k
3 votes
0 answers
131 views

Is using (but not dereferencing) the address of a captured variable that is out of range, undefined behavior ? #include <iostream> auto foo() { int i = 42; return [&] { std::cout &...
Tootsie's user avatar
  • 909
4 votes
2 answers
480 views

Let's say I have a function accepting an std::span<int>, and I want to call it with an rvalue of an std::vector<int>: void foo(std::span<int> s); std::vector<int> make_vector()...
Mikhail's user avatar
  • 22.1k
0 votes
1 answer
71 views

I have a Entity system (I tried to recreated a simpler ECS without much worry on memory placement...) EntityManager: class EntityManager { static std::unordered_map<std::type_index, std::list&...
Miaan's user avatar
  • 1
0 votes
1 answer
82 views

If I have a fn(&mut Box<fn()>) in a cdylib crate, and it sets the value of the Box to another function defined in the crate, I get a segmentation fault when calling the resulted function. I ...
TwiceUponATime's user avatar
0 votes
0 answers
61 views

std::array<T,N>::operator[] always returns an lvalue reference (const if needed), but what can possibly be the use of a line of code like this, which is valid C++? std::array<int,1>{1}[0] =...
Enlico's user avatar
  • 30.3k
1 vote
0 answers
53 views

I use some external lib written in C that implement hash table. When hash table size need to grow it uses realloc to double the memory space for keys/values. I familiar with this behavior but others ...
Brave's user avatar
  • 329
2 votes
1 answer
119 views

In the code below, I defined 4 classes: class A. base class B_parent. derived from B_parent class B_child which contains a reference to an object of class A, the reference is initialized by the ...
Ivan K.'s user avatar
  • 23
1 vote
1 answer
12k views

I have installed "Valgrind Task Integration" extension in Visual studio code and after restarting VS code and typed the following Valgrind command in terminal, "valgrind --leak-check=...
Stackyquest's user avatar
-2 votes
2 answers
189 views

I'm making a game engine and I have run into a problem with destroying elements. For example I have some cameras that follow and look at an target (a gameobject or an actor). Later in the game the ...
Fewnity's user avatar
  • 39
0 votes
1 answer
48 views

I generate a GUID and then save it in OLECHAR* with StringFromCLSID(). If I create a function which returns an OLECHAR and not nullptr the OLECHAR after using CoTaskMemFree() - will it cause dangling ...
Samuil Dimitrov's user avatar
0 votes
0 answers
52 views

Is the reason why I got a garbage value because of the following or am I wrong? the Other object o passed as an argument to the Inner constructor, goes out of scope after the Inner object has been ...
sam's user avatar
  • 911
0 votes
1 answer
71 views

I have the following program: #include <iostream> #include <string> using namespace std; using int_arr = int[3]; int& f(int_arr& arr, int index) { return arr[index]; } int ...
ktqq99's user avatar
  • 35
0 votes
1 answer
387 views

I'm trying to build a class for a linked list in C++. My professor says that I have a dangling pointer in my code. I've spent hours looking for it but I just can't find it no matter what. I tried ...
Horsio's user avatar
  • 3
4 votes
1 answer
140 views

I want to mix up the co_yielding string literals and std::strings Generator<std::string_view> range(int first, const int last) { while (first < last) { char ch = first++; ...
Tom Huntington's user avatar
1 vote
0 answers
39 views

Would reference to values in std::map or std::unordered_map be valid/maintained if the map's being inserted new elements or removed from? For example, is the below code safe? Or would there be ...
Xero's user avatar
  • 23
4 votes
2 answers
483 views

std::reference_wrapper cannot be bound to rvalue reference to prevent dangling pointer. However, with combination of std::optional, it seems that rvalue could be bound. That is, std::...
slyx's user avatar
  • 2,326
1 vote
1 answer
427 views

I am constructing a shared pointer in a function_1 and giving it as a capture to a lambda. I think this is an issue, could you please confirm if this safe or I am right and I shoudn't be doing this? #...
Vero's user avatar
  • 351
-1 votes
1 answer
201 views

Suppose I'm compiling the following code: struct foo { const int& x_; foo(const int x) : x_(x) { } }; int main() { int x = 1; auto b2 = foo{x}; return b2.x_; } This program ...
einpoklum's user avatar
  • 137k
0 votes
1 answer
424 views

Why we initialize the next pointer of Linked List as NULL before deletion we move our head to the next node during deletion and we free the memory of the first node, so why we need to initialize the ...
user avatar
0 votes
0 answers
109 views

I'm new to c++ and I face some problems implementing the iterator on the template class Tree (structure of the code includes the canonical classes (Node,Tree,Iterator). Fwd iterator is not working ...
mpv's user avatar
  • 1
0 votes
2 answers
45 views

I understand the concept of dangling pointers, func2 returns address of deallocated memory. In func1 instead of returning the address we store by passing a double pointer. Isn't this also a case of ...
Manan Chawla's user avatar
11 votes
1 answer
373 views

It's common knowledge that returning a pointer to a stack variable is generally a bad idea: int* foo() { int i = 0; return &i; } int main() { int* p = foo(); } In the example above, my ...
user118534's user avatar
0 votes
0 answers
77 views

I found this piece of code at https://alexgaynor.net/2019/apr/21/modern-c++-wont-save-us/. The author says that in main x goes out of scope, destroying the last reference to the data, and causing it ...
Attila Szasz's user avatar
0 votes
3 answers
364 views

lets think we have a code like this : int * ptr = new int; int *nptr = ptr int * ptr2 = new int; int *nptr2 = 2*nptr+ptr2; delete ptr; delete ptr2; ptr = NULL; ptr2 = NULL; So now the nptr ...
esmaeil.zivari's user avatar
0 votes
1 answer
274 views

#include<stdio.h> int *func(int * ptr){ int a = 12; int *c = &a; return c; // here it returns the pointer by storing the address of local variable } int main() { int *...
anonymous's user avatar
1 vote
1 answer
463 views

T&& operator[](std::size_t n) && noexcept {std::cout<<"move"<<std::endl; return std::move(vec[n]); } I cannot get the expected result in this part. I predict a ...
gahhu's user avatar
  • 31
-1 votes
2 answers
56 views

The fllowing questions are: p->test() should not work after b is destroyed. However, the code is running without any issue, the dynamic binding still works; when the destructor of A is defined, ...
Changhe Li's user avatar
1 vote
1 answer
2k views

I am using the Raylib GUI framework for a project that displays all the nodes within an iteration of the Collatz Conjecture. In my program, I have a class for a Node object that acts simply as a ...
TheAngriestCrusader's user avatar
1 vote
1 answer
321 views

thing contains 2 vectors, one of foo and one of bar. The bar instances contain references to the foos - the potentially dangling ones. The foo vector is filled precisely once, in things's constructor ...
Oliver Schönrock's user avatar
0 votes
4 answers
553 views

In the below code I free the pointer ptr but still *ptr retuns me the same value. If I free the variable then it should give me some garbage value but it didn't. #include <stdio.h> #include<...
user avatar
2 votes
1 answer
164 views

I have written this method: std::string Utils::GetFileContents(const char* filePath) { std::ifstream in(filePath, std::ios::binary); if (in) { std::string contents; in....
Valentin Popescu's user avatar
0 votes
1 answer
90 views

I want to implement a function which returns a reference to Base which actually comprises Derived (types are polymorphic). Something among the lines of the following (incorrect) code: struct Base { ...
Zhiltsoff Igor's user avatar
1 vote
1 answer
943 views

Basically the situation is we have a C++ program that occasionally crashes when it attempts to access an already-freed object (in Debug build we notice that the memory being pointed to is full of the ...
Dan's user avatar
  • 321
0 votes
2 answers
101 views

Our lecturer in C++ made the following statement: Whenever a scope of some variable ends, the compiler checks whether this variable was an object itself or a reference to it, thus deciding whether to ...
Zhiltsoff Igor's user avatar
9 votes
1 answer
294 views

During constant expression evaluation in C++17, shall the compiler consider any pointer addressing a valid object unequal to any pointer addressing an object after the end of its lifetime? For example:...
Fedor's user avatar
  • 24.7k
12 votes
3 answers
1k views

We can only de-reference a valid pointer and we can only check the address that a dangling built-in pointer points to. We cannot access its value (the value in the address of object it is pointing to)....
Itachi Uchiwa's user avatar
0 votes
0 answers
45 views

in below program a string cstis created inside the block scope and store its const reference in unique_ptr<Def> d(unique_ptr<Def> d is outside block scope). as per my understanding when ...
CY5's user avatar
  • 1,580
1 vote
1 answer
102 views

so essentially I have a set of instances of struct A. I want to extract an instance, modify the fields. One of the fields is a unique ptr. I'm not that great at reading c++ errors, it looks like the ...
cheeseburgereddy's user avatar
0 votes
2 answers
508 views

why ptr is dangling pointer. I know "ch" is out of scope, but address of ch is still valid out of inner block. And when I print *ptr I get correct value i.e. 5. void main() { int *ptr; ...
Ravi's user avatar
  • 303
0 votes
3 answers
247 views

#include <stdio.h> int main() { int *ptr; { int x = 2; ptr = &x; } printf("%x %d", ptr, *ptr); return 0; } Output: address of x, value of x. ...
INDHUJA G's user avatar
6 votes
2 answers
640 views

I'd think that the VS2019 suggestion would create a dangling reference situation, but I tested it out, and it seems to work. What is happening here? template<typename MessageType> class ...
ratiotile's user avatar
  • 971
0 votes
1 answer
88 views

If you have a pointer or reference to an object contained in a container, say a dynamic array or a hash table/map, there's the problem that the objects don't permanently stay there, and so it seems ...
Zebrafish's user avatar
  • 16.3k
0 votes
3 answers
270 views

I saw a strange behavior the other day. So I wanted to store lines(present in a vector) in a char array and wanted to use '\n' as delimiter. I know c_str() method in string class returns a pointer to ...
Priyanshu Tirkey's user avatar
-1 votes
1 answer
194 views

int main(int argc, char *argv[]) { int x[3] = {1, 2, 3}; //create an array x int *y = x; //create pointer y and let it point to the array x *y = null; //now x points to the null, ...
Ali Kılıç's user avatar
0 votes
1 answer
470 views

I'm just starting out learning Rust, and I've hit a bit of a roadblock; I'm trying to create a function that initialises the rusty_v8 library. They've given the following code get set up with: use ...
J-Cake's user avatar
  • 1,710
0 votes
2 answers
1k views

When a pointer is allocated memory using malloc, pointer (say x)will now point to memory address. Later I free this(x) memory pointer,but pointer is still pointing to it's old memory. This would now ...
bbcbbc1's user avatar
  • 95
-1 votes
1 answer
60 views

Code 1: int *g(){ int *p; p=(int *)malloc(sizeof(int)); *p=10; return p; } Code 2: int *g(){ int p=10; return &p; } OUTPUTS: Code 2 ----> [Warning] function returns ...
sukhbir1996's user avatar
4 votes
1 answer
334 views

Yes, I know perfectly well you should not do that. If we have this code: int *foo() { int a = 42; return &a; } As most C coders know, this is undefined behavior: Using pointer after free()...
klutt's user avatar
  • 31.7k