Tarkan Batar
Tarkan Batar

Reputation: 91

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

Hi guys I'm learning C programming. 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.

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;
}

Upvotes: 0

Views: 105

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 311038

The program initially is designed incorrectly and has undefined behavior..

For example the data member data of the node root was not initialized. So its output in the function nodePrint results in undefined behavior. Moreover the function itself is incorrect.

Neither function uses its parameter 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). 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.

Parameters of the function main are not used in the program. So the function should be declared like

int main( void )

Upvotes: 2

user2736738
user2736738

Reputation: 30926

data is an unintialized pointer variable. 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.

  2. Allocate dynamically the memory.

In case of 1.

struct node{

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

In case of 2:

Initially make the pointers point to 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*. 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.

Upvotes: 2

Related Questions