glibc detected on multiple malloc

This is a discussion on glibc detected on multiple malloc within the C Programming forums, part of the General Programming Boards category; Code: /* *Sieve of Eratosthenes * * Programmed by Michael J. Quinn * * Last modification: 7 September 2001 */ ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    12

    glibc detected on multiple malloc

    Code:
    /*
     *Sieve of Eratosthenes
     *
     *   Programmed by Michael J. Quinn
     *
     *   Last modification: 7 September 2001
     */
    
    #include "mpi.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define MIN(a,b)  ((a)<(b)?(a):(b))
    
    int main(int argc, char *argv[])
    {
       int    count;        /* Local prime count */
       double elapsed_time; /* Parallel execution time */
       int    first;        /* Index of first multiple */
       int    global_count; /* Global prime count */
       int    high_value;   /* Highest value on this proc */
       int    i;
       int    id;           /* Process ID number */
       int    index;        /* Index of current prime */
       int    low_value;    /* Lowest value on this proc */
       char  *marked;       /* Portion of 2,...,'n' */
       int    n=(double)atoi(argv[1]);            /* Sieving from 2, ..., 'n' */
       int    p;            /* Number of processes */
       int    proc0_size;   /* Size of proc 0's subarray */
       int    prime;        /* Current prime */
       int    size;         /* Elements in 'marked' */
       char initMarked[(int)sqrt(n)];
       int sqn;
       int nop=1;
       int *primes;
       
    
      sqn = sqrt((double)atoi(argv[1])); 
      if(sqn%2==0)
        sqn = sqn/2;
      else
        sqn = sqn/2+1;
    	
       for (i = 0; i < sqn; i++) initMarked[i] = 0;
       prime = 2;
       low_value = 2;
       index = 0;
       nop = 1;
       do {
          if ((2*prime-1) * (2*prime-1) > (2*low_value-1))
             first = ((2*prime-1) * (2*prime-1) - (2*low_value-1))/2;
          else {
             if (!((2*low_value-1) % (2*prime-1))) first = 0;
             else{
     		first = (2*prime-1) - ((2*low_value-1) % (2*prime-1));
    		first = (first%2==0)?first/2:(first+2*prime-1)/2;
    	}
          }
          for (i = first; i < sqn; i +=2*prime-1){ initMarked[i] = 1;}
             while (initMarked[++index]);
             prime = index + 2;	 
    nop++;	
       } while ((2*prime-1) * (2*prime-1) <=n );
    	primes=(int*) malloc (size*sizeof(int));;
    	index = 0;
    	for(i=0;i<sqn;i++){
    		if(!initMarked[i]){
    			primes[index]=2*i+3;
    			index++;			
    		}
    	}
    //MPI_Bcast(&primes,nop,MPI_INT,0,MPI_COMM_WORLD);
    printf("%d\n",nop);
    //MPI_Init(&argc,&argv); 
    int rc = MPI_Init(&argc,&argv);
       if (rc != MPI_SUCCESS) {
         printf ("Error starting MPI program. Terminating.\n");
         MPI_Abort(MPI_COMM_WORLD, rc);
         }
    
    MPI_Barrier(MPI_COMM_WORLD);
    MPI_Comm_rank(MPI_COMM_WORLD,&id);
    MPI_Comm_size(MPI_COMM_WORLD,&p);
    elapsed_time = -MPI_Wtime(); 
       /* Start the timer */
    
       if (argc != 2) {
          if (!id) printf("Command line: %s <m>\n", argv[0]);
          MPI_Finalize();
          exit(1);
       }
    
       n = atoi(argv[1]);
       if(n%2==0)
       n=n/2;
       else
       n=n/2+1;
    
       /* Figure out this process's share of the array, as
          well as the integers represented by the first and
          last array elements */
    
       low_value = 2 + (id)*(n-1)/p;
       high_value = 1 + (id+1)*(n-1)/p;
       size = high_value - low_value + 1;
    
       /* Bail out if all the primes used for sieving are
          not all held by process 0 */
       proc0_size = (n-1)/p;
       if (2*(2 + proc0_size) < (int) sqrt((double) ((2.0)*n))) {
          if (!id) printf("Too many processes\n");
          MPI_Finalize();
          exit(1);
       }
    
       /* Allocate this process's share of the array. */
       marked = (char*) malloc (size*sizeof(char));
       if (marked == NULL) {
          printf ("Cannot allocate enough memory\n");
          MPI_Finalize();
          exit(1);
       }
    
       for (i = 0; i < size; i++) marked[i] = 0;
       index = 0;
       do {
    	prime = primes[index++];
    	//printf("prime = %d id = %d\n",prime,id);
          if (prime * prime > (2*low_value-1))
             first = (prime* prime - low_value)/2;
          else {
             if (!((2*low_value-1) % prime)) first = 0;
             else{ 
    		first = prime - ((2*low_value-1) % prime);
    		first = ((first%2==0)?first/2:(first+prime)/2);
    	
    	}
          }
          for (i = first; i < size; i += prime) marked[i] = 1;
       } while (index < nop);
       count = 0;
       for (i = 0; i < size; i++)
          if (!marked[i]) count++;
       MPI_Reduce(&count, &global_count, 1, MPI_INT, MPI_SUM,
          0, MPI_COMM_WORLD);
    
       /* Stop the timer */
    
       elapsed_time += MPI_Wtime();
    
    
       /* Print the results */
    
       if (!id) {
          printf("There are %d primes less than or equal to %d\n",
             global_count, n);
          printf("SIEVE (%d) %10.6f\n", p, elapsed_time);
       }
       MPI_Finalize();
       return 0;
    }
    I am getting glibc detected at second malloc usage? Is there any specific reason?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I think you left out a word. What did glibc detect?

    Anyway you're probably allocating a negative amount of memory, what with a 50% chance of size being negative.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    Quote Originally Posted by tabstop View Post
    I think you left out a word. What did glibc detect?

    Anyway you're probably allocating a negative amount of memory, what with a 50% chance of size being negative.
    Nope I am not allocating negative amount. One more thing I forgot to mention..

    When size is 39, my programs runs fine. But when it is 40 the actual problem starts.

    "glibc detected" is when memory is corrupted I guess we get this error.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How do you know what size is (unless you're doing backtrace, I guess)? You never set it anywhere in your code.

    And the other point is that you have clipped out the important words in the message. The full message is "glibc detected ... at et cetera" where the ... tells you what it detected.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    Quote Originally Posted by tabstop View Post
    How do you know what size is (unless you're doing backtrace, I guess)? You never set it anywhere in your code.

    And the other point is that you have clipped out the important words in the message. The full message is "glibc detected ... at et cetera" where the ... tells you what it detected.
    Stack trace for you.

    I am checking size by using printf.

    Code:
    *** glibc detected *** /home/kuchick/par_comp/assign_23: free(): invalid size: 0x08097018 ***
    ======= Backtrace: =========
    /lib/libc.so.6[0x4ec071e0]
    /lib/libc.so.6[0x4ec08421]
    /lib/libc.so.6(malloc+0x74)[0x4ec09587]
    /home/kuchick/par_comp/assign_23[0x806a0b3]
    /home/kuchick/par_comp/assign_23[0x804a464]
    /home/kuchick/par_comp/assign_23[0x804a2d4]
    /home/kuchick/par_comp/assign_23[0x8049f58]
    /lib/libc.so.6(__libc_start_main+0xdf)[0x4ebb8d7f]
    /home/kuchick/par_comp/assign_23(xdr_u_long+0x51)[0x8049cb9]
    ======= Memory map: ========
    08048000-0808d000 r-xp 00000000 00:19 47222      /home/kuchick/par_comp/assign_23
    0808d000-0808e000 rw-p 00045000 00:19 47222      /home/kuchick/par_comp/assign_23
    0808e000-080da000 rw-p 0808e000 00:00 0          [heap]
    4eb86000-4eba0000 r-xp 00000000 00:0c 6373574    /lib/ld-2.3.6.so
    4eba0000-4eba1000 r--p 00019000 00:0c 6373574    /lib/ld-2.3.6.so
    4eba1000-4eba2000 rw-p 0001a000 00:0c 6373574    /lib/ld-2.3.6.so
    4eba4000-4ecc7000 r-xp 00000000 00:0c 6373584    /lib/libc-2.3.6.so
    4ecc7000-4ecc9000 r--p 00122000 00:0c 6373584    /lib/libc-2.3.6.so
    4ecc9000-4eccb000 rw-p 00124000 00:0c 6373584    /lib/libc-2.3.6.so
    4eccb000-4eccd000 rw-p 4eccb000 00:00 0
    4eccf000-4ecf2000 r-xp 00000000 00:0c 6373598    /lib/libm-2.3.6.so
    4ecf2000-4ecf3000 r--p 00022000 00:0c 6373598    /lib/libm-2.3.6.so
    4ecf3000-4ecf4000 rw-p 00023000 00:0c 6373598    /lib/libm-2.3.6.so
    4ed11000-4ed1f000 r-xp 00000000 00:0c 6373619    /lib/libpthread-2.3.6.so
    4ed1f000-4ed20000 r--p 0000d000 00:0c 6373619    /lib/libpthread-2.3.6.so
    4ed20000-4ed21000 rw-p 0000e000 00:0c 6373619    /lib/libpthread-2.3.6.so
    4ed21000-4ed23000 rw-p 4ed21000 00:00 0
    4efd6000-4efdf000 r-xp 00000000 00:0c 6373596    /lib/libgcc_s-4.0.2-20051126.so.1
    4efdf000-4efe0000 rw-p 00009000 00:0c 6373596    /lib/libgcc_s-4.0.2-20051126.so.1
    4f0fe000-4f106000 r-xp 00000000 00:0c 6373621    /lib/librt-2.3.6.so
    4f106000-4f107000 r--p 00007000 00:0c 6373621    /lib/librt-2.3.6.so
    4f107000-4f108000 rw-p 00008000 00:0c 6373621    /lib/librt-2.3.6.so
    4f108000-4f112000 rw-p 4f108000 00:00 0
    b7d00000-b7d21000 rw-p b7d00000 00:00 0
    b7d21000-b7e00000 ---p b7d21000 00:00 0
    b7ef4000-b7ef6000 rw-p b7ef4000 00:00 0
    b7f03000-b7f05000 rw-p b7f03000 00:00 0
    bf8ef000-bf905000 rw-p bf8ef000 00:00 0          [stack]
    ffffe000-fffff000 ---p 00000000 00:00 0          [vdso]

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So it detected an invalid size. Specifically the size allocated there, and it even shows you size and how it isn't 40 (or 160).

    Again, when you reach this line:
    Code:
    primes=(int*) malloc (size*sizeof(int));;
    size is a completely random value (I don't think I want to know why (a) it is indented so badly and (b) why it has an extra semicolon at the end) -- so fix that, either by setting size to the value it's supposed to be or using the correct value instead of size.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    Quote Originally Posted by tabstop View Post
    So it detected an invalid size. Specifically the size allocated there, and it even shows you size and how it isn't 40 (or 160).

    Again, when you reach this line:
    Code:
    primes=(int*) malloc (size*sizeof(int));;
    size is a completely random value (I don't think I want to know why (a) it is indented so badly and (b) why it has an extra semicolon at the end) -- so fix that, either by setting size to the value it's supposed to be or using the correct value instead of size.
    Sorry thats my mistake its nop, not size. And extra semicolon doesnt harm I guess.

    That still doesnt solve the probelm

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, wait. Are you trying to tell me that either (1) you typed that code in from scratch and it has nothing whatsoever to do with your code or (2) you changed size to nop and nothing happened?

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    Quote Originally Posted by tabstop View Post
    So, wait. Are you trying to tell me that either (1) you typed that code in from scratch and it has nothing whatsoever to do with your code or (2) you changed size to nop and nothing happened?
    2nd assumption is correct, that is supposed to be nop not size.

  10. #10
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Hold the phone. . . .Looky looky looky:
    Quote Originally Posted by dump
    *** glibc detected *** /home/kuchick/par_comp/assign_23: free(): invalid size: 0x08097018 ***
    Now, what this really means is that 0x08097018 is COMPLETELY out of memory. . . and how do you expect to free() a pointer location that doesn't exist?

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    Quote Originally Posted by Kennedy View Post
    Hold the phone. . . .Looky looky looky:

    Now, what this really means is that 0x08097018 is COMPLETELY out of memory. . . and how do you expect to free() a pointer location that doesn't exist?
    I guess, I am not using free() anywhere in my program.

  12. #12
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    GlibC thinks that something is attempting to free memory somewhere. All those macros you are calling. . . what do they do?

    [edit]And, oh by the way, who the crap is:
    Quote Originally Posted by your code
    *Sieve of Eratosthenes
    *
    * Programmed by Michael J. Quinn
    *
    * Last modification: 7 September 2001
    that ain't you, messa thinks. . . do your own work, for cryin' out loud. [/edit]
    Last edited by Kennedy; 10-12-2009 at 10:35 PM.

  13. #13
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    Quote Originally Posted by Kennedy View Post
    GlibC thinks that something is attempting to free memory somewhere. All those macros you are calling. . . what do they do?

    [edit]And, oh by the way, who the crap is:that ain't you, messa thinks. . . do your own work, for cryin' out loud. [/edit]
    Does that matter to you.

    If you cant help, I will be grateful if you keep quiet.
    Last edited by mickey123; 10-12-2009 at 10:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using malloc multiple times on one variable
    By Bladactania in forum C Programming
    Replies: 8
    Last Post: 02-13-2009, 10:53 AM
  2. glibc detected malloc(): memory corruption
    By totalnewbie in forum C Programming
    Replies: 6
    Last Post: 01-12-2009, 05:21 AM
  3. glibc detected.
    By eXeCuTeR in forum C Programming
    Replies: 19
    Last Post: 07-09-2008, 01:38 PM
  4. glibc malloc() and Posix Threads
    By scioner in forum C Programming
    Replies: 1
    Last Post: 05-01-2008, 05:42 PM
  5. *** glibc detected *** free(): invalid next size
    By icebabe in forum C Programming
    Replies: 2
    Last Post: 05-24-2006, 12:09 PM

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