Segmentation fault

J

JS

I have this struct:

struct list {
char *thread;
struct list *previous;
struct list *next;
};

struct list *n;



I then have this function:


void addthread(char *t){
n = (struct list *)malloc(sizeof(struct list));
printf("%s\n",t);


strcpy(n->thread,t);

}

In my main function I give the addthread function this argument:

addthread("test");

when I run the program it prints "test" from line 2 in the addthread
function but then I gives the error:

Segmentation fault

I guess it has something to do with this statment:

strcpy(n->thread,t);

But why is that statement illegal?
 
L

lndresnick

JS said:
I have this struct:

struct list {
char *thread;
struct list *previous;
struct list *next;
};

struct list *n;



I then have this function:


void addthread(char *t){
n = (struct list *)malloc(sizeof(struct list));
printf("%s\n",t);


strcpy(n->thread,t);

}

In my main function I give the addthread function this argument:

addthread("test");

when I run the program it prints "test" from line 2 in the addthread
function but then I gives the error:

Segmentation fault

I guess it has something to do with this statment:

strcpy(n->thread,t);

But why is that statement illegal?

n->thread is a char * pointing nowhere in particular in your code, it
has
not been initialized. You can't copy bytes to the location pointed to
by it unless you are very (un)lucky. You need something like this (and
BTW you should always check that malloc doesn't return NULL, I'll leave
that to you):

size_t len = strlen(t) + 1;
n->thread = malloc(len);
memcpy(n->thread, t, len);

-David
 
J

jim1154

I guess it has something to do with this statment:
strcpy(n->thread,t);

You need to allocate memory for the string before you attempt to copy
it. Adding the following line above your strcpy line should do it:

n->thread = (char *)malloc(strlen(t) + 1);
 
D

Default User

You need to allocate memory for the string before you attempt to copy
it. Adding the following line above your strcpy line should do it:

n->thread = (char *)malloc(strlen(t) + 1);


The cast for malloc() is unnecessary in C and can mask a dangerous
problem when there is no declaration in scope for malloc().



Brian
 
J

Jason Curl

JS said:
I have this struct:

struct list {
char *thread;
struct list *previous;
struct list *next;
};

struct list *n;



I then have this function:


void addthread(char *t){
n = (struct list *)malloc(sizeof(struct list));

At this point here, the values of 'thread', 'previous' and 'next' are
all undefined.
printf("%s\n",t);


strcpy(n->thread,t);

So now you try to copy the buffer pointed to by 't' to the memory
pointed to by 'thread'. But wait - thread has not been defined.

You need to do something like:

n->thread = malloc(strlen(t)+1);
strcpy(n->thread, t);

Make sure you don't forget to free() the memory when you've finished!

free(n->thread);
free(n);

Also, you would probably want to define 'next' and 'previous':

n->previous = NULL;
n->next = NULL;

For this to work said:
}

In my main function I give the addthread function this argument:

addthread("test");

when I run the program it prints "test" from line 2 in the addthread
function but then I gives the error:

Segmentation fault

I guess it has something to do with this statment:

strcpy(n->thread,t);

You're right, you're using an undefined pointer.
But why is that statement illegal?

See above.
 
J

JS

Jason said:
At this point here, the values of 'thread', 'previous' and 'next' are
all undefined.


So now you try to copy the buffer pointed to by 't' to the memory
pointed to by 'thread'. But wait - thread has not been defined.

You need to do something like:

n->thread = malloc(strlen(t)+1);

I presume you add 1 because strlen only takes the size of a string excluded
the NULL character.
strcpy(n->thread, t);

Make sure you don't forget to free() the memory when you've finished!

free(n->thread);
free(n);


Why do I need to free n->thread and n if I later on would like to use them?
The idea is to store a whole bunch of info in a list and then it should be
possible to access that info afterwards.
 
K

Keith Thompson

JS said:
Jason Curl wrote: [...]
You need to do something like:

n->thread = malloc(strlen(t)+1);

I presume you add 1 because strlen only takes the size of a string excluded
the NULL character.
Yes.
strcpy(n->thread, t);

Make sure you don't forget to free() the memory when you've finished!

free(n->thread);
free(n);


Why do I need to free n->thread and n if I later on would like to use them?
The idea is to store a whole bunch of info in a list and then it should be
possible to access that info afterwards.

"Make sure you don't forget to free() the memory *when you've
finished*!" He meant when you're finished using them, not when you're
finished setting them up.
 
J

JS

Jason said:
At this point here, the values of 'thread', 'previous' and 'next' are
all undefined.


So now you try to copy the buffer pointed to by 't' to the memory
pointed to by 'thread'. But wait - thread has not been defined.

You need to do something like:

n->thread = malloc(strlen(t)+1);
strcpy(n->thread, t);


if the thread field in my list struct had just been:

int thread;

did I also had to do the malloc thing?
 
J

JS

Keith said:
JS said:
Jason Curl wrote: [...]
You need to do something like:

n->thread = malloc(strlen(t)+1);

I presume you add 1 because strlen only takes the size of a string
excluded the NULL character.
Yes.
strcpy(n->thread, t);

Make sure you don't forget to free() the memory when you've finished!

free(n->thread);
free(n);


Why do I need to free n->thread and n if I later on would like to use
them? The idea is to store a whole bunch of info in a list and then it
should be possible to access that info afterwards.

"Make sure you don't forget to free() the memory *when you've
finished*!" He meant when you're finished using them, not when you're
finished setting them up.

ok, thanks.
 
R

Resembelkanix

JS said:
Jason Curl wrote:





if the thread field in my list struct had just been:

int thread;

did I also had to do the malloc thing?

Not if its just a normal int.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top