-
November 18th, 2013, 08:20 AM
#1
Allocation problem
Hi!
So i have to generate 3kk diffrent 6-digit codes (i know its a lot) i wrote this program and it got quite a few flaws first and main problem is that everytime i start it, i got an error with "bad allocation size" but it works anywys so i tried to ignore it but the biggest number it ever generated was 30k so its only 1/100 of what i need. In bigger cases it just doesnt start i belive he lack memory so firstly is there any way i can make it work? On the other hand i tried to make it save to the file only uniqe codes but in 30k case it saved all of them, so is there a mistake in code or i just got lucky?
I'd be really pleased for any advices and sorry for my english :<
Code:
int j=0;
int w=0;
string tab[30000];
int z=0;
int ch=0;
ofstream File;
File.open("C:\\file.txt");
while (z <=30000)
{
while (j <=5)
{
char los = ( rand() % ( 'Z' - 'A' ) ) + 'A';
tab[z] += los;
j++;
}
while (w <= z-1)
{
if (tab[w] == tab[z])
{
ch=1;
}
w++;
}
if (ch==0)
{
File<<tab[z]<<endl;
}
ch=0;
cout << tab[z] << endl;
j=0;
z++;
}
File.close();
getch();
-
November 18th, 2013, 08:29 AM
#2
Re: Allocation problem
Originally Posted by Piotrifek
Code:
int j=0;
int w=0;
string tab[30000];
int z=0;
int ch=0;
ofstream File;
File.open("C:\\file.txt");
while (z <=30000)
{
while (j <=5)
{
char los = ( rand() % ( 'Z' - 'A' ) ) + 'A';
tab[z] += los;
j++;
}
...
z++;
}
File.close();
getch();
string array of the size 30000 has elements with indexes from zero to 29999, not 30000.
Victor Nijegorodov
-
November 18th, 2013, 08:33 AM
#3
Re: Allocation problem
Ye that's true but it's not really the case since i don't need 30k but 3kk and when i try to do more of them i got only this
thx for replay anyways
-
November 18th, 2013, 08:38 AM
#4
Re: Allocation problem
You're probably trying to create so large an array on the stack that there's no space available. There are a few ways around this, the first that I would investigate is whether you actually need so many strings in memory to begin with. If you do, then one way out is to use the free store, say via a std::vector, e.g.,
Code:
vector<string> tab(3000000);
-
November 18th, 2013, 08:44 AM
#5
Re: Allocation problem
Well it worked now Thank you very much Also if its not big problem can you also check if my uniqueness condition is valid please?
-
November 18th, 2013, 09:41 AM
#6
Re: Allocation problem
Originally Posted by Piotrifek
Well it worked now Thank you very much Also if its not big problem can you also check if my uniqueness condition is valid please?
You should learn to use for loops.
Your dupe check appears to output unique identifiers, but if it finds a dupe, shouldn't it try again? You'll end up with fewer keys than you want if you don't.
Your dupe check is very inefficient. If you store results in a set instead of an array, you can check for dupes much more efficiently than iterating an array every time. Once you start working with larger sizes, you'll find that gets very slow very quickly/
-
November 18th, 2013, 09:44 AM
#7
Re: Allocation problem
Yup you're right about this this is really messy but the problem is i should end up with fewer keys but i do end up with exackly same number as it should be moreover i cant find any duplicates which bothers me quite a lot
i also changed it to
Code:
if (ch==0)
{
File<<tab[z]<<endl;
z++;
}
Last edited by Piotrifek; November 18th, 2013 at 09:47 AM.
-
November 18th, 2013, 11:10 AM
#8
Re: Allocation problem
Originally Posted by Piotrifek
i should end up with fewer keys but i do end up with exackly same number as it should be moreover i cant find any duplicates
There may be two reasons for that. First, shouldn't the w variable start from 0 in each iteration of z? Otherwise you're not comparing the current code with all codes already in the array.
Secondly there are 26 different letters in six positions so the total number of codes is 26^6 = 308.915.776. That's well over 300.000.000 and you're picking just 30.000 of these. Chances are pretty big I think that there are no duplicates in this realtively small sample (but sooner or later there will be one of course).
By the way does the letter Z appear in any of the codes?
-
November 18th, 2013, 12:35 PM
#9
Re: Allocation problem
This may be of interest. It uses a set for duplicate checking.
Code:
#include <fstream>
#include <set>
#include <iostream>
#include <string>
using namespace std;
const int require = 30000;
const int digits = 6;
const string fname = "c:\\file.txt";
int main()
{
set<string> codes;
string acode(digits, ' ');
ofstream File (fname.c_str());
if (!File.is_open()) {
cerr << "Cannot open file " << fname << endl;
return 1;
}
for (int c = 0; c < require;) {
for (int i = 0; i < digits; i++)
acode[i] = (rand() % 26) + 'A';
if (codes.count(acode) == 0) {
codes.insert(acode);
File << acode << endl;
c++;
}
}
return 0;
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|