My program undergoes a run-time error every time I uncomment l70-72 . It's not the first time I encountered this particular problem. I asked my C++ instructor about this, but it had been for over 3 weeks now and she is yet to give me any feedback.

Basically I make a pointer to an array of objects, send that pointer into a function, try to execute commands defined in that function in main and get a run-time error, all whilst using heap. Something clearly happens with that pointer, or with the pointers in the array.

An error occurs when I call manage_Rectangle_arr(string strCommand, Rectangle * *arr, short *siArrayL) with any of the following: "fill array", "get areas" and "empty contents" (obviously the last two are related to "fill array" in nature, since they need it to be executed first in order to function)

This question came from our site for professional programmers interested in conceptual questions about software development.

2

If you edit the minimum possible code that demonstrates the problem into the question then this question can get migrated to Stack Overflow. In it's current state I won't let it get migrated.
–
ChrisF♦Mar 19 '12 at 11:54

3

You are missing the point. Include the code in the question not as a link to an external site.
–
ChrisF♦Mar 19 '12 at 12:35

2

I tested your code. Using Visual Studio 2010 the code crashes just with delete uncommented. This is because you try to delete a non-allocated memory area.
–
MaxMar 19 '12 at 12:37

Because you have added code I feel it is good enough to migrate to SO now. You should get better answers there.
–
maple_shaftMar 19 '12 at 13:16

to be fair, sometimes a tutorial exercise can be artificial as the tutor is trying to get you to understand the problem. Its not "real-world" code solutions. If this was to force an understanding of pointers, then this answer get a F. (even if I'd write it like the answer in my job).
–
gbjbaanbMar 19 '12 at 17:46

@gbjbaanb If this was to force an understanding of pointers, then the instructor has failed abysmally. This is simply not a legitimate situation for pointers, and I’m getting increasingly vocal about this since so much crap code in C++ is caused by use of pointers.
–
Konrad RudolphMar 19 '12 at 18:32

@Max if by the 'string lookup' you mean "toString", that thing is being taught in the written course explicitly (and was asked for in that particular assigment, even though it wasn't needed), so yeah, pretty much, if you're referring to "toString" data member.
–
Alex D.Mar 19 '12 at 18:56

Even though this seems to get alot of downvotes, I'll try to provide a helpful answer. :)

Your array is of type Rectangle** - that is, a pointer to a pointer of a rectangle. You send this by value into a method. This means that the value of arr in your original method will be copied into the variable arr in manage_Rectangle_arr. Thus, when you assign arr a value (the address of the new memory area / array) arr will only contain this value in the called function - since these are completely different variables.

That variable / memory area will be sent by reference, which means it will be another , automatic, level of indirection. You could accomplish this by having a variable of type Rectangle*** and dereferencing the variable arr, but this quickly gets silly.

There are also many other 'not-best-practices' in your code, but I won't talk about them all here. :) Hopefully this will help you get back on track!

@CrawlingPastaHellion Go ahead, it's better if you figure it out on your own than having me tell you... As long as your not just randomly interchanging symbols in your source code :) Just try to incorporate the address-of operator. Hint: You don't need it declared as Triple-Star everywhere ;)
–
MaxMar 19 '12 at 13:07