|
-
August 10th, 2010, 09:35 AM
#16
Re: Unitialized memory help
 Originally Posted by ninja9578
Why not:
Code:
memmove(&arr2[0], &arr[0], 5 * sizeof(int));
Well, for one thing, memcpy() is always at least as efficient as memmove(), and sometimes moreso. Furthermore, while both of those would be perfectly fine on ints, they would not generalize to any type; for instance, it could cause a crash if you tried it with an array of std::string.
So the "proper" solution if you didn't want to use a loop would be
Code:
std::copy(arr,arr+5,arr2);
This would of course decay to memcpy() internally when instantiated for ints.
Of course, that disregards move semantics. I'm not sure if there's a multi-element form of std::move on any compilers yet, or even if that's going to be the name for that function when there is.
-
August 10th, 2010, 10:01 AM
#17
Re: Unitialized memory help
 Originally Posted by jdblack
Point taken, but I don't feel up to starting over at this point.
Bad choice. Using a std::vector instead of dynamic arrays will make your code much much simpler. It's not difficult to change the code either. Just change the type from pointer to std::vector<something>, compile and fix the errors one by one. Since accessing elements of the vector uses the same syntax as for a dynamic arrays, all you'll have to fix is the allocation/deallocation code, which is causing the problems anyhow.
 Originally Posted by jdblack
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;
}"
It looks like you are using Visual Studio. If that's the case, open the 'Call stack' window when the debugger stops on this line and double click on the first line that is part of the code you've written. Then examine the expression that is pointed out (it may actually be one line above what is indicated).
Try searching for some online tutorials on how to debug a program in your compiler if you are not familiar with these techniques. It'll pay off, trust me. 
Edit: you can start with this FAQ
Last edited by D_Drmmr; August 10th, 2010 at 10:41 AM.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
-
August 10th, 2010, 10:57 AM
#18
Re: Unitialized memory help
 Originally Posted by Paul McKenzie
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.
Sure you can, that's what I did and it's working now. I'll agree it's not ideal though because you don't learn from it.
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.
Thanks, I should've caught that. I'll also make an effort to test each function first next time I right a complicated (for me) program. And thanks for the info about C++. I've used C# before quite a bit on my own.
Bad choice. Using a std::vector instead of dynamic arrays will make your code much much simpler.
Dictated by my teacher unfortunately, no vectors or lists allowed.
If that's the case, open the 'Call stack' window when the debugger stops on this line and double click on the first line that is part of the code you've written. Then examine the expression that is pointed out (it may actually be one line above what is indicated).
Sweet, thanks! I'm too busy now, but I'll do it in when I get home.
-
August 10th, 2010, 11:18 AM
#19
Re: Unitialized memory help
 Originally Posted by jdblack
Dictated by my teacher unfortunately, no vectors or lists allowed.
So you're learning to write C++ programs as if it's the year 1990 instead of the year 2010. Instead of a book repository program, you had to write a dynamic array class disguised as a "real" assignment. If the course you're taking were honest, it should be called "C programming with some new syntax thrown in". It is not C++ programming, at least not in the way how the C++ language stands today, and truthfully, for the past 10+ years.
C++ has been standardized since 1998, and part of the standardization was to include containers into the language library. If the "no vector, no list" stuff keeps up, you won't be learning anything that a majority of the C++ programmers actually use.
If you were told up front to write a dynamic array class and only a dynamic array class, test it with various inputs, etc. that's one thing that could be acceptable (but for an intermediate programmer, not a beginner). You would then have used your home-made dynamic array class in your bookkeeping assignment, and see how it works. Personally, I have yet to see a beginner get a dynamic array class correct -- they are just not ready for such coding.
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
|