简体   繁体   English

C编程EXC_BAD_ACCESS(代码:1,地址= 0x0)错误

[英]C Programming EXC_BAD_ACCESS (code:1 ,address=0x0) Error

Hi guys I'm learning C programming. 大家好,我正在学习C编程。 I wanted to write some codes for learning linked list topic but there is a problem. 我想写一些代码来学习链表主题,但是有一个问题。 This code about creating linked list with 5 nodes, writing something into 3rd node, printing them to console. 这段代码创建了具有5个节点的链表,将一些内容写入第3个节点,并将它们打印到控制台。

Here is all of my codes: 这是我所有的代码:

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

struct node{    
     char *data;
     struct node *next;
};

typedef struct node node;
node *root;


void nodeAdd(node *n, int x)
{    
  node *iter;
  iter=root;

  for(int i=0;i<x;i++)
  {
    iter->next = (node*)malloc(sizeof(node));
    iter->next->data="0";
    iter->next->next=NULL;

    iter=iter->next;
    printf("Node created.\n");
  }
}

void nodeWrite(node *n,char *string,int x)
{
  node *temp;
  temp=root;

  for(int k=0;k<x;k++)
  {
    temp=temp->next;
  }
  strcpy(temp->data,string);  //HERE IS ERROR
  printf("\n");
  printf("%s \n",temp->data);
  printf("Node writed.");
} 

void nodePrint(node *n)
{
  while(root->next!=NULL)
    printf("%s\n",root->data);
  root=root->next;
}

int main(int argc, const char * argv[])
{
  root = (node*)malloc(sizeof(node));

  nodeAdd(root,5);
  nodeWrite(root,"WTF", 3);
  nodePrint(root);

  return 0;
}

data is an unintialized pointer variable. data是未初始化的指针变量。 Initialize with the address of a valid memory that you allocate. 用您分配的有效内存的地址初始化。 That will solve the problem. 这样可以解决问题。 Now you have udnefined behavior. 现在,您的行为已无法定义。

What you can possibly do is 您可能会做的是

  1. Use char array instead of using pointer. 使用char array而不是使用指针。

  2. Allocate dynamically the memory. 动态分配内存。

In case of 1. 如果是1。

struct node{

     char data[MAXSTRINGLEN];
     struct node *next;
};

In case of 2: 如果是2:

Initially make the pointers point to NULL . 最初使指针指向NULL So now you can allocate to it like this 所以现在您可以像这样分配它

temp->data = malloc(sizeof *temp->data*MAXSTRINGLEN);
if( temp->data == NULL)
{
    fprintf(stderr,"Error in malloc");
    exit(1);
}

Just one point, free the allocated memory when you are done working with it. 只需一点,在完成分配的内存后就释放它。

Use of the global variable here is not really required here. 这里实际上并不需要使用全局变量。 You can always pass return pointers from memory and assign it to the struct node* . 您始终可以从内存传递返回指针,并将其分配给struct node* Or yes you can use double pointers. 或者是的,您可以使用双指针。 Use of global variable is not needed here. 这里不需要使用全局变量。

Clean up code, that are redundant and not required. 清理多余且不需要的代码。 That makes things readable and less confusing. 这使事情变得易读且不那么混乱。

The program initially is designed incorrectly and has undefined behavior.. 该程序最初设计不正确,并且具有未定义的行为。

For example the data member data of the node root was not initialized. 例如,节点root的数据成员data未初始化。 So its output in the function nodePrint results in undefined behavior. 因此,它在函数nodePrint输出nodePrint导致未定义的行为。 Moreover the function itself is incorrect. 此外,功能本身是不正确的。

Neither function uses its parameter node *n . 两个函数都不使用其参数node *n

In this statement 在此声明中

strcpy(temp->data,string);  

there is an attempt to change the string literal pointed to by the data member temp->data provided that the data member was initialized (as it was pointed above the data member is not initialized for the node root ). 如果已初始化数据成员,则尝试更改数据成员temp-> data指向的字符串文字(如上面指出的那样,未为节点root初始化数据成员)。 You may not change a string literal. 您不能更改字符串文字。 Any attempt to modify a string literal leads to undefined behavior. 尝试修改字符串文字会导致未定义的行为。

There is no need to declare the node root as a global variable. 无需将节点root声明为全局变量。

Parameters of the function main are not used in the program. 函数main参数未在程序中使用。 So the function should be declared like 所以函数应该像

int main( void )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM