C++ badly initialized std::set [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a public variable inside a class, declared as
std::set<int> test;
and never explicitly initialized. When I try to access it from a shared pointer c of an instance of the object:
std::set<int>& myset = c->test;
I see in the debugger that myset is badly initialized: it has both fields _Myhead and _Mysize null. Could you please explain why that happens?

You are using std::set<>, and it has own constructor which initialize inner data. So null is OK.

The issue was that the pointer was not correctly initialised, although I thought it was.
I got confused since I come from Java where a null pointer would have generated an exception without letting me inspect the object's inner variables, so I ruled out that option too early!

Related

When the code goes in the print_city function, the variable "code_search" reset to 0 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Here's where my code freak out Ive tried to initialize in the main, in the search but nothing works, thank you in advance, and i have to kept the two functions I can't put them in the same one! so code_search is at 1 but when it goes to the print_city it goes back at 0.
As SuperStorme said, you are passing code_search by value. Try to pass it by reference:
void search_city(City cities_array[], const int MAX_Cities, int & code_search){...}
Passing by value, you are changing only the local variable code_search in the function scope.

Exception 0xC0000409 (stack buffer overflow) when using dynamic_pointer_cast [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm experiencing a crash when trying to dynamic_pointer_cast a shared point of type A to type B.
Type B is not related to type A and I'd expect an empty shared_ptr but instead, the exception above is raised.
Is there any scenario where it isn't safe to use dynamic_pointer_cast?
Exception raised here:
Using MSVC 14.16.27023
std::dynamic_pointer_cast requires that the conversion of U* (source) to T* (destination) is well formed. If it isn't then you have undefined behavior. If you want to get a null pointer like you would from dynamic_cast then you are going to have to write your own version that will do this.
Another option is test the result of
dynamic_cast<decltype(destination_ptr.get())>(source_ptr.get())
And if that succeeds then call std::dynamic_pointer_cast else return a null pointer.

Why am I getting an exception with a push involved with a shared pointer? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
How do I pass a smart pointer into this class so that it may be pushed into.
This line:
auto alltransactions(atrans);
Declares a new variable named alltransactions, which has the same name as your member.
Your constructor should use member init list to ensure your member are initialized correctly:
struct my_class {
my_class(std::shared_ptr<std::vector<cl_order>> atrans) : transactions(atrans) {}
std::shared_ptr<std::vector<cl_order>> transactions;
};

pointer being freed was not allocated, without any new usage [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm going through a piece of code here. For testing purposes it shows a window (QWidget) when executed.
When I hit close it returns:
my_object(7082,0x7fff7a538000) malloc: *** error for object 0x7fff5199b9b8: pointer being freed was not allocated
Where my_object is an instance of the class instantiated at a QMainWindow.
There is no new usage in all code I wrote. And also no delete call. How is that (pointer being freed) possible? I though not using explicit new I'll be outside the dangerous zone.
What's the best way to approach this (pointer being freed was not allocated) issue. I'll go by disabling some parts or, in other words, try/error approach.
Boy,
Check the order of member declaration in all classes involved.

What is the correct way of initializing a smart pointer member variable? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am currently learning up on smart pointers, and actually used them in my code as well. However, I wanted to reread the documentation on smart pointers and came across this line in MSDN (https://msdn.microsoft.com/de-de/library/hh279669.aspx):
// When initialization must be separate from declaration, e.g. class members,
// initialize with nullptr to make your programming intent explicit.
shared_ptr<Song> sp5(nullptr);
//Equivalent to: shared_ptr<Song> sp5;
right now I am using this in my header file:
shared_ptr<Song> sp5 = NULL;
I tried it a few time now but I couldn't really get the MSDN example to work without a bunch of errors popping up, but when trying to find out the problem there is not really much in terms of resource which even tell me to initialize smart pointers like this.
Is it really necessary to solve it like MSDN suggested?
No, it's not necessary. There's no "correct" way of initialising a smart pointer.
As the comment itself indicates, you may safely omit the initialiser.
For a initializing I would just write.
shared_ptr<Song> sp5;
and then later assign a new pointer or use
sp5.reset(new Song());
Inizializing it with
shared_ptr<Song> sp5(nullptr);
works to show that you want the sharded_ptr to be null in the beginning.
But i personally would suggest to you, that when you deal with null shared_ptrs to check if they are null before using them.
shared_ptr operator bool checks if the pointer is not null.
http://www.cplusplus.com/reference/memory/shared_ptr/operator%20bool/