Open main menu

CDOT Wiki β

Changes

C/C++ FAQ

4,497 bytes added, 22:56, 24 September 2013
C/C++ FAQ
Q: What is the correct way to pass a 2D array (called array) into a function? Which of the following is right: array[][],
array[][COLS] or **array?
Q: When creating the header file for a template class and putting the implementation in the header as well there are no errors. However when splitting the deceleration (header) and implementation (making a cpp file) for that class template there are numerous compile errors. What do all these errors mean?Q: Can Stacks, Queues or Lists be referenced using indexes? Q: How to get size of an array without storing the size anywhere? Q: How do you get the length of a file / read the entire file without explicitly knowing its length, and how do you use that data afterwards?Q: How do you compile the happy face on matrix? What is correct way to use CIO library in matrix?</pre> 
'''Q:''' Why is the postfix increment/decrement operator (e.g. a++ and a--) evaluated differently on different compilers?<br>
'''A:''' The evaluation of expressions, especially arithmetic expressions are based on sequence points which are undefined by the language. Arithmetic expressions containing complex postfix calculations are evaluated differently across different compilers because each compiler is unequally efficient. That is to say, these expressions are not portable as each compiler uses a different way to evaluate the expression based on its efficiency implementation. This can be noted by observing the process time of an expression across different platforms, which will be different for the same expression, due to different methods of evaluation. <br>
Example explanation: A stack has numbers 15 10 5 on it and an array allocated to 3 indexes (with values 0, 1, 2) to match the number of Nodes. If a person adds 1 more data to the Stack (push(20))then the array would have to be deleted and reallocated, defeating the purpose of the stack in the first place.
<br>'''Submitted by:''' Team 6 <br><br>
'''Q:''' How to get size of an array without storing the size anywhere? <br>
'''A:''' There are several ways to get the length of an array without explicitly knowing its length. The C method is as follows
<source lang="cpp">
#define SIZEOF_ARRAY( a ) (sizeof( a ) / sizeof( a[ 0 ] ))
</source>
The above define macro can be called to find the size of an array in a C manner. This function exists in the std namespace.<br>
One could also use the below code for a more effective (but c++ only) solution.
<source lang="cpp">
#include <iostream> using namespace std;
template <typename T, size_t N>
 
inline
 
size_t SizeOfArray( const T(&)[ N ] ) {
 
return N;
 
}
 
 
int main() {
 
const char s[] = "Hello world!";
cout << "s[] is " << SizeOfArray( s );
}
</source>
The good thing about the above method is not only its improved efficiency (most compilers will optimize the template function out of existance upon compilation) but the fact that it will not work with a pointer to an array, such as
<source lang="cpp">
const char* s = "Hello world!"; cout << "s is " << SizeOfArray( s )
</source>
Lastly, with C++11 there is an even better method of doing this: std::extent. More info on this trait class can be found [http://www.cplusplus.com/reference/std/type_traits/extent/ here].<br>Submitted by Team42.<br><br>
 
'''Q:''' How do you get the length of a file / read the entire file without explicitly knowing its length, and how do you use that data afterwards? <br>
 
'''A:''' There are several ways to read the contents of a file without knowing it's length and then split the result into usable parts.
First, you could use a string class and load the contents of the entire file into the string object. At this point you can manipulate it as any other string object (not to be confused with a char array) by using methods such as find, substr, erase, etc as outlined [http://www.cplusplus.com/reference/string/string/ here]
<source lang="cpp">
std::ifstream in("myfile", ios::binary);
std::stringstream buffer;
buffer << in.rdbuf();
std::string contents(buffer.str());
</source>
You could also use
<source lang="cpp">
inMyStream.seekg(0,std::ios_base::end);
std::ios_base::streampos end_pos = inMyStream.tellg();
return end_pos;
</source>
to get the length of the file without actually reading it, and then if needed read / append / modify it as required using a char array.. This method of getting a file's length can be used with char arrays / strings / vectors without problems.
Lastly, you could also load the file into a vector and manipulate it as a vector object from that point onwards (more advanced topic) like so
<source lang="cpp">
std::ifstream ifs("foobar.txt", ios::binary);
ifs.seekg(0, std::ios::end);
std::ifstream::pos_type filesize = ifs.tellg();
ifs.seekg(0, std::ios::beg);
std::vector<char> bytes(filesize);
ifs.read(&bytes[0], filesize);
</source>
For any of these methods, to be able to work with the data afterwords simply use a common delimiter when originally generating the file, and then you can use methods like [http://www.cplusplus.com/reference/clibrary/cstring/strtok/ strtok] (for char array), [http://www.cplusplus.com/reference/string/string/find/ .find] combined with [http://www.cplusplus.com/reference/string/string/substr/ .substr] (string class) or use it as a vector (if that's required).<br>Submitted by Team42.<br><br>
 
Q: How do you compile the happy face on matrix? What is correct way to use CIO library in matrix?
Details of problem/question: The CIO library files (console.h, console.cpp, keys.h) and Happyface.cpp were transferred to Documents\A_1 folder on zenit. And when attempt was made to compile happyface.cpp, the lots of errors were generated that stated
"undefined reference to 'cio::console' or
"undefined reference to 'cio::console::(some function)'.
You can find the happyface.cpp at https://scs.senecac.on.ca/~btp300/pages/content/names.html (just scroll to the bottom)
This just happens only on zenit. The program works ok on visual studio. [Asked by Soban Akbar]
 
A: In order to compile the happyface.cpp on linux, you have to use "g++ console.cpp happyface.cpp -lncurses". [Answered by Soban Akbar]