简体   繁体   English

C 结构 memory 分配

[英]C struct memory allocation

Here is relevant header of tree data structure:这里是树数据结构的相关header:

#include <stdlib.h>
#ifndef KDTREE_H
#define KDTREE_H
#define DEFAULT_NUMBER_OF_DIMENSIONS 1
#define KD_TREE_HEAP_SIZE 100
#define MAX_DIMENSIONS 32
//References: https://www.geeksforgeeks.org/k-dimensional-tree/
//References:  https://www.cs.cmu.edu/~ckingsf/bioinfo-lectures/kdtrees.pdf
//References:https://iq.opengenus.org/k-dimensional-tree/

  /*
     * Representation of a kd tree
     */
    typedef struct tree_
    {
        struct tree *left; 
        struct tree *right; 

        float * info =  (float *) calloc (MAX_DIMENSIONS, sizeof(float));
        float distance_to_neighbor;
    } tree;

The float * info = (float *) calloc (MAX_DIMENSIONS, sizeof(float)); float * info = (float *) calloc (MAX_DIMENSIONS, sizeof(float)); throws compiler error:抛出编译器错误:

kdtree.h:31:22: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token
         float * info =  (float *) calloc (MAX_DIMENSIONS, sizeof(float));

Im able to do dynamic allocation outside of a struct but not inside of a struct?我能够在结构之外而不是在结构内部进行动态分配吗? How can pre-allocate inside of a struct.如何在结构内部进行预分配。

This might work, however without the calloc() or whatever:这可能有效,但是没有calloc()或其他任何东西:

typedef struct tree_
{
    struct tree *left; 
    struct tree *right; 

    float info[MAX_DIMENSIONS];
    float distance_to_neighbor;
} tree;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM