Quick STL question, but please help...

Started by
12 comments, last by peckerpeck 21 years, 1 month ago
I''m trying to typedef an stl list and i''m not too familiar with stl. Here''s the code: typedef struct _SMemInfo { DWORD address; DWORD size; char file[64]; DWORD line; } SMemInfo; typedef list MemLeakList; Here''s the error: e:\mssdk\myprojects\game\debug.hpp(49) : error C2143: syntax error : missing '';'' before ''<'' Is there an include or something that it''s not recognizing the syntax on? ps I pulled this code of flipCode for installing a mem leak checker.
Advertisement
Always use the [ source ] and [ /source ] tags to make sure all of the code shows up:


    typedef struct _SMemInfo{    DWORD address;    DWORD size;    char file[64];    DWORD line;} SMemInfo;typedef list<SMemInfo *> MemLeakList;  


Are you using namespace std; or using std::list;? If not, do this:


        struct SMemInfo{    DWORD address;    DWORD size;    char file[64];    DWORD line;};typedef std::list<SMemInfo *> MemLeakList;  


Not that typedef-ing a struct is not necessary in C++, only in C.

Oh, and make sure you #include <list>



[edited by - Kippesoep on March 5, 2003 1:29:29 AM]
Kippesoep
ohhh, my friend.

thanks, it was the #include <list> and the std namespace.
btw, isn''t it about 6am your time? go to bed!

thanks for the tip on source and /source.
are those html tags? and thus your image is
?

A more convinient way (without potentially leaking pointers):

typedef std::list<SMemInfo> MemLeakList;  



[edited by - darookie on March 5, 2003 1:38:30 AM]
quote:Original post by peckerpeck
btw, isn''t it about 6am your time? go to bed!

Nah, that was about 7am. I get up around 5:30am.

quote:
thanks for the tip on source and /source.
are those html tags? and thus your image is
<img src="..."> ?

The source and /source tags (with square brackets) are GameDev specific (see the FAQ). However, HTML is supported, so when you actually need an < or > type &lt; and &gt; respectively. When you need an &, type &amp;

Kippesoep
IF you use pointers (new) in your list, don''t forget to clean them up later! You have to iterate through the list in your destructor (or where you destroy the list) and properly delete all pointers. You don''t have to destroy or empty the actualy list, it does that with it''s own destructor.


  MemLeakList::iterator iter; // equal to: std::list<SMemInfo *>::iteratorfor(iter = MyList.begin(); iter != MyList.end(); ++iter)  delete *iter;  


notice the !=
It''s because list uses nodes all over the memory you can''t actually see if an iterator is < or > than another in a logical way. You CAN write it, but you won''t get the result you''d expect.
And ++iter is "alot" faster than iter++

iterator is kinda like a pointer to the contents in your container. So you have to dereference it (*) to get the contents, which in your case is a pointer.

If you got any more questions I''d be happy to answer them :D

"No lies of sugar can sweeten the sournes of reality"

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
Thanks Seriema,

i had wondered about the ''!=''. Are they just integer,
erm DWORD, handles that are stored in the list object somewhere?
Also, can you point me to or tell me why the ++iterator is
faster?

Thanks again guys,
Daniel
quote: had wondered about the ''!=''. Are they just integer,
erm DWORD, handles that are stored in the list object somewhere?

It''s implementation dependant. For a simple container like a vector, iterators are just pointers to the elements, however for more complex containers, such as maps, iterators can be objects within themselves that simply overload the operators you need. In a sense, the iterator is a way of iterating the elements in the container. While the guy writing the library should maintain a consistant interface for iterators - deference, incrementation, etc - the actual code behind it depends on the type of container. What you get is an interface

quote:Also, can you point me to or tell me why the ++iterator is
faster?

I can''t say anything about speed, but the post-increment/decrement operators have to create a temporary object. What happens is the object is saved, then incremented, and then the saved copy is returned. This way you can increment the object, but return a copy of the "old" object, satisfying the post part. Pre-increment/decrement don''t have to create a temporary, they just increment and return themselves.
In case you didn''t know, the pre-fix and post-fix operators are different because the pre-fix operator (++obj) increments first then returns the object while the post-fix operator (obj++) returns the object first (essentially) and then increments. Well, if you ever get into designing classes that require the overloading of the increment operator, you''ll learn that by its very nature the post-fix operator requires you to create a temporary with the current value, increment your object, and then return the temporary. As you can see, in the context of a for loop, you''d be creating a temporary of a fairly complex object each time through the loop for no particular reason...you get the idea.

______________________________

And the Phoenix shall rise from the ashes...

--Thunder_Hawk -- ¦þ
______________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
I knew you'd ask those questions :D

some already answered, but I'll gladly answer anyway

quote:
had wondered about the '!='. Are they just integer,
erm DWORD, handles that are stored in the list object somewhere?


they're types. internal classes to be more specific. where they overloaded the * and the -> operators so it works just like a pointer. and overloaded the != and == operators so you can use it in loops etc. (you knew you could overload your own operators for your classes in C++, didn't you?)


  template <class Type>    class list{public:class iterator{...};...};  


that's also why you write list::iterator to access it


quote:
Also, can you point me to or tell me why the ++iterator is
faster


as someone mentioned, postfix (iter++) returns a copy of the iterator and the prefix (++iter) returns a reference.


// NOT ACTUAL STL CODE! This is an illustration. But it works something like this
...
iterator operator ++ () // postfix, returns a "whole" iterator
{
iterator temp = *this;

// increment temp

return temp;
}

iterator& operator ++ ( iterator ) // prefix, returns a reference to itself
{
// increment
return *this;
}
...
[/source]

references are cheap, most times even free (they don't take up space on the stack). So use them! the postfix/prefix thing applies to all types, even int's. Although int's are small try to use ++MyInt instead of MyInt++ whenever you can, so you don't forget that prefix rox0rz ur box0rz

why it's faster: you don't keep creating and destroying objects over and over again (in your loop for example)


[edit] - added some more info *yeey*

[edit] - cleanup

[edit] - more cleanup

[edited by - Seriema on March 6, 2003 3:19:26 AM]
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]

This topic is closed to new replies.

Advertisement