Our server costs ~$56 per month to run. Please consider donating or becoming a Patron to help keep the site running. Help us gain new members by following us on Twitter and liking our page on Facebook!
Current time: June 10, 2024, 8:31 am

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Custom error message for stack overflow in C++
#16
RE: Custom error message for stack overflow in C++
(August 11, 2021 at 4:40 pm)FlatAssembler Wrote: Is it important whether I create the
Code:
shared_ptr<AssemblyCode> assemblyCode=new AssemblyCode();
inside the method, or can I write
Code:
AssemblyCode *assemblyCode=new AssemblyCode();
and only later
Code:
return assemblyCode
for the raw pointer to be automatically converted to the smart pointer upon returning? I have no idea how it works.

Yes, this will work (if the calling code assigns the function return value to a smart pointer), but it is bad form to mix smart pointers and raw pointers to the same object in a program.  What if one piece of code holds onto the raw pointer, and another the smart pointer?  The raw pointer will point to a dead object at some point, once the last smart pointer to the object disappears.  By enforcing the return object to be a shared_ptr<>, you are making the contract that everyone should use the smart pointer only (yes, they can still pull out the raw pointer, but the user would know that is bad).

Using a smart pointer is just as easy as using a real pointer.  The * (prefix) and -> (postfix) dereference operators are the same.

Part of your problem may be solved by using the C++ references in parameter passing, as I posted earlier.  It is almost always a performance mistake to pass class objects (as opposed to simple numeric types or pointers) by copy instead of by C++ reference.

To further expand on the idea to "not mix smart pointers and regular pointers to the same object", consider this scenario:


Code:
shared_ptr<AssemblyCode> convertToInteger32(const TreeNode & node, const CompilationContext & context);

// assign the return code to the smart pointer.  There is now only one smart-pointer "myCode" that is keeping the returned data alive.
shared_ptr<AssemblyCode> myCode = convertToInteger32(node, context);
// We can use the underlying object using normal de-reference.
cout << *myCode;


This is perfectly fine, but this should blow up (it may not, but this dereferences deleted memory).


Code:
shared_ptr<AssemblyCode> convertToInteger32(const TreeNode & node, const CompilationContext & context);

// I don't like smart pointers, so just grab the raw pointer.
AssemblyCode * myCode = &(*convertToInteger32(node, context));
// Oops, because my smart pointer return-code just got deleted from the stack, myCode points to a deleted object (as there are no more smart pointers pointing to it)
cout << *myCode;
Reply



Messages In This Thread
RE: Custom error message for stack overflow in C++ - by HappySkeptic - August 11, 2021 at 4:54 pm

Possibly Related Threads...
Thread Author Replies Views Last Post
  What is the most absurd error message a program you have made was outputting? FlatAssembler 38 4247 June 14, 2023 at 8:01 am
Last Post: FlatAssembler
  Error 502 bad gateway for Rational Responders Brian37 13 1415 April 23, 2022 at 4:17 pm
Last Post: BrianSoddingBoru4
  How Do I Send an SMS message From UK mobile to a US one ReptilianPeon 11 1867 July 9, 2018 at 5:17 pm
Last Post: bennyboy
  HELPPPPP Blue screen error just now!!! Edwardo Piet 114 11096 November 10, 2015 at 2:26 am
Last Post: Aractus
  Disk read error. Creed of Heresy 12 4100 July 27, 2012 at 9:19 am
Last Post: Tiberius
  Video Error: Grey circle with a white exclaimation mark inside. Reforged 5 3815 July 5, 2012 at 1:19 am
Last Post: Reforged



Users browsing this thread: 1 Guest(s)