dangling-pointer

Why is there no safe alternative to unique_ptr::operator*()?

守給你的承諾、 提交于 2020-01-12 12:00:13
问题 std::vector has the member function at() as a safe alternative to operator[] , so that bound checking is applied and no dangling references are created: void foo(std::vector<int> const&x) { const auto&a=x[0]; // What if x.empty()? Undefined behavior! const auto&a=x.at(0); // Throws exception if x.empty(). } However, std::unique_ptr lacks the corresponding functionality: void foo(std::unique_ptr<int> const&x) { const auto&a=*x; // What if bool(x)==false? Undefined behavior! } It would be great

How can I demonstrate a zombie object in Swift?

随声附和 提交于 2019-12-31 03:45:07
问题 I've read How to demonstrate memory leak and zombie objects in Xcode Instruments? but that's for objective-c. The steps don't apply. From reading here I've understood zombies are objects which are: deallocated but something pointer is still trying to point to them and send messages to them. not exactly sure how that's different from accessing a deallocated object. I mean in Swift you can do: var person : Person? = Person(name: "John") person = nil print(person!.name) Is person deallocated?

Can a local variable's memory be accessed outside its scope?

心已入冬 提交于 2019-12-25 04:56:09
问题 I have the following code. #include <iostream> int * foo() { int a = 5; return &a; } int main() { int* p = foo(); std::cout << *p; *p = 8; std::cout << *p; } And the code is just running with no runtime exceptions! The output was 58 How can it be? Isn't the memory of a local variable inaccessible outside its function? 回答1: How can it be? Isn't the memory of a local variable inaccessible outside its function? You rent a hotel room. You put a book in the top drawer of the bedside table and go

Memory allocation stack

本秂侑毒 提交于 2019-12-23 12:13:26
问题 In the stack, memory is reserved for main which we call stack frame for the main function. When we call the Add function, memory is reserved on top of the stack. In the Add function stack frame, a and b are local pointers and c is an integer which calculates the sum and then we return the reference. c is a local variable of Add function. Now when the Add function execution is completed the memory space in the stack is also deallocated so when we try to access this address in the main with

Safer way to expose a C-allocated memory buffer using numpy/ctypes?

萝らか妹 提交于 2019-12-20 16:28:51
问题 I'm writing Python bindings for a C library that uses shared memory buffers to store its internal state. The allocation and freeing of these buffers is done outside of Python by the library itself, but I can indirectly control when this happens by calling wrapped constructor/destructor functions from within Python. I'd like to expose some of the buffers to Python so that I can read from them, and in some cases push values to them. Performance and memory use are important concerns, so I would

dangling pointer is still accessing the memory value [duplicate]

試著忘記壹切 提交于 2019-12-13 23:57:35
问题 This question already has answers here : What does delete command really do for memory, for pointers in C++? [duplicate] (6 answers) Closed 3 years ago . I am pretty new to this concept and I am confused that if a dangling pointer is a pointer which points to a memory location which points to memory which has been freed or deleted then in this case why it is still able to call the function test() #include <stdio.h> #include <iostream> #include <stdlib.h> using namespace std; class MyClass{

Understanding dangling pointer behaviour in this case

一世执手 提交于 2019-12-11 17:37:10
问题 I have a pointer and by default it carries NULL then it waits for some event and gets a value if the event happens, later I am freeing the pointer somewhere else but even after freeing the pointer I am not making it NULL so it still keeps referencing the same memory location and I know the next malloc call might allocate that memory chunk to some other memory request! pointer_type *p = NULL; while (process_get_wakeup(//some logic//)) { while ((qelem = (void*)process_dequeue(//some logic//)) !

Is this undefined behaviour in C++ calling a function from a dangling pointer

ⅰ亾dé卋堺 提交于 2019-12-11 04:27:37
问题 A question came up here on SO asking "Why is this working" when a pointer became dangling. The answers were that it's UB, which means it may work or not. I learned in a tutorial that: #include <iostream> struct Foo { int member; void function() { std::cout << "hello";} }; int main() { Foo* fooObj = nullptr; fooObj->member = 5; // This will cause a read access violation but... fooObj->function(); // Because this doesn't refer to any memory specific to // the Foo object, and doesn't touch any

What is the most hardened set of options for GCC compiling C/C++?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 18:38:10
问题 What set of GCC options provide the best protection against memory corruption vulnerabilities such as Buffer Overflows, and Dangling Pointers? Does GCC provide any type of ROP chain mitigation? Are there performance concerns or other issues that would prevent this GCC option from being on a mission critical application in production? I am looking at the Debian Hardening Guide as well as GCC Mudflap. Here are the following configurations I am considering: -D_FORTIFY_SOURCE=2 -fstack-protector

Simple, efficient weak pointer that is set to NULL when target memory is deallocated

时光总嘲笑我的痴心妄想 提交于 2019-12-04 11:15:04
问题 Is there a simple, efficient weak/guarded pointer? I need multiple pointers to the same object that are all automatically set to NULL when the object is deleted. There is one "master" pointer that is always used to delete the object, but there can be several other pointers that reference the same object. Here are some solutions that don't quite match my needs: QPointer: I am not developing a QT app; I do not wish to include this libary/derive from QObject. boost::weak_ptr: an exception is