Advertisement

does this DYnamic STaCK class look good?

Started by October 04, 2002 06:48 PM
32 comments, last by vaneger 22 years, 1 month ago
i dont know anything about error handling, i just code it so that if i get a NULL node created i dont execute code that will crash
The AP''s suggestion would be correct if you weren''t using templates - but you are, and your compiler is quite correct in generating an error. With templates, all the code needs to be available to whatever file includes the header. That can be achieved either by including a cpp file from the header, or by just writing it in the header in the first place. However, I think it''s a bit confusing to include a cpp file, which is why I recommend writing the code in the header.

When I spoke of "unwanted copies", I meant that once you''ve compiled your code into an .exe file, you won''t find duplicate copies of the template.


With the code you have, a NULL node will never be created unless your compiler is outdated - that''s what I was trying to get at.


I recommend you look into exception handling (the try/throw/catch keywords); they''re really useful for dealing with errors.
Advertisement
The implementation of is_full is not so good. You will get memory leaks. And it should never be full as long as you have memory left on your computer, I''d have is_full always return false.
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Also: What is the public member bool m_return used for?
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
I wouldn't use node-structs like you do. It takes a lot of memory (you need to store a pointer to next node and also the size of each node object is stored into memory) and using new and delete constantly is pretty slow too. It's generally faster to put all the objects into an array, and resize that array when it gets full [*]. It gets slower if you add a LOT of objects into the stack, because resizing an array requires you to copy all the elements in it. But you could make a constructor that lets &#111;ne choose the inital size for the stack array. With a high enough initial size, you wouldn't need to use new or delete at all when pushing and popping from the stack.<br><br>Also, a chunk-based approach would be good, perhaps faster than the std::stack.<br> <!–STARTSCRIPT–><BR><DIV CLASS=source><pre> <br><font color="blue">template</font> &lt;<font color="blue">class</font> Type, <font color="blue">int</font> size&gt;<br><font color="blue">struct</font> Chunk {<br> Type array[<font color="purple">size</font>];<br> Chunk&lt;Type, size&gt;* nextChunk;<br>}<br> </pre></DIV><!–ENDSCRIPT–><br>It'd be generally the same way you're doing now, but you'd allocate lots of objects at &#111;nce instead of just &#111;ne object. The overhead of 'next'-pointers, block size and new+delete would be considerably smaller.<br><br>
[*]Of course, a std::vector would be a better than an array, but if we're going to the 'high' abstraction level, std::stack would be best (which is usually implemented by using std::vector or std::deque)<br><br><SPAN CLASS=editedby>[edited by - civguy &#111;n October 5, 2002 11:28:39 AM]</SPAN>
dalleyboy: m_return i used because all of the methods are boolean so i added something for it to return to....civguy the whole idea is to have a linked list stack, not an array other wise id just usse a vector, how should i recode is_full...like how can i tell when memory is low and new nodes should not be created
Advertisement
quote: Original post by vaneger
ok mister annoymous poster you''re an a$$hole, i explained to you that including .h at top of .cpp doesnt work for my compiler why i dont know but ive always done it by including .cpp at bottom of .h because thast the way i was taught and it works so shove it


This is not the right attitude. Everybody includes .h files in the top section of the .cpp. If you have problems, you should work on it until you get that working, instead of using such an ugly workaround.

Errors are to be learned from. To abuse people who try to help you and to stubbornly stick to what you do isn''t the way to learn.
fidello for templated classes it doesnt work, so i have to include the .cpp at the bottom of the header, AP was wrong and lashed out at me for doing something wrong when in fact he was telling me the wrong thing, my code compiles and runs fine so i obviously have it linked properly
By the way, to the original poster --- GOOD FOR YOU that you are not wimping out and using the STL without first starting to learn about data structures. Too many people say "I use the STL rather than write my own..." -- just ignore them because they probably can''t write their own routines and thus need to act big by saying they use the STL.

Anyway, From the MSDN Visual Studio help doc:

"If there is insufficient memory for the allocation request, by default operator new returns NULL. "

with the example:

int *pi = new int[BIG_NUMBER];

if( pi == 0 )
{
cerr << "Insufficient memory" << endl;
return -1;
}


So, checking versus NULL is OKAY with Visual C++ by default.

As someone previously said DO NOT allocate a new node to check for memory. Because that is just USING up memory!

It is awkward visually to include a .cpp but there is NO RIGHT WAY...there is ANY WAY you want...that compiles.

People on this list seem to lack any sense of individuality and have a herd mentality...very odd for game developers one would think but when you see the lack of creativity in the gaming world it comes as no surprise.

You are writing YOUR OWN code - experiment and learn --- see what others do so you know what the norms are but do not follow them just because others do.

Anyway, if you don''t want to include the .cpp code just paste in the code and thus have everything in the header file.

quote: Original post by Anonymous Poster
By the way, to the original poster --- GOOD FOR YOU that you are not wimping out and using the STL without first starting to learn about data structures. Too many people say "I use the STL rather than write my own..." -- just ignore them because they probably can''t write their own routines and thus need to act big by saying they use the STL.


In case you are referring to my post, it''s pretty clear that you are completely misconstruing what I said. I think it''s obvious enough that nobody here is trying to "act big" by saying that the STL is a useful and powerful tool, and probably much better than anything you could write.

quote:

Anyway, From the MSDN Visual Studio help doc:

"If there is insufficient memory for the allocation request, by default operator new returns NULL. "

with the example:

int *pi = new int[BIG_NUMBER];

if( pi == 0 )
{
cerr << "Insufficient memory" << endl;
return -1;
}


So, checking versus NULL is OKAY with Visual C++ by default.



A standards compliant compiler will throw a bad_alloc exception. It''s generally a good idea to code with the standards in mind.

quote:

As someone previously said DO NOT allocate a new node to check for memory. Because that is just USING up memory!



Yes, the is_full() function will cause memory leaks if you''re not already out of memory.

quote:
It is awkward visually to include a .cpp but there is NO RIGHT WAY...there is ANY WAY you want...that compiles.

People on this list seem to lack any sense of individuality and have a herd mentality...very odd for game developers one would think but when you see the lack of creativity in the gaming world it comes as no surprise.

You are writing YOUR OWN code - experiment and learn --- see what others do so you know what the norms are but do not follow them just because others do.

Anyway, if you don''t want to include the .cpp code just paste in the code and thus have everything in the header file.



He asked for a critique on his code. Someone pointed out that it''s a better idea to put a templated class in a header file, and to avoid including cpp files because it is unnecessary and can be confusing. Where in that did you get the impression that we have a "herd mentality"?

This topic is closed to new replies.

Advertisement