176 questions
1
vote
1
answer
133
views
Dangling pointer to pointer warning
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){
...
3
votes
1
answer
197
views
Why does std::function_ref allow passing in an expiring functor, rather than disallow it?
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;
...
3
votes
0
answers
131
views
Using the address of a dangling variable without dereferencing it
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 &...
4
votes
2
answers
480
views
How to pass an rvalue std::vector to a function accepting std::span
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()...
0
votes
1
answer
71
views
Lambda and std::function causing dangling reference (segmentation fault) [duplicate]
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&...
0
votes
1
answer
82
views
Segfault in Rust when calling a function returned by a cdylib crate function
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 ...
0
votes
0
answers
61
views
Why doesn't std::array<T,N>::operator[] return an rvalue reference for rvalue object? [duplicate]
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] =...
1
vote
0
answers
53
views
Detecting dangling pointer caused by realloc using static analyze tools
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 ...
2
votes
1
answer
119
views
Why does the initialization of a reference to the base class object with the child class object lead to a dangling reference?
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 ...
1
vote
1
answer
12k
views
How to run Valgrind and other tools to check memory leaks in Visual studio code?
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=...
-2
votes
2
answers
189
views
Dangling pointer issue C++
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 ...
0
votes
1
answer
48
views
OLECHAR used as pointer - will it dangling pointer if not nullptr? Function CoTaskMemFree()
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 ...
0
votes
0
answers
52
views
question on properly managing reference members in a local class in C++
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 ...
0
votes
1
answer
71
views
Does the following program contain a dangling reference?
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 ...
0
votes
1
answer
387
views
How to look for dangling pointers?
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 ...
4
votes
1
answer
140
views
Coroutines: Do co_yielded string_views dangle?
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++;
...
1
vote
0
answers
39
views
Will there be dangling references to map values if it gets resized? [duplicate]
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 ...
4
votes
2
answers
483
views
prevent initializing std::optional<std::reference_wrapper<const T>> with rvalue std::optional<T>
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::...
1
vote
1
answer
427
views
Return a shared pointer from a function vs Capturing a shared pointer in a Lambda
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?
#...
-1
votes
1
answer
201
views
Why doesn't GCC warn me about storing a reference to a local variable?
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 ...
0
votes
1
answer
424
views
Why we initialize the next pointer of Linked List as NULL before deletion
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 ...
0
votes
0
answers
109
views
bugged const_iterator& operator++() for red-black tree
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 ...
0
votes
2
answers
45
views
Storing address of local var via passing a double ptr to function does not cause a seg fault but returning address of that var causes seg fault. Why? [duplicate]
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 ...
11
votes
1
answer
373
views
Is it unsafe to co_yield a pointer to a local coroutine variable?
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 ...
0
votes
0
answers
77
views
What is the exact cause of undefined behaviour in this piece of code involving shared_ptr's and lambda ref capture?
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 ...
0
votes
3
answers
364
views
how to monitor dangling pointers that points to leakded memory?
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 ...
0
votes
1
answer
274
views
Why ptr not becomes Dangling pointer because when return pointer who store the address of local variable get destroy after return function?
#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 *...
1
vote
1
answer
463
views
Dangling reference solution
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 ...
-1
votes
2
answers
56
views
What is the effect of calling a virtual method by a base class pointer bound to a derived object that has been deleted
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, ...
1
vote
1
answer
2k
views
C++ Dangling pointer issue
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 ...
1
vote
1
answer
321
views
dangling reference in nested vector when parent container reallocates
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 ...
0
votes
4
answers
553
views
Why is the pointer not free and doesn't it bring me same value of a variable? [duplicate]
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<...
2
votes
1
answer
164
views
I don't understand why I have a dangling pointer [duplicate]
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....
0
votes
1
answer
90
views
How to return Derived from a function as a reference to Base?
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 { ...
1
vote
1
answer
943
views
C++ How to debug a dangling pointer in production
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 ...
0
votes
2
answers
101
views
Why does the creation of a reference when assigning REQUIRE garbage collection?
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 ...
9
votes
1
answer
294
views
Can dangling pointer be equal to valid pointer during constant evaluation in C++?
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:...
12
votes
3
answers
1k
views
Is checking the value of a dangling pointer safe or Undefined Behavior? [duplicate]
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)....
0
votes
0
answers
45
views
why const ref of string in the program is still valid?
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 ...
1
vote
1
answer
102
views
Is it possible to extract a struct containing fields that are unique/shared pointers from a set
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 ...
0
votes
2
answers
508
views
why is this dangling pointer in C?
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;
...
0
votes
3
answers
247
views
Printing Dangling Pointers in C
#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.
...
6
votes
2
answers
640
views
Why doesn't this create a dangling reference?
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 ...
0
votes
1
answer
88
views
How to have valid references to objects owned by containers that dynamically move their objects?
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 ...
0
votes
3
answers
270
views
Question related to std::string object and c_str() method in 3 different implementations
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 ...
-1
votes
1
answer
194
views
Is this a dangling pointer?
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, ...
0
votes
1
answer
470
views
Creating a factory function in Rust
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 ...
0
votes
2
answers
1k
views
Dangling pointers in C
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 ...
-1
votes
1
answer
60
views
Returning the address of a local variable and dynamically created variable
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 ...
4
votes
1
answer
334
views
Is it UB to return a pointer to local variable?
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()...