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: April 23, 2024, 7:58 pm

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Custom error message for stack overflow in C++
#11
RE: Custom error message for stack overflow in C++
I've looked at your code, and you are making heavy use of the stack.  The default stack size is 1 MB on Visual Studio, but you can change that with a compiler flag.

It is possible you have simply run out of stack.

Experienced programmers learn to use very little stack space, and use the heap instead.  That requires making use of things like shared_ptr<> rather than copying large objects into new variables (it also helps with performance).

I notice that every TreeNode generates some data which looks like it is the same for all instances.  That is wasteful - it should be static and initialized statically.  That also causes much less data to be copied.

If you increase the stack, still get the error, and get an even deeper stacktrace on the same lines of code, then you know you have an infinite loop.  Just walk the stacktrace and look at the lines and data, and fix it.  You think you don't have an infinite loop (something I haven't verified), so just use the larger stack.  But, you really shouldn't be using so much stack space.

Set Stack Size

I don't know how to change the compile parameters directly from CMake, but it is easy to temporarily make a change in the generated VS project file.
Reply
#12
RE: Custom error message for stack overflow in C++
(August 10, 2021 at 3:41 pm)FlatAssembler Wrote:
(August 10, 2021 at 3:36 pm)Angrboda Wrote: Try hitting it with a hammer.

To hit what with a hammer? The problem occurs on whatever computer my program, if compiled using CLANG without optimization or Visual Studio C++, is run on.

(August 10, 2021 at 3:26 pm)zebo-the-fat Wrote: Out of cheese error?

I am not sure what you mean.

I's a Diskworld thing
The meek shall inherit the Earth, the rest of us will fly to the stars.

Never underestimate the power of very stupid people in large groups

Arguing with an engineer is like wrestling with a pig in mud ..... after a while you realise that the pig likes it!

Reply
#13
RE: Custom error message for stack overflow in C++
(August 10, 2021 at 4:11 pm)HappySkeptic Wrote: That requires making use of things like shared_ptr<> rather than copying large objects into new variables (it also helps with performance).

I notice that every TreeNode generates some data which looks like it is the same for all instances.  That is wasteful - it should be static and initialized statically.  That also causes much less data to be copied.
Damned pointers, I was never good with them.

I have tried using static fields, but the linker keeps complaining then. I do not know enough C++ to figure out why.

Maybe you can fork me on GitHub to fix those things?
Reply
#14
RE: Custom error message for stack overflow in C++
(August 11, 2021 at 4:28 am)FlatAssembler Wrote:
(August 10, 2021 at 4:11 pm)HappySkeptic Wrote: That requires making use of things like shared_ptr<> rather than copying large objects into new variables (it also helps with performance).

I notice that every TreeNode generates some data which looks like it is the same for all instances.  That is wasteful - it should be static and initialized statically.  That also causes much less data to be copied.
Damned pointers, I was never good with them.

I have tried using static fields, but the linker keeps complaining then. I do not know enough C++ to figure out why.

Maybe you can fork me on GitHub to fix those things?

I don't know if I have the time to do a fork and fix them.  I might, but, increasing the stack size is probably your solution.

The heap is literally 1000 times the size of the default stack (or more).  Using bare pointers is just a bad idea in C++ 11 or higher.  Always use shared_ptr<> to hold the pointer, and new the object from the heap.

The shared_ptr<> can be thought of as a reference object - don't think of it as a pointer any more (although you dereference it with a "->" post-fix symbol or "*" prefix symbol just like a pointer).  When the last shared_ptr<> reference to an object disappears, the object gets deleted.


Code:
shared_ptr<TreeNode> node = new TreeNode();


Now, node is just an object that get copied into methods, returned from methods, or held in other objects - but the copies of it copy the reference, not the TreeNode data.  The data is on the heap, not the stack.  Only the shared_ptr<> reference object is on the stack.

Here is an example of a performance (and stack) problem.


Code:
AssemblyCode convertToInteger32(const TreeNode node, const CompilationContext context)

A correct method would be

Code:
AssemblyCode convertToInteger32(const TreeNode & node, const CompilationContext & context)

The difference is that the node and context are now passed as C++ references.  Basically, a C++ reference is a hidden pointer.  You still treat node and context just like before, no "->" needs to be added, but under the hood, the objects are being passed by an internal pointer.

Your way, the entire context and node are copied, and the copies reside on the stack until the method returns.  That is extremely wasteful. Doing this one change everywhere may be enough to fix your blow-up, though you should increase your stack size as well.

The other stack and performance issue, is that AssemblyCode is being returned as a copy.  That isn't terrible, but it is wasteful, and the variable it is being assigned to in your compile() code is on the stack.

Instead, a better method would be


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

Of course, to use this, you would have to "new" the AssemblyCode from within the method, and use it with "->" dereference operators instead of "." .  shared_ptr<> is just another std:: class (i.e. from the standard template library).

Now, if you went all the way with shared_ptr<>, you would always have context and node be shared_ptr<> as well, throughout your code.  Then, you would pass those in:


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


Here, I wouldn't get much performance penalty by changing "& node" to just "node", as copying a shared_ptr<> is relatively fast (a few bytes, plus incrementing an atomic_int in a thread-safe way).  However, once you get into using shared_ptr<> everywhere, a lot of code will need changing.
Reply
#15
RE: Custom error message for stack overflow in C++
HappySkeptic Wrote:I don't know if I have the time to do a fork and fix them.
Nevertheless, thank you for writing all that information, it will probably help.
And what do you think about my project in general? Is it interesting?
HappySkeptic Wrote:I might, but, increasing the stack size is probably your solution.
So, you think I should add in the README that CLANG on Windows and Visual Studio C++, but not GCC, provide a too small stack by default to compile the Analog Clock program?
HappySkeptic Wrote:Of course, to use this, you would have to "new" the AssemblyCode from within the method, and use it with "->" dereference operators instead of "." .
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.
Reply
#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
#17
RE: Custom error message for stack overflow in C++
Flat is seeking those gullible to do his homework again.

Nope.
Reply
#18
RE: Custom error message for stack overflow in C++
(August 11, 2021 at 9:14 pm)Abaddon_ire Wrote: Flat is seeking those gullible to do his homework again.

Nope.

That is not my homework, it is my hobby project. We have not yet learned anything about compiler theory at the university. And, actually, I will probably drop out from the university. I feel like I can learn more useful stuff about computers on Internet forums than at the university.
Reply
#19
RE: Custom error message for stack overflow in C++
(August 12, 2021 at 3:42 am)FlatAssembler Wrote:
(August 11, 2021 at 9:14 pm)Abaddon_ire Wrote: Flat is seeking those gullible to do his homework again.

Nope.

That is not my homework, it is my hobby project. We have not yet learned anything about compiler theory at the university. And, actually, I will probably drop out from the university. I feel like I can learn more useful stuff about computers on Internet forums than at the university.

But will anyone hire you?

Most successful graduates "also" do hobby projects.

You have talent, but you need more knowledge (and you need to be able to prove you have knowledge).  Right now you are "almost" at a junior programmer level.

If you quit because you just prefer to do your own hobby stuff, why would anyone hire you? You aren't going to be doing your own hobby work in a company. They will tell you what they want done, and you have to figure out the design and solution (or work as part of the solution, if you are working under a senior developer)
Reply
#20
RE: Custom error message for stack overflow in C++
(August 12, 2021 at 8:58 am)HappySkeptic Wrote:
(August 12, 2021 at 3:42 am)FlatAssembler Wrote: That is not my homework, it is my hobby project. We have not yet learned anything about compiler theory at the university. And, actually, I will probably drop out from the university. I feel like I can learn more useful stuff about computers on Internet forums than at the university.

But will anyone hire you?

Most successful graduates "also" do hobby projects.

You have talent, but you need more knowledge (and you need to be able to prove you have knowledge).  Right now you are "almost" at a junior programmer level.

If you quit because you just prefer to do your own hobby stuff, why would anyone hire you?  You aren't going to be doing your own hobby work in a company.  They will tell you what they want done, and you have to figure out the design and solution (or work as part of the solution, if you are working under a senior developer)
Well, I think I am competent enough to have a job. And even if I am not, I don't think university will make me competent. They did not teach me how to change a lightbulb in our electrical engineering classes, so they also will not teach me useful things in our programming classes.
Reply



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 3835 June 14, 2023 at 8:01 am
Last Post: FlatAssembler
  Error 502 bad gateway for Rational Responders Brian37 13 1291 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 1780 July 9, 2018 at 5:17 pm
Last Post: bennyboy
  HELPPPPP Blue screen error just now!!! Edwardo Piet 114 9802 November 10, 2015 at 2:26 am
Last Post: Aractus
  Disk read error. Creed of Heresy 12 4040 July 27, 2012 at 9:19 am
Last Post: Tiberius
  Video Error: Grey circle with a white exclaimation mark inside. Reforged 5 3740 July 5, 2012 at 1:19 am
Last Post: Reforged



Users browsing this thread: 1 Guest(s)