【问题标题】:Cannot access memory at address in C无法访问C中地址的内存
【发布时间】:2011-11-28 20:41:57
【问题描述】:

我有一个包含字符串的节点链接列表。程序从 stdin 读取字符,直到它到达一个新行,一旦它把这个字符串放入列表的一个新节点。

我已经对程序中涉及的不同步骤进行了一些调试,并且可以看到正在正确创建的节点列表。

但是,如果我单步执行代码,printf 语句似乎没有任何作用。如果我不单步执行并运行代码,我会得到:

无法访问地址 0x2e666564 处的内存

无法访问地址 0x2e666564 处的内存

无法访问地址 0x2e666564 处的内存

我的源代码是:

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

typedef struct Node {
    char *string;
    struct Node *next;
} List;

List *addNode(List *currentList, char *character)
{
    List *tempList = calloc(1, sizeof(List));
    tempList->string = strdup(character);
    tempList->next = currentList;

    return tempList;
}

void printList(List *currentList)
{
    while (currentList != NULL)
    {
        printf("%s", currentList->string);
        currentList = currentList->next;
    }
}

int main ()
{
    char currentCharacter;
    char *currentString;
    List *mainList = NULL;

    do 
    {
        currentCharacter = getchar();
        if (currentCharacter == '\n')
        {
            mainList = addNode(mainList, currentString);
            currentString[0] = '\0';
        } else {
            strcat(currentString, &currentCharacter);
        }
    } while (currentCharacter != '.');

    mainList = addNode(mainList, currentString);


    printList(mainList);

    return 0;
}

【问题讨论】:

    标签: c linked-list


    【解决方案1】:

    你在一个无效的指针上调用 strcat。

    这样的事情会起作用:

    char currentString[128];
    currentString[0] = '\0';
    

    currentCharacter 不是以 null 结尾的,因此 strcat 不起作用。请改用 strncat。

    【讨论】:

    • 他需要在某个时候将字符放在 currentString[0] 以外的某个位置。
    【解决方案2】:

    这里有几个问题。

    主要问题是您没有为 currentString 分配空间。 strcat 要求目标数组 (currentString) 中有空间。

    还有一个问题:当用户输入 '\n' 时,您没有以 null 终止要附加的字符串,因此 strdup 将无法正常工作。

    【讨论】:

      【解决方案3】:

      您的main 函数从不初始化currentString 以指向分配的内存,因此您对strcat 的调用只是开始将字符添加到'\0' 终止的字符串currentString 碰巧指向的内容。

      【讨论】:

        【解决方案4】:

        您不需要strcat。您确实需要在某个时候分配currentString。替换:

        #define BUFFER_SIZE 128
        /*
           shouldn't need more than 128 characters for a line of console
           but you can expand it later if you want
        */
        int main() {
            List *mainList = NULL;
            char *buffer = malloc(sizeof(char)*BUFFER_SIZE);
            int currentIndex = 0;
            char currentCharacter;
        
            do {
                currentCharacter = getChar();
                if (currentCharacter == '\n' || currentIndex == (BUFFER_SIZE-1))
                    buffer[currentIndex] = 0;                     //the null byte needs to be added before strdup is called
                    mainList = addNode(mainList, buffer);
                    currentIndex = 0;
                } else {
                    buffer[currentIndex++] = currentCharacter;
                }
            } while (currentCharacter != '.');
        
            mainList = addNode(mainList, buffer);
            return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 2010-11-01
          • 1970-01-01
          • 2018-09-08
          • 2013-02-18
          • 1970-01-01
          • 1970-01-01
          • 2019-04-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多