A common error when writing code

Source: Internet
Author: User

Lab stage to do exercises, among which, change bug title:
#include <iostream> #include <vector>int main (int, char**) {std::vector<int>** Pprandomdata = Distributerandomint (unsigned int i=0; i<1000; i++) {if (Pprandomdata[i]) {std::cout << "Elements in [" & lt;< i << "]:"; for (unsigned int k=0; K<pprandomdata[i]->size (); k++) {std::cout << pprandomdata[i]->at (k) << "";} Std::cout << Std::endl;}} Delete[] Pprandomdata;return 0;} std::vector<int>** distributerandomint (int vMod) {assert (VMod > 0);std::vector<int> **ppcontainer; Ppcontainer = new Std::vector<int>*[vmod];srand (0); for (int i=0; i<1000; i++) {int randomnum = rand (); STD:: vector<int> *pcontainer = ppcontainer[randomnum%vmod];if (Pcontainer = NULL) {pcontainer = new std::vector<int& Gt;;}pcontainer->push_back (randomnum);} return Ppcontainer;}

This question has been tested at several test centers:

1 when you first apply for an array pointer, be sure to initialize each member of the array

2 assignment and reference between pointer field pointers

The difference between 3= and = =

4 memory leaks: releasing pointers and releasing pointers

5 This migic number

Changes such as the following:

#include <assert.h>//add#include <iostream> #include <vector>std::vector<int>** Distributerandomint (int vMod); const INT total = 100;int main (int, char**) {std::vector<int>** Pprandomdata = Distributerandomint (+);//for (unsigned int i=0; i<1000; i++) for (unsigned int i=0; i<100; i++)//100 migic number{i F (Pprandomdata[i]) {std::cout << "Elements in [" << I << "]:", for (unsigned int k=0; K<pprandomdata[i]->size (); k++) {std::cout << pprandomdata[i]->at (k) << "";} Std::cout << Std::endl;}} for (int i=0; i<100; ++i) {if (Pprandomdata[i]! = NULL) Delete pprandomdata[i];//releases the memory for each piece of application in the array}delete[] pprandomdata; return 0;} std::vector<int>** distributerandomint (int vMod) {assert (VMod > 0);std::vector<int> **ppcontainer; Ppcontainer = new Std::vector<int>*[vmod];for (int i=0; i<vmod; ++i)//Initialize array pointer {ppcontainer[i] = NULL;} Srand (0); for (int i=0; i<1000; i++) {int randomnum = rand ();//std::vector<int> *pcontainer = ppcontainer[randomnum%vmod];std::vector<int>*& PContainer = ppContainer[ randomnum%vmod];//pointer reference, to change the value of the pointer//if (Pcontainer = null) if (Pcontainer = = null)//= = = Difference {//pcontainer = new std::vector& Lt;int>;pcontainer = new Std::vector<int>;} Pcontainer->push_back (randomnum);} return Ppcontainer;}


Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

A common error when writing code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.