• ### Announcements

#### Archived

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

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

## Recommended Posts

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]

##### Share on other sites
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]

##### Share on other sites
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.

##### Share on other sites
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 --

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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;

##### Share on other sites
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

##### Share on other sites
quote:
Original post by ph33r
-He told us he would not be teaching us and he will be teaching us because isn't ANSI standard - they are pushing it to be ANSI standard but it isn't yet.

C++ was standardized in 1997 and ammended several times afterwards. iostream is definetly standard now, though it might not have been until 1999.

quote:

-He also talked about scope and how in a for loop
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.

He is correct for standard C++. MSVC6 pre-dates the standard, and behaves like C in this regard. MSVC7 has a compiler flag to choose which way the for scope behaves.

To say the memory is deallocated is not perfectly correct, but that is how you should treat it. The variable goes out-of-scope - what happens to its memory is implementation defined. The memory for that variable can be reused later (and not deallocated), or left in limbo until the function returns and the stack space reclaimed all at once, not in small pieces.

[edited by - Magmai Kai Holmlor on January 30, 2003 4:29:03 PM]

##### Share on other sites
yeah; the prof is mostly right, except about iostream.h

to use the STL I think you need iostream

Enjoy the class.

##### Share on other sites
Well, I just can''t resist budding in and saying that my "C programming" teacher just docked marks off my assignment because I didn''t assign sizeof(unsigned) to a constant.... Now that''s wrong, right? sizeof() is a compile time function...

##### Share on other sites
quote:
Original post by Cat_B
Well, I just can''t resist budding in and saying that my "C programming" teacher just docked marks off my assignment because I didn''t assign sizeof(unsigned) to a constant.... Now that''s wrong, right? sizeof() is a compile time function...

No, he''s right ''compile time'' is the key phrase. Constants can''t be changed during ''run time''.

##### Share on other sites
i don''t have time to read through what have been said, but as for ms visual c++, the compiler will ignore the register keyword, and determine on it''s own when something is to be put into the cpu register or not.

##### Share on other sites
quote:
Original post by Vlion
to use the STL I think you need iostream

nope. The following doesn''t need iostream..

  #include <list>int main(void){    std::list <int> stlIntList;    stlIntList.push_back(3);    stlIntList.clear();    return(0);}

##### Share on other sites
In response to Cat_B:

There's nothing wrong with assigning the result of sizeof () to a constant or non-constant value. Perhaps if we knew the context of the mark-deducting code we could see where he's coming from.

So, you may have done:

int size = sizeof (unsigned);

and then not changed the value of 'size', in which case he has a valid point, but if you'd done:

int *an_array = (int *) malloc (NUM_ARRAY_ELEMENTS * sizeof (int));

const int int_size = sizeof (int);
int *an_array = (int *) malloc (NUM_ARRAY_ELEMENTS * int_size);

then he's being incredibly picky, since, as you said, sizeof returns a constant size_t value which the compiler would inline.

As a general rule: if it never changes then make it const. This can help the compiler big time and reduce many potential bugs. (Avoid doing 'typedef {type}* {type}_ptr;' as you can't then declare a 'pointer to constant data' unless you also declare 'typedef const {type}* c_{type}_ptr;' but that's just adding excessive number of types, best off just specifying the ‘const’ness with the 'const' keyword [{/rant}].)

Incidentally, sizeof is an operator and not a function and you don't always need to use parenthesis, so 'sizeof "a string"' is valid (and equals 9 on a single byte char system). You only need parenthesis in the 'sizeof (type-name)' case. Also, sizeof can actually generate code instead of in lining a constant value - specifically the following:

size_t fsize3 (int n)
{
char b[n+3]; // Variable length array.
return sizeof b; // Execution time sizeof.
}

although MSV6 complains about the non-const parameter to the array size (but that compiler is very non-ansi).

Of course, everything above is assuming strict ansi compliance and your own compiler may require things to be done differently, and it applies to C++ too, although things can get nasty with class hierarchies and such.

I seem to have wandered off topic somewhat, sorry for that.

Skizz

[edited by - Skizz on January 31, 2003 9:40:24 AM]

##### Share on other sites
Ok - update from my C++ class today was another interesting episode.

My teacher is defintaly anti-microsoft it''s kind of sad because whenever a piece of code doesn''t work he blames it on MSVC''s compiler and says it should work according to the standard. Then all the students begin to mock him. Like for example I was finished with my work and I look over at the kid and I said "hey you need to put the default values in the prototype not the definition of the function" and his reply was " well mscv will probally let me" - moron

Regardles this is what he told us today

  #include <iostream>using namespace std;int main(){	float f = 4.4f;	cout << f;	{		float f = 5.5f;		cout << f;		cout << ::f;	}	return 0;}''f'' : is not a member of ''global namespace''''  `

He said that this code should compile and access the int main''s variable. I argued with him that it won''t compile and ::f means that it will try to access the variable f if it was only a global variable. He then told me thats the way microsoft does it but in the standard his way was the way it really happens.

I think I''m going to buy a copy of the standard and bring it to class from now on. Some of the stuff he is teaching us is rediculous.

##### Share on other sites

forget about trying to knock heads with your prof. a good portion of what they *teach* is stuff you''ll never use except to pass tests. the coding examples given out like what you posted are just that, examples. they serve only to make a point. so, point made, move on. remember it for the tests and then promptly toss it as you''ll never code anything like that in "real life."

##### Share on other sites
Anonymous Poster - maybe your right maybe I should not try to knock heads with him. But should he be teaching his students a version of c++ that he makes up? He is such a stickler to the standard yet he doesn''t even know the standard he just uses it as a fallback when code doesn''t compile as expected "well..thats microsoft for you never following the standard"

I feal like I''ve wasted my money on this class.

##### Share on other sites
Well. . . at the very least, everytime you see that he is wrong, you''ll be motivated to find out what way is right.

##### Share on other sites
in reality you have to learn the differences.

Enjoy the course. I haven''t been on a course for years and would love to have input. ask lots of questions, don''t bang heads. If you take issue with something, do a bit of research and say, ''i''ve found this information, what does it mean?''

complaining about microsoft is tiring. complaining about lecturers is tiring. instead use your time trying to learn, question, understand and explain and you''ll be a step ahead of most others.

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627684
• Total Posts
2978627

• 9
• 14
• 12
• 10
• 12