Sign in to follow this  
drkpriest

Memory Management Error

Recommended Posts

*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]

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites

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 >=)

Share this post


Link to post
Share on other sites
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:


#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.

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this