|
Prev: How to build a string on the fly, using std::ostringstream?
Next: N2575: Initializer lists - Alternative mechanism and rationale
From: philmasterplus on 23 Apr 2008 04:16 Here goes my list of questions that have been bugging me for a couple of days. 1. It seems that console I/O with "cout <<" and "cin >>" results in a bloated executable, whereas I/O functions from C such as printf() and scanf() make smaller, faster programs. Are there any other C/C++ console I/O functions available, and which of them are fastest? Also, do you think I should stick to cin and cout when regarding speed, portability and the C++ philosophy? 2. I am using conio2.h from http://conio.sourceforge.net/ in my Visual C++ 2008 projects to create a 16-color text user interface. Is there a portable (for Windows, Linux, and Mac) alternative to conio2? I�d like to have all functionality that conio2 provides. (The conio2 site provides documentation.) 3. (If there is no suitable alternative to conio2) Microsoft Visual C+ + Express 2008 seems to have a problem with structures. It gives a syntax error with the following code: someFunction(var1, var2, (COORD) {myInt1, myInt2}); And this one: COORD cTemp = {0, 0}; To fix these issues (which came from the conio2 library mentioned above), I have substituted the above to the below: COORD cTemp; //Temporary measure for VCX 2008 cTemp.X = myInt1; cTemp.Y = myInt2; someFunction(var1, var2, cTemp); Is there a way to eliminate the error without using temporary variables? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Christopher on 23 Apr 2008 16:07 On Apr 23, 2:16 pm, philmasterplus <philmasterp...(a)gmail.com> wrote: > Here goes my list of questions that have been bugging me for a couple > of days. > > 1. It seems that console I/O with "cout <<" and "cin >>" results in a > bloated executable, whereas I/O functions from C such as printf() and > scanf() make smaller, faster programs. Are there any other C/C++ > console I/O functions available, and which of them are fastest? Also, > do you think I should stick to cin and cout when regarding speed, > portability and the C++ philosophy? What does size of the executable have to do with the speed at which it executes? It probably grew because it happened to be the first call to the standard C++ library you made, which causes the library to link. However, if you intend on writing C++, than surely you are going to cause the link at some point. It would be pretty hard to write a C++ program without the C++ library. > 2. I am using conio2.h fromhttp://conio.sourceforge.net/in my Visual > C++ 2008 projects to create a 16-color text user interface. Is there a > portable (for Windows, Linux, and Mac) alternative to conio2? I�d like > to have all functionality that conio2 provides. (The conio2 site > provides documentation.) I don't know of any such libraries that change the colour of text on in your DOS box. I highly doubt you would find anything from MS either as they would be happy if the "console" did not exist at all. > 3. (If there is no suitable alternative to conio2) Microsoft Visual C+ > + Express 2008 seems to have a problem with structures. It gives a > syntax error with the following code: > someFunction(var1, var2, (COORD) {myInt1, myInt2}); I doubt it has anything to do with your IDE. You are attempting to cast something to a COORD without providing that something. Ask yourself how the compiler is supposed to know what a {myInt1, myInt2} is before it tries to cast it. It looks like you are trying to do something like someFunction(var1, var2, COORD(myInt1, myInt2)); where COORD has a constructor that takes an 2 int params while myInt1 and myInt2 are ints. If the definition of COORD doesn't provide that for you and you can't change it, than do it the way you did declare it outside the call, which is more readable anyway. What advantage do you have in declaring a temporary in the call anyway? It still gets created and set either way. > And this one: > > COORD cTemp = {0, 0}; what is the definition of COORD? > To fix these issues (which came from the conio2 library mentioned > above), I have substituted the above to the below: > > COORD cTemp; //Temporary measure for VCX 2008 > cTemp.X = myInt1; > cTemp.Y = myInt2; > someFunction(var1, var2, cTemp); That is more readable anyway. > Is there a way to eliminate the error without using temporary > variables? You are making a temporary anyway, either way. Are you thinking that just because you create something inside a function call that it magically doesn't get constructed? Maybe, maybe not. Depends if you on the definition of COORD and if you can change it. A better question is why you would worry about having the ability to construct something and assign values to it within the function call rather than outside? -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Carl Barron on 23 Apr 2008 16:08
In article <4ccb4779-b255-42f9-a3f2-384a2ea3cbac(a)56g2000hsm.googlegroups.com>, philmasterplus <philmasterplus(a)gmail.com> wrote: > Here goes my list of questions that have been bugging me for a couple > of days. > > > 1. It seems that console I/O with "cout <<" and "cin >>" results in a > bloated executable, whereas I/O functions from C such as printf() and > scanf() make smaller, faster programs. Are there any other C/C++ > console I/O functions available, and which of them are fastest? Also, > do you think I should stick to cin and cout when regarding speed, > portability and the C++ philosophy? > The C++ library footprint is ussually bigger than the C library footprint, since the C++ library is in general more general. That genealily is quite useful as soon as you get beyond built-in types and '\0' terminated char arrays. I'd stick to std::cin/cout unless there is a real reason to 'drop down' to the <cstdio> routines [essentially the C IO library]. > 2. I am using conio2.h from http://conio.sourceforge.net/ in my Visual > C++ 2008 projects to create a 16-color text user interface. Is there a > portable (for Windows, Linux, and Mac) alternative to conio2? I'd like > to have all functionality that conio2 provides. (The conio2 site > provides documentation.) > 16 color text is a platform dependent feature and not portable. So the answer is compiler/platform specific, and the results vary. > 3. (If there is no suitable alternative to conio2) Microsoft Visual C+ > + Express 2008 seems to have a problem with structures. It gives a > syntax error with the following code: > > someFunction(var1, var2, (COORD) {myInt1, myInt2}); > > And this one: > > COORD cTemp = {0, 0}; > > To fix these issues (which came from the conio2 library mentioned > above), I have substituted the above to the below: > > COORD cTemp; //Temporary measure for VCX 2008 > cTemp.X = myInt1; > cTemp.Y = myInt2; > someFunction(var1, var2, cTemp); > > Is there a way to eliminate the error without using temporary > variables? I am not familiar with MS 's conio stuff but I assume struct COORD { int X; int Y; }; is the defintion cast in stone. consider struct my_coord:COORD { my_coord(int a,int b):X(a),Y(b){} }; this is nothing but adding a ctor to COORD so slicing should not be a problem. [Passing a my_coord as a COORD only passes the COORD stuff not any thing added by my_coord.] some_function(var1,var2,my_coord(myInt1,myInt2)); should work; now all temps are compiler generated and are destructed upon the completion of some_function(). if this does not compile without warnings then you could use some_function(var1,var2,static_cast<COORD>(my_coord(myInt1,myInt2))); although the static_cast is most likely not needed. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |