5

Okay, when I run this code, I have a segmentation fault:

#include<stdio.h> 
#include<stdlib.h>
#include<string.h>
#define MAX 64

struct example {
    char *name;
};

int main()
{
    struct example *s = malloc (MAX); 
    strcpy(s->name ,"Hello World!!");
    return !printf("%s\n", s->name);
}

the terminal output:

alshamlan@alshamlan-VGN-CR520E:/tmp/interview$ make q1
cc -Wall -g    q1.c   -o q1
alshamlan@alshamlan-VGN-CR520E:/tmp/interview$ ./q1
Segmentation fault (core dumped)
alshamlan@alshamlan-VGN-CR520E:/tmp/interview$ gedit q1.c

Can someone explain what's going on?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
malsh002
  • 53
  • 1
  • 1
  • 3

4 Answers4

4

You may have allocated memory for your struct, but not for its character pointer.

You can't perform a strcpy onto memory that isn't allocated. You could say

s->name = "Hello World"; 

instead.

Alternatively, allocate memory for your char, and then perform the copying. NOTE: I in NO way endorse that the following code is good, just that it will work.

int main()
{
  struct example *s = malloc(MAX);
  s->name = malloc(MAX);
  strcpy(s->name ,"Hello World!!");
  return !printf("%s\n", s->name);
}

Edit: Here is perhaps a cleaner implementation, but I still hate C-style strings

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define KNOWN_GOOD_BUFFER_SIZE 64

typedef struct example {
  char *name;
} MyExample;

int main()
{
  MyExample *s = (MyExample*) malloc( sizeof(MyExample) );
  s->name = (char*) malloc(KNOWN_GOOD_BUFFER_SIZE);
  strcpy(s->name ,"Hello World!!");
  printf("%s\n", s->name);
  free(s->name);
  free(s);
  return 0;
}
AndyG
  • 39,700
  • 8
  • 109
  • 143
1

You are allocating memory for the struct, but the char *name is still pointing to uninitialized memory. You need to allocate memory for the char * as well. If you want it to be a string of max size 64, which you can change after creation, try this:

#include<stdio.h> 
#include<stdlib.h>
#include<string.h>
#define MAX 64

struct example {
    char *name;
};

int main()
{
    struct example *s = malloc(sizeof(struct example)); 
    s->name = malloc(MAX * sizeof(char));
    strcpy(s->name ,"Hello World!!");
    return !printf("%s\n", s->name);
}

Note I'm allocating the MAX only to the char *. The example struct only needs to be sizeof(struct example)), so there's no point making it MAX. This is a better approach, as your malloc will keep giving you the exact right size, even if you change the members of your example struct.

Baldrick
  • 11,712
  • 2
  • 31
  • 35
0

the problem is that you are allocating memory for s but not for s->name and so you are performing indirection on an uninitialized pointer which is undefined behavior. Going on the assumption that you really want to allocate space for one struct example and you want your string to be size MAX then you want the following allocations:

struct example *s = malloc (sizeof(struct example)); 
s->name = malloc(MAX*sizeof(char)) ;

Note the use of operator sizeof to determine the correct size of the data type you are allocating memory for.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
0
struct example *s = malloc (MAX); 

That line points s to a memory capable of holding 64 example structures. Each example structure only has enough memory to hold a pointer (called name).

strcpy(s->name ,"Hello World!!");

Thats not valid, as s->name isn't pointing anywhere, it needs to point to allocated memory.

You probably want:

struct example *s = malloc(sizeof(struct example)); //Allocate one example structure
s->name = malloc(MAX); //allocate 64 bytes for name 
strcpy(s->name,"Hello world!"); //This is ok now.

This is the same as:

struct example s;  //Declare example structure
s.name = malloc(MAX);  //allocate 64 bytes to name
strcpy(s.name,"Hello world!");
Myforwik
  • 3,438
  • 5
  • 35
  • 42