简体   繁体   中英

C malloc and free does not work

I am new to C and I have some troubles with malloc and free. I can not figure it out where is the problem. I have a program with prime.c and main.c

I use malloc allocation for my array and when I call free in main.c valgrind shows me this error

==13518==     in use at exit: 0 bytes in 0 blocks
==13518==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==13518== 
==13518== All heap blocks were freed -- no leaks are possible
==13518== 
==13518== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==13518== 
==13518== 1 errors in context 1 of 1:
==13518== Invalid read of size 1
==13518==    at 0x40D2DFB: ____strtol_l_internal (in /usr/lib/libc-2.23.so)
==13518==    by 0x40D2C68: strtol (in /usr/lib/libc-2.23.so)
==13518==    by 0x40CFAFF: atoi (in /usr/lib/libc-2.23.so)
==13518==    by 0x80487B0: main (in /media/test/prime)
==13518==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==13518== 
==13518== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

here are my two classes

prime.c

    #include <stdio.h>
#include <stdlib.h>
#include "prime.h"
#include <stdint.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#include <unistd.h>

uint64_t* findPrime(uint64_t max ,size_t* primeCount)
{

    int cnt=0;
    uint64_t *array;
    array = (uint64_t *) malloc(max * sizeof(uint64_t));


    if(array ==NULL)
    {

    return (uint64_t*) 1;
    }


for(int i =2; i < max;i++)
    {
    array[i] = i;
    }

for(int i = 2;i <= sqrt(max);i++)
    {

    if(array[i] != 0)
    {

for(int j = (i*i); j< max; j =j+i)
        {
    array[j] = 0;
        }
    }
    }

for(int i =2; i <max; i++)
    {
        if(array[i])
        {
        cnt++;
        }
    }

    *primeCount = cnt;
return array;
}

main.c

#include <stdio.h>
#include <stdlib.h>
#include "prime.h"
#include <string.h>

int main(int argc, char* argv[]){

    int len = argc-1;
    int limit = atoi(argv[1]);
    int ret =1;
    size_t pc =0; 


    if(len == 1)
    {
    limit = atoi(argv[1]);
    }
    else if(len == 2)
    {
    limit = atoi(argv[2]);
    ret = strcmp(argv[1],"-p");

    if(ret != 0)
    {
    printf("Invalid input!\n");
    return 1;
    }
    }
    else
    {
    printf("Wrong number of arguments!!!\n");
    return 1;    
    }

    uint64_t* primes  = findPrime(limit, &pc);
       printf("Total number of primes %zu \n", pc);

    if(ret == 0)
    { 
        for(int i =2; i < limit;i++)
        {
        if(primes[i] != 0)
        {
            printf("primess: %lld \n", primes[i]);
        }    
        }
    }
    free(primes);
    }

The stack trace in the valgrind output attributes the error to one of the three calls to atoi() by main() . The specific problem appears to be that a null pointer was passed to that function. The argument to each atoi() call is an element of the argv array, so your (immediate) problem is with program argument handling, not with dynamic memory allocation.

It looks like the problem must be here:

int limit = atoi(argv[1]);

You perform that atoi() call unconditionally, but when argc is less than 2, it exhibits undefined behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM