c++ - std::bad_alloc at memory location (probably sth with creating dynamic tables) -


i've got problem creating dynamic table in c++. program breaks , shouts:

unhandled exception @ at 0x75914598 in xxx.exe: microsoft c++ exception: std::bad_alloc @ memory location 0x0107f73c.

i know there i'm missing if kind , point me , how repair it. ll has random value, (but if set value in code underneath i'm getting same error, problem following code), ganerated in function (problem code :/).

full code: http://pastebin.com/gafjd5um

code:

#include <iostream> #include <math.h> #include <cstdio> #include <locale.h> #include <conio.h> #include <cstdlib> #include <time.h>  #include <vector>  class generatepass {  private:     //int length;     int ll=5;      char *lowertab;  public:     void chooselenght();     void createlist();     generatepass()     {         lowertab = new char[ll];     }     ~generatepass()      {          delete[] lowertab;      } };  void generatepass::createlist() {       srand( time( null ) );     int i, j;      for( = 0; < ll; i++ )     {         lowertab[ ] =( char )( rand() % 24 ) + 97;      }     for( = 0; < ll; i++ )     {         cout << lowertab[ ];      } }   int main() { generatepass create; create.createlist();  return 0; } 

your full code failed in "creating " 2 objects.

in full code, didn't initialize ll used in constructor. if intend let user choose length, should not create array in constructor. instead need in choose_length function.

  generatepass()   {     lowertab = null;     ll=0;   }    void generatepass::createlist()  {      if(ll<1 || ll > 1024*1024*1024) throw "invalid length";      lowertab = new char[ll];      ....      <your code here>   } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -