Hi,
Below is a code that has been written so that I can compute the fibinacci sequence using threads based on user input at command line. The program has been written for a linux OS.
There is a statement close to the exit of the program where I am trying to free the memory that I allocated to store the fibonacci sequence. Every time I try to free the memory using the free() system call, it gives me a "glibc detected" error AFTER printing the correct output. If I comment out the line where I am freeing the memory, there are no issues. Can anyone suggest, what I might be doing wrong?
Thanks
V
Code:
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int *myPtr; //integer pointer to fibonacci sequence generated by fibonacci methode
void *fibonacci(void *param); //thread that generates the fibonacci sequence
int main(int argc, char *argv[]){
pthread_t tid; //thread identifier
pthread_attr_t attr; //thread attributes
int i; //loop control
int intValue = atoi(argv[1]); //convert upper value from char to int
//ensure that there are two arguments provided in the command line. (program name and fibonacci upper bound)
if(argc != 2){
fprintf(stderr, "argumenent count in the command line must be 2! program name and fibonacci upper bound value\n");
return -1;
}
//ensure that fibonacci upper bound value is a positive integer value
if(intValue < 0){
fprintf(stderr, "fibonacci upper bound value must be greater than 0\n");
return -1;
}
// allocate memory space for storing the fibonacci sequence
myPtr = (int*) malloc(intValue + 2);
if(myPtr == NULL){ //Ensure sufficient memory. Exit if there is no memory available
fprintf(stderr, "Unable to allocate memmory\n");
return -1;
}
//initialize thread atttribute with default values
pthread_attr_init(&attr);
//create thread
pthread_create(&tid, &attr, fibonacci, argv[1]);
//wait for thread to exit
pthread_join(tid, NULL);
//print the sequence of integers pointed to by sequence pointer
printf("The Fibonacci sequence are:\n");
for(i = 0; i <= intValue; i++){
printf("%d ", *myPtr);
myPtr++;
if(i + 1 % 21 == 0) //go to a new line after each 20th value
printf("\n");
}
printf("\n\n");
// free(myPtr);
// myPtr = NULL;
}
//Thread will begin control in this function
void *fibonacci(void *param){
int j; //loop control integer
int integerParam = atoi(param); //convert param to integer
int sequence[integerParam + 1];
sequence[0] = 0;
if(integerParam > 0)
sequence[1] = 1;
if(integerParam > 1){
for(j = 2; j <= integerParam; j++){
sequence[j] = sequence[j-1] + sequence[j-2];
}
}
// copy each value in the array over to the memory space pointed to by myPtr
for(j = 0; j <= integerParam; j++)
myPtr[j] = sequence[j];
pthread_exit(0); //exit thread
}
[Output]
*** glibc detected *** ./jonathan: munmap_chunk(): invalid pointer: 0x084cd030 ***
======= Backtrace: =========
/lib/libc.so.6(cfree+0x1bb)[0x6ffceb]
./jonathan[0x80487b2]
/lib/libc.so.6(__libc_start_main+0xdc)[0x6a8e8c]
./jonathan[0x8048561]
======= Memory map: ========
0036a000-0036b000 r-xp 0036a000 00:00 0 [vdso]
00670000-0068a000 r-xp 00000000 68:01 4063261 /lib/ld-2.5.so
0068a000-0068b000 r-xp 00019000 68:01 4063261 /lib/ld-2.5.so
0068b000-0068c000 rwxp 0001a000 68:01 4063261 /lib/ld-2.5.so
00693000-007d1000 r-xp 00000000 68:01 4063268 /lib/libc-2.5.so
007d1000-007d3000 r-xp 0013e000 68:01 4063268 /lib/libc-2.5.so
007d3000-007d4000 rwxp 00140000 68:01 4063268 /lib/libc-2.5.so
007d4000-007d7000 rwxp 007d4000 00:00 0
00808000-0081b000 r-xp 00000000 68:01 4063297 /lib/libpthread-2.5.so
0081b000-0081c000 r-xp 00012000 68:01 4063297 /lib/libpthread-2.5.so
0081c000-0081d000 rwxp 00013000 68:01 4063297 /lib/libpthread-2.5.so
0081d000-0081f000 rwxp 0081d000 00:00 0
00a87000-00a92000 r-xp 00000000 68:01 4063463 /lib/libgcc_s-4.1.2-20080825.so.1
00a92000-00a93000 rwxp 0000a000 68:01 4063463 /lib/libgcc_s-4.1.2-20080825.so.1
08048000-08049000 r-xp 00000000 68:02 21495810 /home/psblnx01/vbd1/Students/jonathan
08049000-0804a000 rw-p 00000000 68:02 21495810 /home/psblnx01/vbd1/Students/jonathan
084cd000-084ee000 rw-p 084cd000 00:00 0
b7527000-b7528000 ---p b7527000 00:00 0
b7528000-b7f2a000 rw-p b7528000 00:00 0
b7f43000-b7f44000 rw-p b7f43000 00:00 0
bffb8000-bffcd000 rw-p bffb8000 00:00 0 [stack]
Aborted
[\Output]