Skip to main content
gamedev.net
🔒 Locked

STL vector: size() function problem [SOLVED]

Started by Multiverse Sep 20, 2005 at 7:43 PM 5 replies 800+ views
Original Post
Multiverse
Multiverse
Greetings to one and all! I'm having a little problem with using the size() function with a vector (as the name of the thread implies) I'm also quite sure that i have had similar problems with begin() and end() which leads me to the (perhaps stupid) conclusion that the type of vector i'm creating doesnt work. ok, so the problem

// this is where it gets called.  'm_Filenames' is the name of the vector
for (i = 0; i < m_Filenames.size(); i++)
{
   ... // code to do with stuff
}

// and the point at which the error occurs whilst debugging, which is in the
// vector include file...

size_type size() const
	{return (_First == 0 ? 0 : _Last - _First); }



I have used vectors (and lists, not that thats relevent) elswhere in my code, and even within the same file, with no ill effects. The type i'm trying to store in the vector is a 'LPCSTR', but i have also tried a few other types and acheived the same error. As you may have gathered i'm trying to store filenames (or to put it another way, character strings) so if somebody could point me in the right direction for doing so, that would be a great help. Obviously any other help is greatly appreciated too! Thanks for your time guys! EDIT: i'm going offline now (to bed) so i apologise for my absence if you post a reply. I'm not being ignorant! I'm just not being conscious. [smile] [Edited by - Multiverse on September 21, 2005 10:51:52 AM]
SiCrane
SiCrane
It would help if you mentioned what the problem was. Assuming you're getting a crash on that line you've probably have a buffer overrun in an unrelated piece of code that's corrupting your vector.

Also, rather the store LPCSTR in a vector, I would recommend using a std::string instead.
Multiverse
Multiverse
ah yes, the actual problem, i'd better mention it. sorry about that.

The error that occurs is an 'Access Violation' when trying to access anything to do with the vector (size(), begin(), end() etc)

Your diagnosis sounds promising, so i'll check a few things out and see what i can find.

I'll also take you advice and look into std::strings a bit more (i have very little experience with them)

THanks for your help, i'll post back when (if) i find the cause of the problem or what i've done to remedy it.

Thanks again!
DrEvil
DrEvil
actually vector.size() is the number of elements currently in the list, vector.capacity() is the number of elements it can hold before resizing.

edit: hmm, responding to a deleted post hehe
twanvl
twanvl
DMatovich, that is not true; size() returns the number of items in use, std::vector has no length() function. capacity() returns the allocated space.

My guess is that the this pointer in that function is not valid, for example:
class Something {    std::vector<std::string> m_Filenames;    void do_something(); // error happens in this function};Something* invalid_pointer;invalid_pointer->do_something();

Put a breakpoint in the function, and look at the value of this
ZQJ
ZQJ
Since it's called m_Filenames it seems the most likely thing is that the containing class is at an invalid memory location for one reason or another.

Edit: beaten to it.
Multiverse
Multiverse
You were right guys!

silly me. I'll do a brief explanation so hopefully other people don't commit the same error...

#include "ResourceManager.h"class Test{public:    Test(ResourceManager* rman = 0);    ~Test();    private:    ResourceManager* m_pRManager;};


when i was creating a new instance of the Test class, I was simply calling new Test(), so m_pRManager was being set to 0. So even though it would create a new instance of Test without hassle, the pointer to the ResourceManager was obviously silly.

THe actual problem was a little more complicated than this, but it highlights the pitfall. So if theres anyone out there who's less competent than me (unlikely!), don't forget silly little things like this!

THanks everyone for your help, you hit the nail right on the head! [smile]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.