3

私は C が初めてで、現在 C で Scheme インタープリターを実装しています。私は終わりに近づいていますが、まだ取り組むことができていない問題が私を悩ませています。

私は、プログラムが実行され、変更される間ずっと残る構造体への「globalEnvironment」ポインターが必要です(定数ではありません)。

/****************************************************************
 Creates the List as a pointer to the conscell structure
 */
typedef struct conscell *List;

/****************************************************************
 Creates a conscell structure, with a char pointer (symbol) and two pointers to
 * conscells (first and rest)
 */
struct conscell {
    char *symbol;
    struct conscell *first;
    struct conscell *rest;

};


List globalEnvironment;


/****************************************************************
 Function: globalVariables()
 --------------------
 This function initializes the global variables
 */
void globalVariables() {

globalEnvironment = malloc(sizeof (struct conscell));
globalEnvironment->symbol = NULL;
globalEnvironment->first = NULL;
globalEnvironment->rest = NULL;

}

ご覧のとおり、"List" はコンセル構造へのポインターです。だから私が望むのは、globalEnvironment リストがグローバルであることだけです。

問題は、そこで malloc を実行できないことです。次のことを試してみると:

List globalEnvironment = malloc(sizeof (struct conscell));

「List globalEnvironment;」の代わりに 「初期化要素は定数ではありません」というエラーが発生します

この状況に対処するために、プログラムの開始時に実行され、globalEnvironment を初期化し、メモリを割り当てる新しい関数「globalVariables」を作成しました。ただし、期待どおりに機能していません。簡単にするためにここに書いていない他の関数のセグメンテーション違反エラーが発生し続けています。

Cの構造体へのポインタ(定数ではない)を宣言する別のより簡単な方法はありますか?

誰かが助けてくれることを願っています、ありがとう

4

3 に答える 3

6

mallocグローバルデータのみを使用する必要があるときにしようとしているようです。あなたは試すことができます

struct conscell globalEnvironment;

決してfreeそうしないことを忘れないでください。

リストのセルをプッシュできるようにポインターハンドルが必要な場合:

struct conscell _globalEnvironment;
List globalEnvironment = &_globalEnvironment;

それでも、絶対にしないでfree _globalEnvironmentください。

于 2012-12-11T07:26:40.370 に答える
1

malloc()は関数であり、任意の関数への呼び出し (実行時に行われる) であるため、main()または他の関数定義内にある必要があります。

また、グローバル変数には初期化リストが必要であり、定数式である必要があります。

ISO :c99 , 6.5.2.5 Compound literals : paragraph 3rd of constraints,

3
If the compound literal occurs outside the body of a function, the initializer list shall consist of constant expressions.

したがって、関数本体の外側でポインターを呼び出すmalloc()と、そのエラーが発生します。List

于 2012-12-11T07:05:38.580 に答える
1

これはある種のリンク リストまたは同様の動的 ADT であると考えているため、malloc を使用する必要があります。

これは、適切なオブジェクト指向プログラム設計でこの機能を実装する方法です。

conscell.h

/****************************************************************
 Creates a conscell structure, with a char pointer (symbol) and two pointers to
 * conscells (first and rest)
 */
typedef struct conscell {
    char *symbol;
    struct conscell *first;
    struct conscell *rest;
} conscell_t;


void conscell_init (void);
void conscell_cleanup (void);
void conscell_add (something); // function that accesses the object

conscell.c

#include "conscell.h"
#include <stdlib.h>

static conscell_t* environment;


void conscell_init() 
{
  environment = malloc(sizeof (conscell_t));

  if(environment == NULL)
  {
    // error handling
  }

  environment->symbol = NULL;
  environment->first  = NULL;
  environment->rest   = NULL;
}

void conscell_cleanup (void)
{
  // perform all custom freeing of dynamic memory here

  free(environment);
  environment = NULL;
}

void conscell_add (something)
{
  // do something with "environment" here.
} 
于 2012-12-11T09:54:53.287 に答える