Advertisement

Weird C++ type conversion problem (HARD)...

Started by September 03, 2000 03:08 PM
74 comments, last by Dire.Wolf 24 years, 3 months ago
quote:
Dire.Wolf:
One question Wilka, nowhere in either of my operator= functions do I use new or require a ctor. Now you''ve got me wondering. Could you possibly provide me an example of what you mean?


Blame the stupid forum software I saw ''new'' in blue after you called _free(), and thought you where newing something.
Actually, guessing what kind of code an optimizing compiler is going generate isn't the issue. Its _generally_ safe to assume it'll be better than the debug code. And guessing what a compiler will generate for a line of C code is both easy and common practice. Sure, there are ins and outs particular to architecture and compile, but in general you know what the deal is. With C++, you just don't know.

Another great example :

We had a function which involved transforming a ton of character vertices. To do so it allocated an array vectors as a local on the stack - something like 1000/2000. On any modern architecture grabbing that much stack is safe and effectively instantaneous. However, because those vectors were _objects_, and someone had gone in and put in a default contructor which looked like

x = 0.0f;
y = 0.0f;
z = 0.0f;

Everytime you entered that function, you did 2000 * 6 stores. Goodbye cache coherency. In fact, as I recall, there were some cases of people putting these largish arrays in _loops_ which multipled the # of overhead instructions by O(n). Aieeeeeeeee!

C++ has the nasty tendency to hide stuff from you in non-obvious ways.

How many times a day do you see a relative newbie here saying.

"I have the following class" :

class rgb_pixel {
public :
rgb_pixel() { set_rgb(0, 0, 0); }
void set_rgb(ubyte *r, ubyte *g, ubyte *b){
DD->Lock();
blah blah
DD->Unlock();
}
};

"Why does my game run at 2 FPS when I use an array of rgb_pixels of size WidthxHeight to blit all my game bitmaps?"

The point being that he was so caught up in using C++, he didn't

- realize that Lock() and Unlock() are crazy slow.
- take the time to understand the problem and either do a "smart" OOD, or do it in a much simpler already-solved-by-50000-other-people in the past approach.

And then you get a bunch of people saying "you should use some inline assembly in set_rgb() to speed it up". Argh!

Yes. You can make some cool game-relevant code with C++. But don't stuff it down the throats of people just getting their feet wet.

(BTW - apologies for continuing to make this a 2 topic thread. this is just in rebuttal to null_pointer. the last thing we need is to open a general thread about C vs. C++ )

Edited by - daveb on September 7, 2000 3:37:57 PM
Volition, Inc.
Advertisement
quote:
How many times a day do you see a relative newbie here saying...


Now where near as many times a day as I people with array & pointer related problems. They are having these problems because they are newbies, after the problem is solved, and they understand it, they''ll be able to move on their next set of problems

C++ is not an easy language, and it is possible to make some big mistakes in it. The same goes for C, the mistakes are just of a different kind for different reasons (e.g. implicit pointer conversion between pointer types). That doesn''t mean should discourage people from using them.
quote: Original post by Dire.Wolf

I have no problem listening to Dave or you but I will not stop using C++ language features, OOA/D or the STL just because C is more simplistic to understand/use.

BTW As far as I know it is very unlikely that C can be optimized to perform better than C++.

- Dire Wolf
direwolf@digitalfiends.com


Umm... you must be some sort of visionary, because where did I ever say that C was a better choice than C++?

Get your head out of your ass. It is extremely obvious that you do not know the finer details of C++ that Dave or I have pointed out here and in other threads on the message board; please, please please stop posting until you get a clue. Read "Inside the C++ Object Model" or "Large Scale C++ Software Design".

In fact, here''s the most apt quote of the day:

quote: Original post by daveb
Yes. You can make some cool game-relevant code with C++. But don''t stuff it down the throats of people just getting their feet wet.


Ugh.

MSN
Would you recommend:

operator value_type*()
{
return ptr;
}

and/or

const value_type* get() const
{
return ptr;
}

- Dire Wolf
direwolf@digitalfiends.com

P.S. The static_cast<>() was missed when I was cleaning up the code. Originally I was using a void pointer and need to cast it. Thanks for pointing it out though - I could have easily missed it.

Keep the help coming

[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
Let''s keep this friendly

How does the phrase go? Teach someone C as their first language and they''re liable to shoot themselves in the foot. Teach them C++ and they blow off their whole leg.

:p
Volition, Inc.
Advertisement
quote:
Read "Inside the C++ Object Model"


This book is pretty out of date now. A better title would have been "Inside the cfront Object Model". It is one (basic) way that things can be done, but it's not the only way.

Dire.Wolf:

I try to avoid conversion operators, they save you a little typing but they also bring problems of their own. For example, if your replace the user of raw pointers with a smart pointer, if it allows conversion to T* code like this:

delete ptr;  


will still compile, and you'll end up with a bug waiting to happen. It's pretty hard to track down cos the deleted object could still be in memory for a while. By the time you notice the error your a long way from the cause.

I guess it's not as much a problem if your using a smart pointer from the start, but it could still cause you some problems. It's pretty much for the same reasons you've got an explicit constructor.

btw for get(), you should have a const as well as a none const version.

Edited by - Wilka on September 7, 2000 4:15:12 PM
quote:
Teach someone C as their first language and they''re liable to shoot themselves in the foot. Teach them C++ and they blow off their whole leg.


Well that''s what happens with a more powerful weapon , and besides when they''ve only got one leg they''re gonna take a lot more care of it

Yes you can make some big mistakes in C++, but they are a lot harder to make than the C mistakes. As long as you know what your doing, you should keep all your limbs fully intact
INVALID PAGE FAULT

Source code dump:

if( argument.base == opinion && argument.base.reasons == 0 ) poster.IQ = tree_stump.IQ;
else process_argument(assert(argument.location == 0x473F:0x890A:0xFE64983B), argument.size);

Invalid page fault detected.
Recovery initiated...
Cleaning process memory...done
Cleaning process resources...done
Relinking code...done
Restarting process...

*null_pointer is okay now*


quote: Original post by daveb

Bottom line : you''re doing a great disservice to all the new programmers in this group by refusing to see outside of the C++ box. There''s just no argument. They''re going to have more problems learning and experimenting with hardcore C++ than if they used a simple blend of C/C++. Many of them will never be able to complete a cool idea/project they had because they get mired in deep C++. You''re shoving the "C++ is everything and if you can''t understand that, you''re obviously an idiot" mentality down their throats.

I''m outta here


Hmm...well, let''s get a couple of things straight, shall we? First, you''re the one who isn''t asking questions and is trying to force his opinions on other people. You were the one who immediately judged people for what you thought that they were thinking, without asking why they were doing these things. Second, I did not give my opinions on whether a Handle class or a typedef was needed, I simply listed some of the benefits of the Handle class because you were experiencing the temporal phenomena of tunnel vision. Third, the only reason I posted is that I do not like the lopsidedness and narrow-mindedness of most C vs. C++ arguments. I pointed out the things in your arguments that were either 1) obviously untrue or 2) just being wildly assumed without looking at the whole picture. Those were the only things that I said either necessitated or inferred stupidity. You have a right to your own opinion - that doesn''t mean that it is correct. Fourth, I questioned your "hard-earned wisdom" - is that a crime?

Now, if you are going to continue making overgeneralized, angry, and apparently groundless accusations, I may stop reading this post entirely. Refute anything I''ve said and prove it wrong, and I''ll apologize. But until you can prove me wrong I''m not going to apologize or change, because I think I am correct. If you can''t show me where I am wrong, why should I take your word for it?

And as for "doing a great disservice to all the new programmers in this group" - IIRC, I didn''t push my opinion on anyone else. You did. You have consistently read an attitude of "C++ for the sake of C++" into this post - just read over your comments and the replies before that, and ask yourself where anyone told Dire.Wolf to make a Handle class. Find it. You didn''t even ask why he wanted to make the class!

Personally, I don''t care if you use C++ or if you think it''s the most terrible language on the face of the earth, or if your opinion is anywhere in between. I just don''t care. Although smart people tend to have smart opinions (and vice versa), opinions certainly do NOT decide a person''s intelligence or the lack thereof. When you post your opinions on a public forum, expect people to disagree (other people can be subjective, too), and expect people to call your reasons for making those opinions into question (reasons are objective).

Another idea I don''t like is this:


class all_other_software {};
class games {};



It should be more like this:


class software {};
class business : public software {};
class games : public software {};



Games are software - specialized software, yes, but what isn''t? People should eliminate the mentality of "we just don''t do that here" and replace it with either "the game simply won''t be playable with that approach" or "hmm...we might sacrifice a little speed to allow a more extensible engine." I''m not talking about C vs. C++ right now, or low-level vs. high-level. I''m just talking about writing software. Things should be evaluated based on solid reasons that fit the situation rather than personal opinion.

Now we have arrived at the heart of the argument. You believe that C (or the C-like portion of C++) is less complex and easier to work with than C++, and that some parts are unnecessary for programming games. The argument is that C++ as a whole is too impractical to be of benefit, either from a performance standpoint, or just because of code readability. Is that your argument, or have I twisted/mis-stated anything? If so, please correct me, and accept that the following would be invalid (obviously).

If that is your argument, I disagree completely. C would (most obviously) be easier for you if you had more experience in C because of the skew in your viewpoints - you are naturally biased if you have used C more often. C will be easier than C++ for anyone who has more experience in C. So what? Does that mean that C++ is a harder language?

Games are software, and all of the C++ features are of benefit to software developers. If you want to disagree with that, that''s your opinion, but you can''t expect me to just take your word for it, nor can you expect other people to take your word for it. Now, you can go around and make lots of claims that C++ is too hard or C++ is too tricky or whatever you like, but people probably won''t listen to you if you come off like you''re ranting. Most likely, they''ll just use what they like, and what lots of other people say works for them (in a non-condescending, non-flame tone).

I still say that if you tried switching your entire development team to C++ without reading up on the nuances of the language, you should expect trouble, just as if you had tried switching to Java or Smalltalk, or whatever language you can bring to mind, and I don''t see how you can possibly expect the experience to give you a fair and unbiased observation (or "examination") of the language. Having less experience in a language (C, C++, etc.) doesn''t mean you are stupid. Experience != intelligence. Intelligence is the capacity to learn, experience is the amount of learning. If I tried to program in C# (apparently a C++ variant), I should expect to run into problems. It has some very different syntax and conventions, and although lots of things may be similar to C++, the differences will catch me if I don''t read up on the language. And it would be my own fault - I shouldn''t blame the language for my mistakes. Therefore, your sole citation of "experience" in the matter is invalid. Have you experience other than that?


And all of this is unbelievably pointless, because if you''ll read through the post, you still haven''t asked my opinion on whether the Handle class is worth the time in this particular instance (which we still have not defined), nor have I given it. So how is it even possible that I am pushing my opinion on other people here?


- null_pointer
Sabre Multimedia
quote:
MSN:

Get your head out of your ass. It is extremely obvious that you do not know the finer details of C++ that Dave or I have pointed out here and in other threads



There is always someone better than you out there so that is why I am posting. If you weren't so socially inept you would understand that people have opinions and are free to express them. Hopefully you will aquire a little more social grace as you grow up or I doubt you will have a very successful professional career.

The difference between you and daveb is that daveb can express himself in an intellectual way - that comes from age and experience, something you obviously lack.

Fortunately I have no more time to waste on you, so unless you have something constructive to add, please crawl back into whatever hole you came out of.

A most appropriate quote for you:
quote:
"If I want your opinion, I'll give it to you."


On a lighter note:

I just wanted to thank everyone who lended a helping hand. Daveb I definitely appreciate all your input and you did give me something to think about (p.s. I do agree with some of what you pointed out).

Take care,

- Dire Wolf
direwolf@digitalfiends.com

Edited by - Dire.Wolf on September 7, 2000 5:29:00 PM
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com

This topic is closed to new replies.

Advertisement