[英]C struct memory allocation
這里是樹數據結構的相關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;
float * info = (float *) calloc (MAX_DIMENSIONS, sizeof(float));
拋出編譯器錯誤:
kdtree.h:31:22: error: expected ':', ',', ';', '}' or '__attribute__' before '=' token
float * info = (float *) calloc (MAX_DIMENSIONS, sizeof(float));
我能夠在結構之外而不是在結構內部進行動態分配嗎? 如何在結構內部進行預分配。
這可能有效,但是沒有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.