Allocation problem
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Allocation problem

  1. #1
    Join Date
    Nov 2013
    Posts
    4

    Question 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();

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,271

    Re: Allocation problem

    Quote Originally Posted by Piotrifek View Post
    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

  3. #3
    Join Date
    Nov 2013
    Posts
    4

    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 Name:  help.png
Views: 54
Size:  87.3 KB
    thx for replay anyways

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,245

    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);
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Nov 2013
    Posts
    4

    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?

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,047

    Re: Allocation problem

    Quote Originally Posted by Piotrifek View Post
    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/

  7. #7
    Join Date
    Nov 2013
    Posts
    4

    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 08:47 AM.

  8. #8
    Join Date
    Jul 2013
    Posts
    201

    Re: Allocation problem

    Quote Originally Posted by Piotrifek View Post
    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?

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,262

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center