【发布时间】:2014-12-31 16:34:11
【问题描述】:
我正在编写一个函数,它创建一组小于传递给它的限制的素数。出于某种原因,我无法正确进行内存管理;我不断收到“分段错误:11”。代码如下:
#include <stdio.h>
#include <stdlib.h>
void getPrimes(int** primes, int* limit);
int main(void){
int primeLimit = 99;
int* primes;
getPrimes(&primes, &primeLimit);
//Do stuff
free(primes);
return 0;
}
void getPrimes(int** primes, int* limit){
int multiplier; //Number used to multiply by to find numbers that do have factors
int multiple; //Stores the current multiple
int numPrimes = 0; //Number of primes (returned to caller)
int count = 0;
int* marked = (int*)malloc(*limit * sizeof(int)); //Initialize memory and sets it to 0
memset(marked, 0, *limit);
marked[0] = 1; //Set 0 and 1 to be not prime
marked[1] = 1;
for(int base = 2; base < *limit; base++){//Go through each number and mark all its multiples, start with 2
if(!marked[base]){ //If base is already marked, its multiples are marked
multiplier = 2; //Start multiple at 2
multiple = base * multiplier; //Set first multiple for loop
while(multiple < *limit){//Mark each multiple until limit reached
marked[multiple] = 1;
multiplier++;
multiple = base * multiplier;
}
}
}
//Do a sweep to get the number of primes
for(int num = 2; num < *limit; num++){//Go through each number and check if marked
if(!marked[num]){ //Number is prime
numPrimes++; //Increase count of primes if number is prime
}
}
*limit = numPrimes; //update limit to the number of primes
*primes = (int*)malloc(numPrimes * sizeof(int)); //Allocate memory for primes
//Now actually put the primes in the array
printf("Number of Primes: %d\n\n", numPrimes);
for(int num = 2; num < *limit; num++){//Go through each number and check if marked
printf("Num: %d, ", num); //Print it for debugging
printf("Count: %d\n", count);
if(!marked[num]){ //Number is prime
*primes[count] = num; //Append to primes list (returned to caller)
count++; //Increase count of primes if number is prime
}
}
free(marked); //Free the memory used to mark multiples
return;
}
【问题讨论】:
-
你试过调试吗?当调试器出现段错误时,你在哪一行?
-
以下是您如何自己调试的示例:stackoverflow.com/a/4717073/19719
-
它不会触发段错误,但
memset(marked, 0, *limit*sizeof(int));可能会更好。 -
在第二个循环中,
for(int num = 2; num < *limit; num++){、*limit发生了变化,不再是marked的长度。如果numPrime高于*limit,它可能会触发段错误......虽然这不太可能......
标签: c memory memory-leaks segmentation-fault