Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 专业阵列处理_C_Arrays_Malloc - Fatal编程技术网

C 专业阵列处理

C 专业阵列处理,c,arrays,malloc,C,Arrays,Malloc,我是一个没有经验的C程序员:我想要5000以下的所有数字都是5的倍数。以下是我目前的做法: int main() { int i; const int max =5000-1; for(i=2; i<(max+1); i++) { if(!(i%5)) { printf("%d\n", i); } } return 0; } intmain() { int i; 常数i

我是一个没有经验的C程序员:我想要5000以下的所有数字都是5的倍数。以下是我目前的做法:

int main()
{
    int i;
    const int max =5000-1;
    for(i=2; i<(max+1); i++)
    {
        if(!(i%5))
        {
            printf("%d\n", i);
        }
    }
    return 0;
}
intmain()
{
int i;
常数int max=5000-1;

对于(i=2;i如果您想要分配完美的大小,您可以尝试以下方法:

    #include <stdio.h>
    #include <stdlib.h>

    int main(){

        int i, j;
        int max = 5000;
        int * ourNumbers = 0;
        int count = 0;

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

            if (i % 5 == 0){

                count += 1;
            }
        }

        printf("\ncount = %d\n", count);

        ourNumbers = (int *) malloc(sizeof (int) * count);

        // and after you can populate your array with those values;
        // like this you will allocate the exact memory

    }
#包括
#包括
int main(){
int i,j;
int max=5000;
int*ourNumbers=0;
整数计数=0;
对于(i=2;i

我知道这不是很有效,但我希望它能帮助您:)

如果您想分配完美的尺寸,您可以尝试以下方法:

    #include <stdio.h>
    #include <stdlib.h>

    int main(){

        int i, j;
        int max = 5000;
        int * ourNumbers = 0;
        int count = 0;

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

            if (i % 5 == 0){

                count += 1;
            }
        }

        printf("\ncount = %d\n", count);

        ourNumbers = (int *) malloc(sizeof (int) * count);

        // and after you can populate your array with those values;
        // like this you will allocate the exact memory

    }
#包括
#包括
int main(){
int i,j;
int max=5000;
int*ourNumbers=0;
整数计数=0;
对于(i=2;i

我知道这不是很有效,但我希望它能帮助你:)

realloc
做你所说的一切。分配一个数组,增加一个数组,缩小一个数组:它完成了一切

int max = 5000; /* why subtract one if you have to add one to use it? */
int *arr = NULL;
int i;

arr = realloc(arr, max * sizeof *arr); /* allocate generous array */
for (i = 0; i < max; i++) {
    /* ... */
}
max = 10000;
arr = realloc(arr, max * sizeof *arr); /* grow array */

max = 100;
arr = realloc(arr, max * sizeof *arr); /* shrink array */

realloc
做你所说的每件事。分配一个数组,增长一个数组,缩小一个数组:它做所有的事

int max = 5000; /* why subtract one if you have to add one to use it? */
int *arr = NULL;
int i;

arr = realloc(arr, max * sizeof *arr); /* allocate generous array */
for (i = 0; i < max; i++) {
    /* ... */
}
max = 10000;
arr = realloc(arr, max * sizeof *arr); /* grow array */

max = 100;
arr = realloc(arr, max * sizeof *arr); /* shrink array */

我会动态调整数组的大小,在需要时通过每次重新分配将其大小增加一倍(如果需要,在最后缩小).
当然我事先不知道确切的所需长度
-为什么不?如果你想用5000以下的5倍填充一个数组,这是一个固定的数字,你可以算出…@Mike True,但我也有兴趣听人们在更一般的基础上做些什么。一般的答案是你分配足够的空间来满足你的上限如果您不知道条目的数量。或者您使用另一种处理效果更好的数据结构。@Flexo如何“修剪”数组中未使用的空间?我会动态调整数组大小,在需要时每次重新分配时将其大小加倍(如果需要,在最后缩小).
当然我事先不知道确切的所需长度
-为什么不?如果你想用5000以下的5倍填充一个数组,这是一个固定的数字,你可以算出…@Mike True,但我也有兴趣听人们在更一般的基础上做些什么。一般的答案是你分配足够的空间来满足你的上限如果您不知道条目的数量。或者您使用另一种处理效果更好的数据结构。@Flexo如何“修剪”数组中未使用的空间?