[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
talk:CppVector
This page is to discuss "CppVector". You can ask questions or make comments.
What is a talkpage?

1th March 2007, Bilderbikkel

The code under paragraph 'Memory use' was changed by Jvorwald to this:

  1. include <iostream>
  2. include <vector>
int main(int argc, char *argv[]) { const unsigned int smallSize = 10; const unsigned int bigSize = 10000; const size_t nbytes_in_int(sizeof(int)); const size_t nbytes_in_char(sizeof(char)); int smallArray[smallSize]; int bigArray[bigSize]; std::vector<int> smallVector(smallSize); std::vector<int> bigVector(bigSize); std::cout << "char: " << nbytes_in_char << std::endl; std::cout << "int: " << nbytes_in_int << std::endl; std::cout << "Small array: " << sizeof(smallArray) << std::endl; std::cout << "Big array: " << sizeof(bigArray) << std::endl; std::cout << "Small vector: " << sizeof(smallVector) << " + " << smallSize << " * " << sizeof(smallVector[0]) << std::endl; std::cout << "Big vector: " << sizeof(bigVector) << " + " << bigSize << " * " << sizeof(bigVector[0]) << std::endl; for (size_t ii(0); ii<bigSize; ii++) bigVector[ii] = ii*2; const size_t nbytes(sizeof(bigVector) + bigSize * sizeof(bigVector[0])); const size_t nchar(nbytes/nbytes_in_char); char cc[nchar]; char *cptr; cptr = (char *)(&bigVector); for (size_t ii(0); ii<nchar; ii++) cc[ii] = cptr[ii]; std::vector<int> *vptr; vptr = (std::vector<int> *) cc; if ((*vptr)==bigVector) { std::cout << " Copied into char array correctly" << std::endl; std::cout << " &bigVector = " << &bigVector << ", vptr = " << vptr << std::endl; for (size_t ii(0); ii<bigVector.size(); ii+=1000) std::cout << " bigVector[" << ii << "] = " << bigVector[ii] << " (*vptr)[" << ii << "] = " << (*vptr)[ii] << std::endl; } else { std::cout << " Did not copy into char array correctly" << std::endl; } std::cout << std::endl << "Press anything to exit"; char ch; std::cin.clear(); std::cin.get(ch); return EXIT_SUCCESS; }


That the paragraph was modified, however, made clear that it was not understood which point was made. I, Bilderbikkel, added some text describing what I wanted to show:
  • a static array of size 100 is 10x bigger then a static array of size 10
  • a std::vector of size 100 is as big as a std::vector of size 10
Of the code added, it can be questioned what the point of the author was to make. If the copying of a std::vector to an array and back was to be demonstrated, this could be done better in a seperate paragraph or at the page CppCopy, the page that described the std::copy command.

The coding style can also be debated:
  • The code generates an access violation
  • Why use 'int main(int argc, char *argv[])' if 'int main()' is also standard C++ when you do not use argc and argv to make a point?
  • Why 'return EXIT_SUCCESS' when the Standard states that the closing bracket of main() must have the same effect as 'return 0;' ?
  • The code uses non-standard variable names: why use 'ii' when 'i' is 1000x more common?
  • Why use 'i++' when '++i' is to be preferred?
  • Why use 'char ch; std::cin.clear(); std::cin.get(ch);' when a std::cin.get() does the job as well?
For references on my critique, see e.g. CppMain and CppFor.

last edited (March 1, 2007) by bilderbikkel, Number of views: 280, Current Rev: 1

[Edit this page]  [Page history]  [What links here]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.