C:将令牌存储在不断增长的数组中

C: store tokens in growing array

我目前正在学习 C 并想编写一个程序来读取一行数字(从文件中),用空格分隔(例如:43 2 6 120 5 23),然后将这些数字存储在一个动态数组。 到目前为止,我的想法是将该行作为字符串读取,将其分成标记 (strtok),然后将这些标记转换为整数 (atoi) 并存储这些。 我的问题是我必须使用 malloc 分配内存,但无法告诉程序手动需要多少内存,因为它必须能够处理任何长度的行和任何大小的数字,我不知道该怎么做。我应该使用realloc

我不想让任何人为我做我的工作,只是一个简单的例子 and/or 很好地解释如何做我想做的事情会很有帮助。如果需要更多信息,我会提供给您。 我知道这里有一些关于这个主题的问题,我已经看过了,我只是很难在没有解释的情况下理解一些东西,因为我是 C 编程的新手。 当然,如果有人能给我一个可能对我有帮助的link,那就太好了。

如果您不介意连续的空格,那么您可以简单地计算它们的数量并加一个:

#include <stdio.h>

int main(void)
{
    char line[] = "43 2 6 120 5 23";

    size_t numCount = 1;
    char *ptr = line;
    while (*ptr != '[=10=]') {
        if (*ptr == ' ') {
            numCount++;
        }
        ptr++;
    }

    // malloc call, etc.

    return 0;
}

读取一行后,代码将需要扫描该行两次,一次计算数字(以便分配正确数量的内存),再次转换数字并将它们存储在数组中.

使用strtok的问题在于它会修改行,这会阻止代码使用strtok第二次扫描行。

所以解决方案是编写一个简单的 for 循环来解析行并计算数字。为此,您需要一个变量来跟踪您是在数字内部还是数字之间。这是伪代码:

state = outside
count = 0
for each character in the line
    if ( state == outside )
    {
        if ( isdigit(character) )
            count++
            state = inside
    }
    else
    {
        if ( isspace(character) )
            state = outside
    }

循环结束时,可以用count为数组分配内存,然后可以用strtokstrtol进行数字转换。请注意,对于字符既不是数字也不是空格的情况,我省略了错误检查。

这是一个使用动态内存的非常标准的应用程序。您要做的是为初始数量的整数声明内存,读取直到达到该数量,重新分配数组的两倍大,然后继续读取所有数据。

虽然将整行数据读入缓冲区然后根据需要解析缓冲区通常是最佳做法,但在这种情况下 fscanf 是为处理单个数据的读取和转换而量身定制的整数值(它会自动消耗分隔值的空格)。

(下面使用 calloc 将所有元素初始化为 0malloc 在这个例子中很好,但是初始化可以防止在更复杂的情况下从未初始化的元素中无意读取)

这是一个简短的例子:

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

#define MAXI 64

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

    int *array = NULL;
    size_t idx = 0, max_idx = 0;
    size_t arraysize = MAXI;
    FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;

    if (!fp) {
        fprintf (stderr, "error: file open failed. '%s'\n", argc > 1 ? argv[1] : "stdin");
        return 1;
    }

    /* allocate initial array */
    array = calloc (MAXI, sizeof *array);

    /* read values from file */
    while (fscanf (fp, "%d", &array[idx]) == 1) {
        idx++;

        /* realloc if necessary */
        if (idx == arraysize) {
            int *tmp = realloc (array, arraysize * sizeof *array * 2);
            if (!tmp) {
                fprintf (stderr, "error: realloc - virtual memory exhausted.\n");
                return 1;
            }
            array = tmp;
            memset (array + arraysize, 0, arraysize);  /* zero new memory */
            arraysize *= 2;
        }
    }

    /* close file */
    if (fp != stdin) fclose (fp);

    max_idx = idx;

    /* print array */
    for (idx = 0; idx < max_idx; idx++)
        printf (" array[%3zu] : %d\n", idx, array[idx]);

    free (array);   /* free memory */

    return 0;
}

示例数据

$ cat dat/100intspace.txt
27086 29317 32736 3356 12059 13921 9388 25672 19828 25390 -1190 25857 ...

输出

$ ./bin/array_dyn_read_int dat/100intspace.txt
 array[  0] : 27086
 array[  1] : 29317
 array[  2] : 32736
 array[  3] : 3356
 array[  4] : 12059
 array[  5] : 13921
 array[  6] : 9388
 array[  7] : 25672
 array[  8] : 19828
 array[  9] : 25390
 array[ 10] : -1190
 array[ 11] : 25857
 ...

验证您的内存使用

无论何时动态分配内存,您都有责任 (1) 跟踪分配的内容; (2) 保留一个指向起始地址的指针(以便稍后释放它); (3) 当不再需要时释放内存。 valgrind 或类似的内存错误检查器使用简单,应该用于验证您的内存使用情况:

$ valgrind ./bin/array_dyn_read_int dat/100intspace.txt
==7348== Memcheck, a memory error detector
==7348== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==7348== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==7348== Command: ./bin/array_dyn_read_int dat/100intspace.txt
==7348==
 array[  0] : 27086
 array[  1] : 29317
 array[  2] : 32736
 array[  3] : 3356
 array[  4] : 12059
 array[  5] : 13921
 array[  6] : 9388
 array[  7] : 25672
 array[  8] : 19828
 array[  9] : 25390
 array[ 10] : -1190
 array[ 11] : 25857
 ....
==7348==
==7348== HEAP SUMMARY:
==7348==     in use at exit: 0 bytes in 0 blocks
==7348==   total heap usage: 3 allocs, 3 frees, 1,336 bytes allocated
==7348==
==7348== All heap blocks were freed -- no leaks are possible
==7348==
==7348== For counts of detected and suppressed errors, rerun with: -v
==7348== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)

如果您有任何问题,请告诉我。

您可以编写一个单独的函数,从字符串中提取数字并将它们存储在动态分配的数组中,而无需使用 realloc。 要在分配数组之前计算字符串中的数字,您可以使用标准 C 函数 sscanf.

这是一个演示程序

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

size_t get_data( const char *s, int **a )
{    
    int x;
    size_t n;
    int m;

    *a = NULL;
    n = 0;

    for ( const char *p = s; sscanf( p, "%d%n", &x, &m ) == 1; p += m ) ++n;

    if ( n && ( *a = malloc( n * sizeof( int ) ) ) )
    {
        const char *p = s;
        for ( size_t i = 0; i < n; i++, p += m ) sscanf( p, "%d%n", *a + i, &m );
    }

    return n;
}    

int main( void )
{
    char s[] = " 43 2 6 120 5 23";
    int *a;
    size_t n = get_data( s, &a );

    if ( n )
    {
        for ( size_t i = 0; i < n; i++ ) printf( "%d ", a[i] );
        printf( "\n" );
    }        

    free( a );
}

程序输出为

43 2 6 120 5 23