在 main 之外分配内存的问题

Problem to allocate memory outside the main

作为 C 的初学者,我学会了一种用 struct{} 分配内存的奇特方法来替换 ch1Buffer = calloc(u32Size, sizeof *ch1Buffer); 之类的东西,并将它们放在 int main{} 之外以提高性能计算速度。 但是,我的变量 x 出现错误:This declaration has no storage class or type specifier,您可以在代码一侧看到注释。我应该声明变量 x 还是?

这是我的示例代码:

#include <stdio.h>

// New way for memory allocation 
struct {
    float ch1Buffer;
    double ch2Buffer;
    double ch2newBuffer;
} *x;
x = calloc(10, sizeof * x); // The error happened on the left side "x": This declaration has no storage class or type specifier

int main()
{
    int i,n;
    const int u32Size = 10;
    float* ch1Buffer = NULL;
    double* ch2Buffer = NULL;
    double* ch2newBuffer=NULL;
    
    
    int pBuffer[] = { 10,2,10,2,10,5,10,5,10,2 };
    int* pi16Buffer = pBuffer;

    // Old way for memory allocation
    //ch1Buffer = (float*)calloc(u32Size, sizeof* ch1Buffer);
    //ch2Buffer = (double*)calloc(u32Size, sizeof* ch2Buffer);
    //ch2newBuffer = (double*)calloc(u32Size, sizeof * ch2Buffer);

    

    // De-interveal the data to ch1Buffer and ch2Buffer
    for (i = 0; i < u32Size/2; i++)
    {
        ch1Buffer[i] += pi16Buffer[i * 2];
        ch2Buffer[i] += pi16Buffer[i * 2 + 1];
    }

    // Use memcpy to pick out the data we are interested
    memcpy(ch2newBuffer, &ch2Buffer[2], 2 * sizeof(ch2Buffer[0]));

    for (i = 0; i < 2; i++)
    {
        printf("a[%d] = %f\n", i, *ch2newBuffer);
        ch2newBuffer++;
    }


    free(ch1Buffer);
    free(ch2Buffer);
    return 0;
}

您不能在 main 或任何其他函数之外执行 malloccalloc。如果你需要它作为全局变量,你可以在代码的开头声明它,但是你需要在 main 中分配内存。

typedef struct mystruct {
  float ch1Buffer;
  double ch2Buffer;
  double ch2newBuffer;
}mystruct;

mystruct* x;

int main (void) {
    x = calloc(10, sizeof(mystruct)); // array of your structs
    if (!x) { // always check calloc return value
        perror("calloc");
        exit(EXIT_FAILURE);
    }

    /* do stuff */

    return 0;
}

此外,我建议为您的结构提供不言自明的名称,以便更好地理解它所代表的含义。

这为您提供了使用每个成员的组件的示例。与上面的答案类似,指针被定义为全局指针,并在 main() 中分配。这不是一个很好的实践,仅供演示。

#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
struct xxx {
    float f1;
    double d2;
    double d3;
};
typedef struct xxx mytype;
mytype *x;
void print_mytype()
{
    int i;
    for (i=0; i<SIZE; i++) printf("x[%d] = %f, %lf, %lf\n", i, x[i].f1, x[i].d2, x[i].d3);
    return;
}
void setdata()
{
    int i;
    for (i=0; i<SIZE; i++)
    {
      x[i].f1 = rand()/(float)RAND_MAX;
      x[i].d2 = rand()/(double)RAND_MAX;
      x[i].d3 = rand()/(double)RAND_MAX;
    }
}
int main()
{
    x = calloc(SIZE, sizeof(mytype));
    setdata();
    print_mytype();
    free(x);
    return 0;
}

post编辑的代码无法编译!

以下是编译器对代码的描述:

gcc -ggdb3 -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled1.c" -o "untitled1.o" 
untitled1.c:9:1: warning: data definition has no type or storage class
    9 | x = calloc(10, sizeof * x); // The error happened on the left side "x": This declaration has no storage class or type specifier
      | ^
      
untitled1.c:9:1: warning: type defaults to ‘int’ in declaration of ‘x’ [-Wimplicit-int]

untitled1.c:9:1: error: conflicting types for ‘x’

untitled1.c:8:4: note: previous declaration of ‘x’ was here
    8 | } *x;
      |    ^
      
untitled1.c:9:5: warning: implicit declaration of function ‘calloc’ [-Wimplicit-function-declaration]
    9 | x = calloc(10, sizeof * x); // The error happened on the left side "x": This declaration has no storage class or type specifier
      |     ^~~~~~
      
untitled1.c:9:5: warning: incompatible implicit declaration of built-in function ‘calloc’

untitled1.c:2:1: note: include ‘<stdlib.h>’ or provide a declaration of ‘calloc’
    1 | #include <stdio.h>
  +++ |+#include <stdlib.h>
    2 |
    
untitled1.c:9:23: error: invalid type argument of unary ‘*’ (have ‘int’)
    9 | x = calloc(10, sizeof * x); // The error happened on the left side "x": This declaration has no storage class or type specifier
      |                       ^~~
      
untitled1.c: In function ‘main’:

untitled1.c:33:22: warning: conversion from ‘int’ to ‘float’ may change value [-Wconversion]
   33 |         ch1Buffer[i] += pi16Buffer[i * 2];
      |                      ^~
      
untitled1.c:38:5: warning: implicit declaration of function ‘memcpy’ [-Wimplicit-function-declaration]
   38 |     memcpy(ch2newBuffer, &ch2Buffer[2], 2 * sizeof(ch2Buffer[0]));
      |     ^~~~~~
      
untitled1.c:38:5: warning: incompatible implicit declaration of built-in function ‘memcpy’

untitled1.c:2:1: note: include ‘<string.h>’ or provide a declaration of ‘memcpy’
    1 | #include <stdio.h>
  +++ |+#include <string.h>
    2 |
    
untitled1.c:47:5: warning: implicit declaration of function ‘free’ [-Wimplicit-function-declaration]
   47 |     free(ch1Buffer);
      |     ^~~~
      
untitled1.c:47:5: warning: incompatible implicit declaration of built-in function ‘free’

untitled1.c:47:5: note: include ‘<stdlib.h>’ or provide a declaration of ‘free’

untitled1.c:13:11: warning: unused variable ‘n’ [-Wunused-variable]
   13 |     int i,n;
      |           ^
      
Compilation failed.

建议您先解决代码中的那些问题,然后再向我们询问您无法解决的任何问题。

如错误消息所述:

  1. 添加语句:

    #include

  2. 添加语句:

    #include

如果您的代码 post 清楚地表明您没有为自己解决问题付出任何努力,我们就不太可能帮助您。