Memory Management Error
*edit*
sorry it wouldn't accept my post for some reason. it is pretty big i suppose. you can get it here:
http://www.geocities.com/dr_xorsysm/post.txt
[Edited by - drkpriest on September 3, 2004 12:47:08 AM]
looks a bit that way ay.. it has to be the [] operator changing the array pointer and not the value. hmm just frustrating. oh well stl it is :P i didn't really want to succumb to its evilness. i'm just one of those people that like to know how everything works. but anyways..
type* operator [](int index) { // make sure a valid index is specified assert(index => 0 && index < arraySize); return (array[index]); }
Shouldn't it be "return &array[index]"?
Did you really debug this code? I think you didn't since the expression inside the assert wouldn't compile (=> should be >=)
Yes your subscript operator signature is syntactically correct but is schematically wrong.
For one there should be two subcript operators one that is constant & another non-constant member functions and they should return constant & non-constant references not pointers.
Further more you shouldn't use ints for indexing, prefer using standard library size_t type defined in cstddef header, so with all that in mind it should be of the form:
really thou your just writing an inefficient version of the standard library container vector so i would recommend you use that instead.
For one there should be two subcript operators one that is constant & another non-constant member functions and they should return constant & non-constant references not pointers.
Further more you shouldn't use ints for indexing, prefer using standard library size_t type defined in cstddef header, so with all that in mind it should be of the form:
#include <cstddef>typedef size_t size_type;const type& operator [](size_type index) const { // make sure a valid index is specified assert(index >= 0 && index < arraySize); return array[index];}type& operator [](size_type index) { // make sure a valid index is specified assert(index >= 0 && index < arraySize); return array[index];}
really thou your just writing an inefficient version of the standard library container vector so i would recommend you use that instead.
thanx for the help guys. its still crashing tho. i originally had type& return value for the subscript operator but pasted it in as type* as i forgot to change it back from previous tests. i debugged it and found that the delete statement in the static removeAllObjects() method was trying to delete an invalid pointer. I cant see why it is doing that tho..
doho: was just a typo when i posted it. its <= in my code. thanx for that tho.
*edit*
snk_kid: yeah rewriting it now with containers. :)
doho: was just a typo when i posted it. its <= in my code. thanx for that tho.
*edit*
snk_kid: yeah rewriting it now with containers. :)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement