在1d数组中动态存储素数

时间:2021-07-01 19:48:15

I am making a program which stores prime numbers in a given range into an 1-d array dynamically. I have read about dynamic memory allocation in c,but i don't know what's wrong with my code. Initially I define an "isprime" function which checks whether a number is prime or not and if the number is prime it returns 1. After that I use a for loop which helps in storing of prime numbers in an array. In the for loop I use an if statement which checks whether the number in the range input by the user is prime or not,and if it is prime it is stored in an array p for which memory is allocated dynamically using malloc. But in the array p no prime numbers are stored and instead garbage values are stored,I don't know why prime numbers are not getting stored in my array?

我正在制作一个程序,将给定范围内的素数动态存储到1-d数组中。我已经读过c中的动态内存分配,但我不知道我的代码有什么问题。最初我定义了一个“isprime”函数,它检查一个数字是否为素数,如果数字是素数,则返回1.之后,我使用for循环,这有助于在数组中存储素数。在for循环中,我使用if语句检查用户输入的范围中的数字是否为素数,如果是素数,则将其存储在数组p中,使用malloc动态分配内存。但是在数组p中没有存储素数而是存储了垃圾值,我不知道为什么素数没有存储在我的数组中?

#include<stdio.h>
#include<math.h>
int isprime(int n)
{
    int i;
    for(i=2;i<sqrt(n);i++)
    {
        if(n%i==0)
        {
            return 0;
        }
    }
    return 1;
}
main()
{
    int *p,i,n,j=1;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        if(isprime(i)&&i!=0&&i!=1)
        {
            p=malloc(j*sizeof(int));//Memory allocation for p should increase as more prime no.s are stored
            p[j-1]=i;
            j++;

        }
    }
    printf("%d\n",p[1]);//garbage value is printed instead of any prime no.
}

1 个解决方案

#1


2  

malloc will return a new memory region each time in your loop, losing your previous data.

malloc将在您的循环中每次返回一个新的内存区域,从而丢失以前的数据。

You need realloc instead

你需要realloc

 int *p = NULL;  // initialize to NULL

and in the loop:

并在循环中:

        p=realloc(p,j*sizeof(int));

so either p address is kept and memory increased, either previous data from p is copied and a new p is issued. Either way it's transparent for you.

因此,要么保留p地址并增加存储器,要么复制p中的先前数据并发出新的p。无论哪种方式,它都对你透明。

(First time, as p is NULL, it acts like malloc)

(第一次,因为p是NULL,它就像malloc一样)

Note that it's rather inefficient to realloc at each iteration. It would be better to resize less often, and keep record of capacity and actual data length. For instance like this:

请注意,在每次迭代时重新分配效率相当低。最好不要经常调整大小,并记录容量和实际数据长度。比如像这样:

Init:

  int growth = 100;
  int capacity = 0;
  int *p = NULL;

and in the loop:

并在循环中:

  if (j>=capacity)
  {
      capacity += growth;
      p = realloc(p,capacity*sizeof(int));
  }

Aside: as comments noted, for the answer to full work, don't omit last value when checking for primes or you'll detect perfect squares as primes.

旁白:正如评论所指出的,对于完整工作的答案,在检查素数时不要省略最后一个值,否则你会发现完美的正方形作为素数。

#1


2  

malloc will return a new memory region each time in your loop, losing your previous data.

malloc将在您的循环中每次返回一个新的内存区域,从而丢失以前的数据。

You need realloc instead

你需要realloc

 int *p = NULL;  // initialize to NULL

and in the loop:

并在循环中:

        p=realloc(p,j*sizeof(int));

so either p address is kept and memory increased, either previous data from p is copied and a new p is issued. Either way it's transparent for you.

因此,要么保留p地址并增加存储器,要么复制p中的先前数据并发出新的p。无论哪种方式,它都对你透明。

(First time, as p is NULL, it acts like malloc)

(第一次,因为p是NULL,它就像malloc一样)

Note that it's rather inefficient to realloc at each iteration. It would be better to resize less often, and keep record of capacity and actual data length. For instance like this:

请注意,在每次迭代时重新分配效率相当低。最好不要经常调整大小,并记录容量和实际数据长度。比如像这样:

Init:

  int growth = 100;
  int capacity = 0;
  int *p = NULL;

and in the loop:

并在循环中:

  if (j>=capacity)
  {
      capacity += growth;
      p = realloc(p,capacity*sizeof(int));
  }

Aside: as comments noted, for the answer to full work, don't omit last value when checking for primes or you'll detect perfect squares as primes.

旁白:正如评论所指出的,对于完整工作的答案,在检查素数时不要省略最后一个值,否则你会发现完美的正方形作为素数。

相关文章