Thread: just stop working when compile...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    13

    just stop working when compile...

    this code is about prime numbers ... and compile print prime numbers but then shows message stop working.. i used dynamic memory allocation but cant find where is problem... thanks in advance...

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main() {
        int *p=NULL,N,v,b,i=0;   
    
        printf("Enter number of elements array:");
        scanf("%d",&N);
        
        p=(int *)malloc(N*sizeof(int)); 
        
        if(p==NULL) {
           printf("Out of memory");
           exit(1);
        }
    
        while(i<=N) { 
           *(p+i)=0;
           i++;
        }
        
        for(b=2;b<=N;b++) {
           v=b;
           if(*(p+v)==0) {
    	   printf("%d ", b);
    	      for (i=2;v<=N;i++) {
    	         v=b*i;               /*  2  3  4  5  6  7  8  9  10  11 ... n  */
    	         *(p+v)=1;	   	      	      }                                                
           }		       
        }				 
        
        free(p); 
        
        printf("\n\nEnd");   /* i put this here just to see if compiler will go to this point,compile will not print this,he just shows message "stop working" before he go to this line */ 
        
        getch();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    i erased one line
    free(p);

    and now there is no message...and all works fine and he prints the last line...
    why...we should always use free after dynamic memory allocation...is not that right...

    i am now really confused...?

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You allocated N elements so valid indices would be 0 to N-1. inclusive.
    You are having heap overrun.

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    i dont get it... please explain...

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    From your code....

    Code:
      p=(int *)malloc(N*sizeof(int)); 
     
    ...
       
      while(i<=N) { 
         *(p+i)=0;
           i++;
        }
    
    ...
       for(b=2;b<=N;b++) {
    
    ...
     for (i=2;v<=N;i++) {
    The variable p points to an array of n elements, numbered 0 to N-1. That is to say that when N = 5 you have an array of elements numbered from 0 to 4. Your loops terminate on <= condtions thus terminating at 5 which is out of bounds and attempts to access unallocated memory.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you allocate N, but you also have <= N in your loops.

    You're stepping off the end of your allocation.

    The point that you notice that the damage has been done is when you call free.
    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.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    i think i understand what zou are saying...

    but if i put
    ...
    for(b=2;b<=N-1;b++) {

    ...
    for (i=2;v<=N-1;i++) {


    it still is not working...he still shows message stop working....

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    Well you allocate N, but you also have <= N in your loops.

    You're stepping off the end of your allocation.

    The point that you notice that the damage has been done is when you call free.
    Hi Salem,
    This drove me nuts when I started in C. Coming from pascal where it was legitimate to declare and first and last number for array indexes ( array x[11..43]; ) and not being keenly aware of how many elements there were, I found myself constantly confusing the "number of elements" ( int x[10]; ) with the "numbers of the elements"... It took quite a while to adjust to the 0 to n-1 thing.

    I'm thinking that's the same mistake our friend is making.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    And your while loop?

    Post your latest code
    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.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by notcodemonkey View Post
    i think i understand what zou are saying...

    but if i put
    ...
    for(b=2;b<=N-1;b++) {

    ...
    for (i=2;v<=N-1;i++) {


    it still is not working...he still shows message stop working....
    Try just using the v < N comparison which is pretty much bog standard in C. If it's getting blown up it's probably at your while statement... it just doesn't report it till later.

  11. #11
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    its just one difference ...i saw that calloc inicialize all elements to 0 so i put that...and what you said about loop to N-1..i put it and its still problem...
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main() {
        int *p=NULL,N,v,b,i=0;   
    
        printf("Enter number of elements array:");
        scanf("%d",&N);
        
        p=(int *)calloc(N,sizeof(int)); // inicijalizira sve na 1 za razliku od malloc
        
        if(p==NULL) {
           printf("Out of memory");
           exit(1);
        }
        
        for(b=2;b<=N-1;b++) {
           v=b;
           if(*(p+v)==0) {
    	   printf("%d ", b);
    	      for (i=2;v<=N-1;i++) {
    	         v=b*i;               /*  2  3  4  5  6  7  8  9  10  11 ... n  */
    	         *(p+v)=1;	   	      	      }                                                
           }		       
        }				 
        
        free(p);
        
        printf("\n\nEnd");   /* this i put here just to see if compiler will go to this point,compile will not print this,he just shows message "stop working" before he go to this line */ 
        
        getch();
        return 0;
    }

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > for (i=2;v<=N-1;i++)
    Maybe test i, rather than v ?
    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.

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    CommonTater i try using that too..
    but its still not working to the end...

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    13
    v is important in this loop so i think that i should not move this...

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Fine, print the value of v and i as you go, and see how large i gets before you end the 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.

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