My "programming in c++" Teacher....

Started by
20 comments, last by ph33r 21 years, 2 months ago
I went to my first "programmin in c++" class today and my teacher introduced himself. He did his undergrad in berkley and got his PhD at MIT in Computer Science - so I'm guessing he must know what he is talking about. But there are some things that urk me about what he said today such as -He told us he would not be teaching us <iostream> and he will be teaching us <iostream.h> because <iostream> isn't ANSI standard - they are pushing it to be ANSI standard but it isn't yet. -He also talked about scope and how in a for loop
    
for(int i=0;i<4;i++)
{
cout << i;
}

int i = 4; 
    
He asked the class what would happen - I said it would produce an error message because your redefining i. He said that it does do that with visual c++ 6.0 but according to standard c++ that int i=0 should be a part of the for loops scope - so it's memory gets deallocated after you finish the for loop. -The last thing he told me that didn't quite settle right was about the register keyword. He explained how it works and how it gives a hint to the compiler to store the variable on the registry. He then said he puts a lot of things on the CPU such as all his for loops. He said it's habit now for him to do this for(register int i = 0; i < 10; i++) Now I think I remeber someone saying you should be carefull about what you put on the registry - I didn't think you should be so careless with that keyword. [edit: fixed <'s & gt;'s] [edited by - Magmai Kai Holmlor on January 30, 2003 11:43:41 AM]
Advertisement
quote:Original post by ph33r

-He also talked about scope and how in a for loop


This is correct. Under gcc this code is perfectly fine because the scope is limited to the for loop. This is part of the standard and should be followed. There are differences between how compiler follow the standards so it it is good to be aware of this.

This is also one of the most common errors when porting code from linux to visual C++.

quote:
-The last thing he told me that didn't quite settle right was about the register keyword.


Well, I think that most compilers are smart enough today to do this without the hints but maybe in his studies he used some older compliers that were not as advanced and this payed off big time. So like you said it's probably habbitual now


-----------
Andrew


[edited by - acraig on January 29, 2003 10:57:36 PM]
It''s only a hint.. if the processor has not open registers, it will simply ignore the hint . I used to use this back when I programmed in Borland Turbo C/C++ 3.0 For Dos (16-bit), and putting the correct variable as a register variable could mean the difference in 200fps, or 20fps (no joke). They can make a huge difference, even things like using ++i, instead of i++ made big differences in inner loop code (the first is faster, because it works directly on the variable, while the second has to copy the variable to a register, perform the inc, and then copy it back). I have since stopped using the register hint, but it was very important back then.

Also, he is correct about VC++ not being 100% C++ ANSI standard. The int i *should* only be in the scope of the for loop, and be able to be re-declared elsewhere in the function scope.

A note on instructors: All of them have their habbits (good or bad) and you have to live with it, whether you like it or not . They aren''t always correct, but they will never admit they are wrong... if you say they are, expect to be disliked for the rest of the semester (whether you are right or wrong is irrelevant). Most are old... which means, things that applied when they were in school, don''t necessarily apply today, so some things they do may not make complete sense to you.
quote:
Also, he is correct about VC++ not being 100% C++ ANSI standard. The int i *should* only be in the scope of the for loop, and be able to be re-declared elsewhere in the function scope.


That isn''t technically true, there is an option somewhere in VC++ called ''disable language extensions'' or something like that which forces VC++ to behave nicely when encountering things that are ANSI standard but for some reason Microsoft didn''t want in their language but removes all the neat things that Microsoft did add. VC.NET I think changed it so the language is ANSI+ compliant (that is compliant with ANSI w/ extensions instead of being either ANSI compliant or having MS extensions and lacking ANSI compliancy).

About the register keyword, it doesn''t really matter what you put in a register in C/C++ because first: the computer can ignore you if it thinks you''re being stupid and second: when it does put something into a register it uses the general-purpose registers (EAX, EBX, ECX, EDX) and not the registers that tend to screw up programs (namely EBP or segment registers).

-- Exitus Acta Probat --
quote:Original post by ph33r
Now I think I remeber someone saying you should be carefull about what you put on the registry - I didn''t think you should be so careless with that keyword.


It doesn''t put a variable "on the registry." It puts a variable in a CPU register. Two very different things.
Interestingly enough, Herb Sutter covered this.

In a nutshell, auto and register are pretty much redundant and therefore of no real value in code aside from extraneous information to the programmer.

MSN
those keywords, particularly "register" date from a time when compilers did not really perform optizations, they pretty much just directly translated each recognized pattern in the source program into certain machine code ... so these:

i++;
++i;
i = i + 1;
i += 1;

all generated different machine code on some architechtures ... because it was not expected for the compiler to analyze the source code for more efficient code creation .. the programmer was expected to spend time writing efficient code.

in the years since, both scientific advances, and economic factors have shifted the burden to the compiler. It is no longer (usually) the programmer''s job to know how to optimize for different CPU models and architectures, it is the compiler verndors. And because the knowledge of the compiler vendor, and the technology present in the code of the compiler, the compiler can usaully far exceed the optimization abilties of even great programmers, for small loops, and intrafunction blocks. It is now the programmer''s job to move optimizations to a higher level ... which functions to call when ... when do you not need to test for conditions which have already been guaranteed ... should a function be virtual ... is polymorphism or generic programming going to more efficient for solving a given problem ...

using the register keyword in this day and age makes absolutely no sense, ON A NORMAL DESKTOP PLATFORM, with advanced compilers. If you we''re writing code for an embedded compiler, using an in house compiler, or some such, then you might use it .. BUT if you are using a primative compiler, you won''t get standard C++ support anyway, so you''ll be following advice specific to your sub dialect anyway ... my advice to you, do not consider "auto" and "register" to be meaningfull keywords in the C++ language ... concentrate on learning the really uses for "static" and "extern" instead ... as these have actual design implications.
Incidently if you want decent for-scoping in VC++ without having all your standard headers failing to compile, you can use this dandy little trick...

#define for if (false); else for
So...it was your first time in your C++ class, the prof introduced himself, and all of a sudden, he put that code in front of his student? Is your C++ class a beginning C++ class or an advanced C++ class?

I''m sorry, but that just doesn''t make any sense to me. He asked that kind of question in the beginning of the semester?

return 0;
This is a beggining C++ class - this was the second class. The pre-req for this class was programming in C.

It''s going to be a looong semester

This topic is closed to new replies.

Advertisement