|
-
August 10th, 2010, 04:21 AM
#10
Re: Unitialized memory help
 Originally Posted by jdblack
I took it out and replaced it with a static reference to the correct value, and nothing changed in the crash behavior.
Instead of that, do you know why that line is suspicious? That is how you solve problems -- you identify why the code causes problems before creating code that basically doesn't do anything. You can't just throw code at a problem and hope it works without knowing what is the exact problem.
The reason why that line doesn't work is that when you allocate n items, the items are indexed from 0 to n-1. You obviously have a memory overwrite on that line, since you allocated n items and are using n to reference an item, when items can only be referenced from 0 to n-1.
Given that, how do you solve it? You obviously have an indexing problem, where the index is going out of bounds. So you solve it by making sure you don't index items out of bounds.
The general problem with your code is writing too much at one time without testing each portion first. The "grow" function should have been fully tested prior to placing it in the middle of the program. Growing an array should be a basic, fundamental piece of the program that needs to be isolated.
Also, whether you're aware of it or not, C++ doesn't work the way you believe it does, where as soon as your code makes a mistake, you see the error at runtime. When you access invalid entries in an array, overwrite memory, etc. a C++ program need not crash immediately. It can silently keep running until a crash finally occurs, or it could run all the way without any visible problems. Unlike other computer languages, where you are immediately informed of any illegal accesses (i.e. Java, C#, etc.), C++ doesn't afford you this "luxury".
Code:
Yes, and the output wasn't meaningful enough to me. This is the line it breaks on -
" static void __CLRCALL_OR_CDECL assign(_Elem& _Left, const _Elem& _Right)
{ // assign an element
_Left = _Right;
}"
That line is part of the runtime, meaning your code did something illegal. This is what I mean -- that line of code is where your program finally breaks down -- it isn't where the problem originated.
Here's the current code after the recommended tweaks, same behavior.
Again, have you used the debugger to debug your program? That is exactly what everyone here would need to do to debug your program, so you should be doing the same thing.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|