1

I've got problem creating a dynamic table in c++. My program breaks and shouts:

Unhandled exception at at 0x75914598 in xxx.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x0107F73C.

I know there is something I'm missing so if could you be so kind and point me where and how to repair it. The ll has random value, (but even if I set a value like in code underneath I'm getting same error, so the problem is with following code), which is ganerated in another function (problem is with this 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( i = 0; i < ll; i++ )
    {
        lowertab[ i ] =( char )( rand() % 24 ) + 97;

    }
    for( i = 0; i < ll; i++ )
    {
        cout << lowertab[ i ];

    }
}


int main()
{
GeneratePass create;
create.CreateList();

return 0;
}
engray
  • 91
  • 1
  • 12

2 Answers2

0

Looking at your full code:

In lines 162 and 163 you construct two distinct instances of your GeneratePass class. In the following lines 167 and 168 you call ChooseLength on one object and createList on the other.

Since the objects do not share any information, the member field ll of the object create is uninitialised, and could therefore be some extremely large value.

In consequence, the allocation fails and throws std::bad_alloc.

To fix this you need to use just one object.

Daniel Jour
  • 15,896
  • 2
  • 36
  • 63
  • Do you mean something like this? `GeneratePass generate; generate.ChooseLenght(); generate.CreateList();` – engray Jun 14 '15 at 18:43
  • Yes, exactly. This way, the member field `ll` inside the object `generate` gets set during `ChooseLenght` and thus has an initialised value when used in `CreateList` – Daniel Jour Jun 14 '15 at 18:46
  • I did it exacly as you said, sadly it still throws same `std::bad_alloc` error :/ – engray Jun 14 '15 at 18:48
  • In your full code, you didn't initialize the "ll" but you used it in the constructor. If you intend to let the user choose the length, you should NOT create the array in your constructor. Instead you need do it in the choose_length function. – Tim3880 Jun 14 '15 at 18:49
  • Wow, @Tim3880 thank you! It finally works. I probably need to get some rest, because I was pretty sure I did try to do it in the first place. – engray Jun 14 '15 at 18:54
  • He is right, it is not the createList member function that is using the value but the constructor. – Daniel Jour Jun 14 '15 at 18:54
  • Yea it's good now. But ty anyway @DanielJour for your time :) – engray Jun 14 '15 at 18:55
0

Your full code failed in "creating " the two objects.

In your full code, you didn't initialize the ll but you used it in the constructor. If you intend to let the user choose the length, you should NOT create the array in your constructor. Instead you need do it in the 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>

 }
Tim3880
  • 2,563
  • 1
  • 11
  • 14
  • Now I bumped into another problem... When choosing Case 2 - randomly generate `l` everything works perfectly. But when I'm trying to get to Case 1 - to `cin` the `l`, I'm getting same old `std::bad_alloc`. Tried to modify it but I failed. My full code [http://pastebin.com/xBaQ0463](http://pastebin.com/xBaQ0463) – engray Jun 14 '15 at 19:21
  • Oh, nevermind I forgot to add `ll; lu; lc; ld` creating functions in case 1, everything is fine – engray Jun 14 '15 at 19:52