array of vectors?

This is a discussion on array of vectors? within the C++ Programming forums, part of the General Programming Boards category; My small program crashes before even entering the called method, any ideas? Code: #include <stdio.h> #include <vector> #include <string> using ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    array of vectors?

    My small program crashes before even entering the called method, any ideas?
    Code:
    #include <stdio.h>
    #include <vector>
    #include <string>
    using namespace std;
    
    
    
    vector<int> primeFactors[1000001];
    
    void genPrimeFactors()
    {
    printf("IN");
    int c=0;
    int composites[1000000]={0};
    for(int i=2;i<=1000000;++i)
    {
    	if(!composites[i])
    	for(int j=2*i;j<=1000000;j+=i)
    	{
    	    c++;
    		printf("%d %d",i,j);
    		composites[j] = 1;
    		primeFactors[j].push_back(i);
    	}
    }
    
    for(int i=2;i<=1000000;++i)
    {
        c++;
    	if(!composites[i])
    		primeFactors[i].push_back(i);
    }
    printf("%d",c);
    }
    
    
    int main()
    {
    
    	printf("START");
    	genPrimeFactors();
    return 0;
    }
    OUTPUT : START
    **CRASHES**


    I am very confused

    it never even prints "IN" , it seems as though it never makes it inside the method.
    Last edited by rodrigorules; 02-09-2010 at 09:21 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Im having a hard time wrapping my head around what your trying to do, and the "odd" for loop conditions.

    Anyway, not that this solves the problem, but I imagine your're not seeing "IN" because STDOUT is line buffered. Try changing "printf("IN");" to "printf("IN\n");", to force the output to be printed. Then move your debug statement (the print "IN" line) further into the function until it no longer prints before a crash. Then you have found the line that is causing the crash.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    hmm still crashes without executing "printf("IN\n");"

    I AM VERY CONFUSED!

    copy and paste the code in your compiler maybe, you will see it crash for no reason.

    EDIT: after doing further testing, this is the reason for the crash: ...but it was working earlier, and I think it might have worked for me earlier as well when i ran the code on a computer with ubuntu.
    int composites[1000000]={0};
    CODE crashes:
    Code:
    #include <stdio.h>
    
    void genPrimeFactors()
    {
    int composites[1000000]={0};
    }
    
    
    int main()
    {
    
    	printf("START");
    	genPrimeFactors();
    return 0;
    }
    edit: it appears this code snip. only crashes on my computer.. it works on codepad.org
    Last edited by rodrigorules; 02-09-2010 at 11:34 PM.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    You are most likely running out of stack space because of the size of the composites array. Try finding a compiler/linker switch to increase stack space; Or better, use a dynamic array.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,023
    You can make the array static and it will probably work.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    It seems that i do not run into this 'problem' If i initialize dynamically
    Code:
     
    int* composites = new int[1000001];
    is there any way to have this array set every element to 0 quickly, without having to do a loop of size 1 million?

    thanks!

  7. #7
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Code:
    memset(composites, 0, 1000001*sizeof(int));

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Quote Originally Posted by rodrigorules
    is there any way to have this array set every element to 0 quickly, without having to do a loop of size 1 million?
    According to the C++ standard, you could write:
    Code:
    int* composites = new int[1000001]();
    which will value-initialise each int, and value initialisation for the built-in types is zero initialisation. However, if I remember correctly a bug in the MinGW port of g++ 3.4.5, and possibly some other versions of g++ and maybe even other compilers, causes it to fail to value-initialise a dynamic array of objects of built-in types with this syntax.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by laserlight View Post
    According to the C++ standard, you could write:
    Code:
    int* composites = new int[1000001]();
    which will value-initialise each int, and value initialisation for the built-in types is zero initialisation. However, if I remember correctly a bug in the MinGW port of g++ 3.4.5, and possibly some other versions of g++ and maybe even other compilers, causes it to fail to value-initialise a dynamic array of objects of built-in types with this syntax.
    Thanks laserlight, I didn't know that.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,598
    Actually, since std::vector can be used, just write:
    Code:
    std::vector<int> composites(1000001);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 06:14 PM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  4. Arrays vs Vectors
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2006, 02:06 AM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21