c - malloc 用于指向结构的指针

标签 c

关闭。这个问题需要details or clarity .它目前不接受答案。












想改进这个问题?通过 editing this post 添加详细信息并澄清问题.

8年前关闭。




Improve this question




这是一个程序的一部分,它接收一个包含一些 shell 命令的文件。
例如)一个 || b, a|b, ... 并以树形结构打印出结果。
make_command_stream 函数将 shell 脚本作为输入并创建一个包含命令树结构的结构。
0.command_t和command_stream_t的类型定义

typedef struct command *command_t;
typedef struct command_stream *command_stream_t;
1.结构体的定义:struct command_stream
struct command_stream{
    struct command* nodeArray[255];
    int count; 
    int front, rear;
    int max;
};
2.结构体的定义:struct command
struct command
{
  enum command_type type;

  // Exit status, or -1 if not known (e.g., because it has not exited yet).
  int status;

  // I/O redirections, or null if none.
  char *input;
  char *output;

  union
  {
    // for AND_COMMAND, SEQUENCE_COMMAND, OR_COMMAND, PIPE_COMMAND:
    struct command *command[2];

    // for SIMPLE_COMMAND:
    char **word;

    // for SUBSHELL_COMMAND:
    struct command *subshell_command;
  } u;
};
3.定义一个函数:postfixToTree
struct command** postfixToTree(char* postfix, struct command **root){
    struct nStack X;
    struct command *newNode[255] = { 0 };
    struct command *op1,*op2;
    char *p;
    p = &postfix[0];
    
//这些是变量的内存分配。我知道它看起来非常困惑和不合理,我无法正确分配内存
    struct command stackCmd[255] = { 0 };
    char stackBuf[255][255] = { 0 };
    char* pStackBuf[255] = { 0 };
    int i;
    for(i=0;i<256;i++){
        pStackBuf[i] = &stackBuf[i][0];
        stackCmd[i].u.word = &pStackBuf[i];
        X.data[i] = &stackCmd[i];
    }
   
    struct command nodeCmd[255] = { 0 };
    char nodeBuf[255][255] = { 0 };
    char* pNodeBuf[255] = { 0 };
    int j;
    for(j=0;j<256;j++){
        pNodeBuf[j] = &nodeBuf[j][0];
        nodeCmd[j].u.word = &pNodeBuf[j];
        newNode[j] = &nodeCmd[j];
    }


    emptyNodeStack(&X);
    int k = 0;
    while(*p){
        char keys[] = ";|&<>";
        
        if(isalpha(*p) || (*p ==':') || (*p == '<') || (*p == '>') || (*p == '-') || (*p == '!')){
            int i = strcspn(p, keys);
            memcpy(pNodeBuf[k],p,i);
            nodeBuf[k][i]='\0';
            pNodeBuf[k+1] = NULL;
            p = p+i;
            newNode[k]->type=SIMPLE_COMMAND;
            newNode[k]->status=-1;
            pushNode(&X,newNode[k]);
            k++;
        }
        
        
        if (*p == '|')
        {
            op1 = popNode(&X);
            op2 = popNode(&X);
            newNode[k]->type=PIPE_COMMAND;
            newNode[k]->status=-1;
            newNode[k]->u.command[0] = op2;
            newNode[k]->u.command[1] = op1;
            pushNode(&X,newNode[k]);
            k++;
            p++;
        }
    }
    *root = popNode(&X);
    return root;
}
4.函数定义:make_command_stream
command_stream_t
make_command_stream (int (*get_next_byte) (void *),
                     void *get_next_byte_argument)
{
    char c = 0;
    char* infix_string= &c;
    infix_string=(char*)checked_malloc(sizeof(char)*1000);
    char d = 0;
    char* postfix_string = &d;
    postfix_string = (char*)checked_malloc(sizeof(char)*1000);
    
    command_stream_t csPtr = (command_stream_t)checked_malloc(sizeof(struct command_stream));
    if(csPtr == NULL)
        return NULL;
    csPtr->max = 100;
    
    if(csPtr->nodeArray == NULL){
        free(csPtr);
        return NULL;
    }
    csPtr->front = 0;
    csPtr->rear = 0;
    csPtr->count = 0;
   
    int i = 0;
    c = get_next_byte(get_next_byte_argument);
    while(c!= EOF){
        unsigned int j = 0;
        while(c!=EOF && c!='\n'){
            infix_string[j] = c;
            c = get_next_byte(get_next_byte_argument);
            j++;
            if(j > sizeof(char)*1000){
                infix_string=(char*)realloc(infix_string,sizeof(char)*2000);
            }
        }
        infixToPostfix(infix_string, postfix_string);
        struct command** r;

// After executing this line, the value of csPtr->nodeArray[0] is replace to csPtr->nodeArray
        r = postfixToTree(postfix_string, &csPtr->nodeArray[i]);

    enqueue(csPtr,r);
            memset(infix_string,0,strlen(infix_string));
        memset(postfix_string,0,strlen(postfix_string));
        i++;
        while(c == '\n' || c == '\t' || c == ' '){
            c = get_next_byte(get_next_byte_argument);
        }
    }
5. 第一个问题。
我想访问结构的成员。
我上面所做的是,声明一个指向结构的指针,称为 csPtr(命令流指针)。
我为结构分配了内存。之后,我为这个结构设置了初始值。
此时,我是否可以在不创建结构对象的情况下访问结构的成员?
编译器没有提示,但我不清楚如果我不创建结构对象会发生什么。
6.第二个问题
postfixToTree 函数的目的是修改我上面提到的“命令流结构”。
具体来说,我的目标是获取树的一个节点,它是从字符串中指向结构(struct command*)的指针。我的程序当前正在执行make_command_stream 函数中的以下行。
r = postfixToTree(postfix_string, &csPtr->nodeArray[i]);
我使用了 GDB,打破了界限,并检查了值。
但是,在我执行上面的行之后,array[0] 的值被覆盖了。
这是我从 GDB 得到的:
当前线路:
6: postfix_string = 0x603640 "g++  -c  foo.c "
5: *((**r).u.word) = 0x7ffffffd7e90 "true "
3: *(csPtr->nodeArray[1]->u.word) = <error: Cannot access memory at address 0x18>
2: *(csPtr->nodeArray[0]->u.word) = 0x7ffffffd7e90 "true "
然后我执行这一行:
(gdb) n
预期的:
6: postfix_string = 0x603640 "g++  -c  foo.c "
5: *((**r).u.word) = 0x7ffffffd7e90 "g++  -c  foo.c "
3: *(csPtr->nodeArray[1]->u.word) = <error: Cannot access memory at address 0x18>
2: *(csPtr->nodeArray[0]->u.word) = 0x7ffffffd7e90 "true "
我得到了什么:
6: postfix_string = 0x603640 "g++  -c  foo.c "
5: *((**r).u.word) = 0x7ffffffd7e90 "g++  -c  foo.c "
3: *(csPtr->nodeArray[1]->u.word) = 0x7ffffffd7e90 "g++  -c  foo.c "
2: *(csPtr->nodeArray[0]->u.word) = 0x7ffffffd7e90 "g++  -c  foo.c "
为什么我的函数会覆盖数组的值?
编辑:
7.popNode的定义
struct command* popNode(struct nStack* s)
{
        struct command *ret=NULL;
        if(!isemptyNode(s))
        {
                ret= s->data[s->top];
                --s->top;
        }
        return ret;
}

最佳答案

am I allowed to access the member of the structure without creating struct object



您已经为 struct 对象分配了空间。没有进一步的步骤来创建结构对象。您可以写入此对象并从您之前写入的字段中读取。

关于第二个问题,您似乎正在覆盖 postfixToTree 内的值。 :
*root = popNode(&X);
root&csPtr->nodeArray[0]在调用代码中,所以 *rootcsPtr->nodeArray[0] ,因此它被覆盖。

在不了解整个代码的情况下,我无法说出您为什么要这样编写它。但我怀疑您正在返回一个指向本地分配内存的指针。在 postfixToTree 内你定义
struct command nodeCmd[255] = { 0 };

稍后您将指针分配给此数组的成员:
newNode[j] = &nodeCmd[j];

并将其输入您的本地数据结构X (使用我们看不到的 pushNode 函数):
pushNode(&X,newNode[k]);
popNode(&X)大概从 X 中检索到这些条目之一.但是函数中指向本地数据的指针在函数返回后无效。

关于c - malloc 用于指向结构的指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19259434/

相关文章:

c - 以下语句在 C 语言中等效吗?

c - 如何强制从 tty 输入

c - 解释 void (*signal(int signo, void *(func)(int)))(int)

c - 如何在 C 中打印居中对齐的字符串

c++ - C和C++中return 0有什么意义?

c - 使用递归扫描多个数字并计算扫描了多少个偶数(在c中)

c - Linux 夏令时通知

c - 指向数组的指针有什么意义?

c - gcc在调用前从esp中减去

c - C 中的 Char 矩阵 - 使用 Scanf 或 getchar