具有未知大小的结构数组的结构 [英] Struct with array of structs of unknown size

查看:138
本文介绍了具有未知大小的结构数组的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整天都在努力把头缠住……

I've been trying to wrap my head around this the whole day...

基本上,我有一个名为State的结构,该结构具有名称,另一个名为StateMachine的结构具有名称,状态数组和添加的状态总数:

Basically, I have a struct called State that has a name and another one called StateMachine with a name, an array of states and total number of states added:

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

typedef struct State {
  const char * name;

} State;

typedef struct StateMachine {
  const char * name;

  int total_states;
  State ** states;

} StateMachine;

StateMachine * create_state_machine(const char* name) {
  StateMachine * temp;

  temp = malloc(sizeof(struct StateMachine));

  if (temp == NULL) {
    exit(127);
  }

  temp->name = name;
  temp->total_states = 0;

  temp->states = malloc(sizeof(struct State));
  return temp;
}

void destroy_state_machine(StateMachine* state_machine) {
  free(state_machine);
}

State * add_state(StateMachine* state_machine, const char* name) {
  State * temp;

  temp = malloc(sizeof(struct State));

  if (temp == NULL) {
    exit(127);
  }

  temp->name = name;

  state_machine->states[state_machine->total_states]= temp;
  state_machine->total_states++;

  return temp;
}

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

  StateMachine * state_machine;

  State * init;
  State * foo;
  State * bar;

  state_machine = create_state_machine("My State Machine");

  init = add_state(state_machine, "Init");
  foo  = add_state(state_machine, "Foo");
  bar  = add_state(state_machine, "Bar");

  int i = 0;

  for(i; i< state_machine->total_states; i++) {
    printf("--> [%d] state: %s\n", i, state_machine->states[i]->name);
  }

}

由于某种原因(阅读低C-fu/ruby​​/python/php年),我无法表达以下事实:状态是状态数组.上面的代码显示:

For some reason (read low C-fu / years of ruby/python/php) I'm unable to express the fact that states is an Array of State(s). The above code prints:

--> [0] state: ~
--> [1] state: Foo
--> [2] state: Bar

添加第一个状态会发生什么?

What happened with the first state added?

如果我在添加的第一个状态上分配状态数组(例如,state_machine = malloc(sizeof(temp));那么我将获得第一个值,而不是第二个值.

If I malloc the states array on the first state added (e.g. state_machine = malloc(sizeof(temp)); then I get the first value but not the second.

有什么建议吗?

这是一个C问题.我正在使用gcc 4.2.1编译示例.

This is a C question. I'm using gcc 4.2.1 to compile the sample.

推荐答案

您似乎没有为计算机中第一个状态之后的状态分配空间.

It looks like you're not allocating space for your states in the machine past the first one.

StateMachine * create_state_machine(const char* name) {
  StateMachine * temp;

  temp = malloc(sizeof(struct StateMachine));

  if (temp == NULL) {
    exit(127);
  }

  temp->name = name;
  temp->total_states = 0;

  temp->states = malloc(sizeof(struct State)); // This bit here only allocates space for 1.
  return temp;
}

您最好将固定大小的状态数组放入状态机结构中.如果那还不行,那么您将不得不重新分配并移动整个集合,或者分配大块并跟踪当前长度,或者创建一个链表.

You're probably better off putting an array of states of fixed size in the state machine struct. If that's not okay, you'll have to realloc and move the whole set around or allocate chunks and keep track of the current length, or make a linked list.

偶然地,init,foo和bar永远不会被使用.

Incidentally, init, foo, and bar never get used.

我的建议如下:

#define MAX_STATES 128 // Pick something sensible.
typedef struct StateMachine {
  const char * name;
  int total_states;
  State *states[MAX_STATES];
} StateMachine;

这篇关于具有未知大小的结构数组的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆