Archived

This topic is now archived and is closed to further replies.

peckerpeck

Quick STL question, but please help...

Recommended Posts

peckerpeck    100
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.

Share this post


Link to post
Share on other sites
Kippesoep    892
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]

Share this post


Link to post
Share on other sites
peckerpeck    100
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
?

Share this post


Link to post
Share on other sites
darookie    1441
A more convinient way (without potentially leaking pointers):


typedef std::list<SMemInfo> MemLeakList;



[edited by - darookie on March 5, 2003 1:38:30 AM]

Share this post


Link to post
Share on other sites
Kippesoep    892
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;

Share this post


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


for(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]

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Thunder_Hawk    314
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 -- ¦þ
______________________________

Share this post


Link to post
Share on other sites
Seriema    634
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]

Share this post


Link to post
Share on other sites
LNK2001    218
peckerpeck: Just wanted to point out that all classes/structs stored in an STL container should have an assignment operator and a copy constructor. To forget about this can cause very nasty bugs.

If SMemInfo was stored in a std::vector, you''d have a serious problem, because STL would use the default assignment operator. And an array (like SMemInfo::file) cant be copied with assignment.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by Seriema
the postfix/prefix thing applies to all types, even int''s.
That''s false, but is stated here pretty often. Post-increment means *delayed* increment. I.e. the incrementation operation occurs after the expression has been evaluated. Thus, there''s never need for temporaries when using post-increment for basic types. Classes that try to *simulate* delayed increment, have to rely on temporaries.

Share this post


Link to post
Share on other sites
Seriema    634
AP: hmmm... maybe our''re right? But I''ve heard ++MyInt is faster than MyInt++ ?

LNK2001: you should also overload the < operator

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

}+TITANIUM+{ A.K.A. DXnewbie[onMIRC]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by Seriema
AP: hmmm... maybe our''re right? But I''ve heard ++MyInt is faster than MyInt++ ?
Well, that''s not strange. Many people have the same misconception. For example, here''s how two statements convert to pseudo asm:

a = ++b;

inc b, 1
set a, b
-------------
a = b++;

set a, b
int b, 1

The only difference is the position of the increment operation.

The funny thing is that most people knew how post increment really works before C++ came. Then people started thinking of C++''s simulation as the one and only real algorithm to make post increment..

Share this post


Link to post
Share on other sites