Thread: just stop working when compile...

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok one last fixup....

    for(b=2;b<=N-1;b++)

    is the same as

    for(b = 2; b < N; b++)

    if N == 5 then N - 1 = 4 so <= makes it exit on 4, returning false on 5. b < N will return true for values 0 .. 4 and false on 5 ...

    It may not seem like much of a difference for relatively small values of N but the <= N -1 adds in a bit of extra code -- suptracting 1 from N and doing 2 comparisons every time-- that will amount to a performance difference on larger values... It could make a difference of minutes if you asked for the first 10,000,000 prime numbers.

  2. #17
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    the loop is about the sieve of Eratosthenes...i dont know if you heard about that,its method which separates prime numbers from all other numbers...

    this loop is okay because this code works fine without dynamic allocation memory,but when i used it compiler start to show message stop working...

  3. #18
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    for input N = 3,

    Code:
        for(b=2;b<=N-1;b++) {       // 2 <= 3-1 is true proceed...
           v=b;
           if(*(p+v)==0) {                             
    	   printf("%d ", b);
    	      for (i=2;v<=N-1;i++) {
    	         v=b*i;                  //1 pass. N= 3, b = 2,i=2, v = 4; p is allocated only 3 int.and you write to *(p+4)!
    	         *(p+v)=1;	   	      	      }                                                
           }		       
        }

  4. #19
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your code is NOT fine!
    Lack of crash != bug free.

    The fact that adding free() makes it crash is proof of that.

    If you're trying to calculate the first N prime numbers, then you need a different test in your inner loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #20
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    i still dont get what should i do... i tried -1,+1,stay the same...

    whatever i put is still put message stop working...

  6. #21
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Frankly, I would ditch v and write:
    Code:
    for (b = 2; b < N; b++) {
        if (p[b] == 0) {
            printf("%d ", b);
            for (i = b * b; i < N; i += b) {
                p[i] = 1;
            }
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #22
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    code and loops works fine...look this code without dynamic allocation...

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main() {
        int *polje=NULL,N,v,b,i;   
        int k[1000]={0};                             
        printf("Enter num elements:");
        scanf("%d",&N);
        
    
     
         
        for(b=2;b<=N;b++) {
           v=b;
           if(k[v]==0) {
    	   printf("%d ", b);
    	      for (i=2;v<=N;i++) {         
    	         v=b*i;              
    	         k[v]=1;	   	       
    	      }                                                 
           }		        
        }				
        
        printf("\nEND");
        
        getch();
        return 0;
    }

  8. #23
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    this is the same code and loops but without dynamic allocation and all works fine...
    sorry all for your time... but i really dont know where is mistake... and i KNOW that it is not in loop because its the same as in this code... its somewhwere in dynamic allocation... but i dont know where...

    thank you all for your help till now...and in advance..

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by notcodemonkey
    code and loops works fine...look this code without dynamic allocation...
    No, it is wrong. In fact, I can get your program to crash by entering 1000 as the input, but even if everything went "okay", that would not mean that your program is correct since it can be observed to be incorrect by inspection.

    My example is based on what I recall from my own implementations of this sieve.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #25
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    thanks laserlight...

    with your piece of code it works...and compiler is quiet...

    thanks...

  11. #26
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by notcodemonkey View Post
    code and loops works fine...look this code without dynamic allocation...
    When it asks for the number of elements, try putting in 1001 ...

  12. #27
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    CommonTater that is not the point...i know that...
    i will be honest...i still dont get it...why my loop doesnot work...

    laserlight if you have a minute i would really be thankfull if you could explain why is your loop working and mine not...

    thanks all of you...and specially to laserlight...

  13. #28
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Suppose that you did change the v<=N loop condition to v < N. Suppose also that v = N - 1. Now, v=b*i; implies that the new value of v is greater than the previous value of v, since i was incremented since the last iteration. Then you access k[v] (or p[v]). We can reason that the array has been accessed out of bounds.

    My version, on the other hand, has the check for i < N come right before the access of p[i], so I never access the array out of bounds.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Have no idea how to compile c++, please help (newbie qn)
    By rholloway in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2008, 08:31 AM
  2. Replies: 8
    Last Post: 01-18-2008, 04:06 AM
  3. Replies: 4
    Last Post: 01-13-2008, 02:14 AM
  4. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 01:36 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 02:58 PM