Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > beginners problem

Reply
Thread Tools

beginners problem

 
 
Lars
Guest
Posts: n/a
 
      02-11-2008
Hi all
If this is the wrong list for beginners trouble I apologize.
please refer to me the correct list in such case.

I am trying to create a simple linked list but I keep getting segmentation
errors and I dont know why. I realize that I do not free the allocated
memory but the segfault happpens (it seems) with the insert function.
Can any one explain ?

Regards Lars

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

typedef struct node {
* struct node * next;
* int data;
} Node;


Node * alloc_node(int n);

void insert(Node * head, int n);

int main(){
* Node * nod = NULL;
* insert(nod,4);
* printf("%d\n",nod->data);
* printf("%10p\n", nod);
* printf("%10p\n", nod->next);
* return 0;
}

Node * alloc_node(int n){
* Node * tmp;
* tmp = (Node *) malloc(sizeof(Node));
* tmp->data = n;
* tmp->next = NULL;
* return tmp;
}

void insert(Node * head, int n) {
* Node * tmp;
* if (head==NULL) {
* * head = alloc_node(n);
* }
* else { *
* * tmp = alloc_node(n);
* * head->next = tmp;
* }
}
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-11-2008
Lars wrote:
> Hi all
> If this is the wrong list for beginners trouble I apologize.
> please refer to me the correct list in such case.
>
> I am trying to create a simple linked list but I keep getting segmentation
> errors and I dont know why. I realize that I do not free the allocated
> memory but the segfault happpens (it seems) with the insert function.
> Can any one explain ?
>
> Regards Lars
>
> #include <stdio.h>
> #include <stdlib.h>
>
> typedef struct node {
> struct node * next;
> int data;
> } Node;
>
>
> Node * alloc_node(int n);
>
> void insert(Node * head, int n);
>
> int main(){
> Node * nod = NULL;
> insert(nod,4);
> printf("%d\n",nod->data);


There's your problem, nod is NULL.

> Node * alloc_node(int n){
> Node * tmp;
> tmp = (Node *) malloc(sizeof(Node));


You don't have to (and shouldn't) cast the return value of malloc.

tmp = malloc(sizeof *tmp);

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Lars
Guest
Posts: n/a
 
      02-11-2008
Ian Collins wrote:

> Lars wrote:
>> Hi all
>> If this is the wrong list for beginners trouble I apologize.
>> please refer to me the correct list in such case.
>>
>> I am trying to create a simple linked list but I keep getting
>> segmentation errors and I dont know why. I realize that I do not free the
>> allocated memory but the segfault happpens (it seems) with the insert
>> function. Can any one explain ?
>>
>> Regards Lars
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> typedef struct node {
>> struct node * next;
>> int data;
>> } Node;
>>
>>
>> Node * alloc_node(int n);
>>
>> void insert(Node * head, int n);
>>
>> int main(){
>> Node * nod = NULL;
>> insert(nod,4);
>> printf("%d\n",nod->data);

>
> There's your problem, nod is NULL.
>
>> Node * alloc_node(int n){
>> Node * tmp;
>> tmp = (Node *) malloc(sizeof(Node));

>
> You don't have to (and shouldn't) cast the return value of malloc.
>
> tmp = malloc(sizeof *tmp);
>


Thanks for your reply. But is this not handled by the the insert functio
through this ?
if (head==NULL) {
* * head = alloc_node(n);
* }

Regards
Lars
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-11-2008
Lars wrote:
> Ian Collins wrote:
>>>
>>> Node * alloc_node(int n);
>>>
>>> void insert(Node * head, int n);
>>>
>>> int main(){
>>> Node * nod = NULL;
>>> insert(nod,4);
>>> printf("%d\n",nod->data);

>> There's your problem, nod is NULL.
>>

>
> Thanks for your reply. But is this not handled by the the insert functio
> through this ?
> if (head==NULL) {
> head = alloc_node(n);
> }
>

No, ant changes to head are local to the function, heat is a pointer, if
you wanted the change to be visible to the caller, it would have to be a
pointer to a pointer.

void insert(Node** head, int n);

--
Ian Collins.
 
Reply With Quote
 
Lars
Guest
Posts: n/a
 
      02-11-2008
Ian Collins wrote:

> Lars wrote:
>> Ian Collins wrote:
>>>>
>>>> Node * alloc_node(int n);
>>>>
>>>> void insert(Node * head, int n);
>>>>
>>>> int main(){
>>>> Node * nod = NULL;
>>>> insert(nod,4);
>>>> printf("%d\n",nod->data);
>>> There's your problem, nod is NULL.
>>>

>>
>> Thanks for your reply. But is this not handled by the the insert functio
>> through this ?
>> if (head==NULL) {
>> head = alloc_node(n);
>> }
>>

> No, ant changes to head are local to the function, heat is a pointer, if
> you wanted the change to be visible to the caller, it would have to be a
> pointer to a pointer.
>
> void insert(Node** head, int n);
>

Thats it!!!
I completely overlooked that.
Thank you very much.
Best regards
Lars

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      02-11-2008
Lars <(E-Mail Removed)> wrote:

> If this is the wrong list for beginners trouble I apologize.


This is not a list, it's a newsgroup. And if you post questions about
ISO C, not about, oh, I dunno, M$VC#++4.6 or Ganuck 4.77.35.88.55-alpha,
you should be fine here whether you're a beginner or not. Your question
is perfectly on-topic. However...

> #include <stdio.h>
> #include <stdlib.h>
>
> typedef struct node {
> * struct node * next;
> * int data;


....you might want to check what is inserting these weird characters into
your code, and...

> int main(){
> * Node * nod = NULL;
> * insert(nod,4);


....you might want to read the FAQ, in particular question 4.8:
<http://c-faq.com/ptrs/passptrinit.html>. The rest of that section, and
the rest of the FAQ, is instructive, too.

Richard
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      02-11-2008
Lars wrote:
> Hi all
> If this is the wrong list for beginners trouble I apologize.
> please refer to me the correct list in such case.
>
> I am trying to create a simple linked list but I keep getting segmentation
> errors and I dont know why. I realize that I do not free the allocated
> memory but the segfault happpens (it seems) with the insert function.
> Can any one explain ?


Below is a modified copy of your program. Not everything that might be
improved has been noted, but enough to suggest where your main problems
might lie apart from not handling allocation failure and not freeing
allocated memory. Consider especially the lines marked with /* $ */ and
think about what is different from your code and why.

There are several questions in the FAQ that are relevant to your code.
You might want to start with <http://c-faq.com/ptrs/passptrinit.html>,
"Question 4.8 Q: I have a function which accepts, and is supposed to
initialize, a pointer:"


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

typedef struct node
{
struct node *next;
int data;
} Node;


Node *alloc_node(int n);

void insert(Node ** head, int n); /* $ */

int main()
{
Node *nod = NULL;
insert(&nod, 4); /* $ */
printf("%d\n", nod->data);
printf("%10p\n", (void *) nod); /* $ */
printf("%10p\n", (void *) nod->next); /* $ */
return 0;
}

Node *alloc_node(int n)
{
Node *tmp;
tmp = malloc(sizeof *tmp);
if (tmp) {
tmp->data = n;
tmp->next = NULL;
}
return tmp;
}

void insert(Node ** head /* $ */ , int n)
{
Node *tmp;
if (*head == NULL) { /* $ */
*head = alloc_node(n); /* $ */
}

else {
tmp = alloc_node(n);
(*head)->next = tmp; /* $ */
}
}

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      02-11-2008
Richard Bos wrote:
> Lars <(E-Mail Removed)> wrote:
>
>> If this is the wrong list for beginners trouble I apologize.

>
> This is not a list, it's a newsgroup. And if you post questions about
> ISO C, not about, oh, I dunno, M$VC#++4.6 or Ganuck 4.77.35.88.55-alpha,
> you should be fine here whether you're a beginner or not. Your question
> is perfectly on-topic. However...
>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> typedef struct node {
>> struct node * next;
>> int data;

>
> ....you might want to check what is inserting these weird characters into
> your code, and...
>

Your client? They don't appear to be in the message source.

--
Ian Collins.
 
Reply With Quote
 
Lars
Guest
Posts: n/a
 
      02-11-2008
Thanks for the pointers
Regards
Lars

Martin Ambuhl wrote:

> Lars wrote:
>> Hi all
>> If this is the wrong list for beginners trouble I apologize.
>> please refer to me the correct list in such case.
>>
>> I am trying to create a simple linked list but I keep getting
>> segmentation errors and I dont know why. I realize that I do not free the
>> allocated memory but the segfault happpens (it seems) with the insert
>> function. Can any one explain ?

>
> Below is a modified copy of your program. Not everything that might be
> improved has been noted, but enough to suggest where your main problems
> might lie apart from not handling allocation failure and not freeing
> allocated memory. Consider especially the lines marked with /* $ */ and
> think about what is different from your code and why.
>
> There are several questions in the FAQ that are relevant to your code.
> You might want to start with <http://c-faq.com/ptrs/passptrinit.html>,
> "Question 4.8 Q: I have a function which accepts, and is supposed to
> initialize, a pointer:"
>
>
> #include <stdio.h>
> #include <stdlib.h>
>
> typedef struct node
> {
> struct node *next;
> int data;
> } Node;
>
>
> Node *alloc_node(int n);
>
> void insert(Node ** head, int n); /* $ */
>
> int main()
> {
> Node *nod = NULL;
> insert(&nod, 4); /* $ */
> printf("%d\n", nod->data);
> printf("%10p\n", (void *) nod); /* $ */
> printf("%10p\n", (void *) nod->next); /* $ */
> return 0;
> }
>
> Node *alloc_node(int n)
> {
> Node *tmp;
> tmp = malloc(sizeof *tmp);
> if (tmp) {
> tmp->data = n;
> tmp->next = NULL;
> }
> return tmp;
> }
>
> void insert(Node ** head /* $ */ , int n)
> {
> Node *tmp;
> if (*head == NULL) { /* $ */
> *head = alloc_node(n); /* $ */
> }
>
> else {
> tmp = alloc_node(n);
> (*head)->next = tmp; /* $ */
> }
> }


 
Reply With Quote
 
Randy Howard
Guest
Posts: n/a
 
      02-11-2008
On Mon, 11 Feb 2008 02:47:11 -0600, Ian Collins wrote
(in article <(E-Mail Removed)>):

> Richard Bos wrote:
>> Lars <(E-Mail Removed)> wrote:
>>
>>> If this is the wrong list for beginners trouble I apologize.

>>
>> This is not a list, it's a newsgroup. And if you post questions about
>> ISO C, not about, oh, I dunno, M$VC#++4.6 or Ganuck 4.77.35.88.55-alpha,
>> you should be fine here whether you're a beginner or not. Your question
>> is perfectly on-topic. However...
>>
>>> #include <stdio.h>
>>> #include <stdlib.h>
>>>
>>> typedef struct node {
>>> struct node * next;
>>> int data;

>>
>> ....you might want to check what is inserting these weird characters into
>> your code, and...
>>

> Your client? They don't appear to be in the message source.


I saw them as well. I'm pretty sure it's some UTF/Unicode weirdness
with tab expansion or perhaps newline encoding somewhere along the way,
and we're both using different clients, on different operating systems.
Being that the OP is apparently in Denmark, it's a safe bet that it
something along those lines.


--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw





 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginners Problem - Static reference to non-static method William Colls Java 4 03-20-2012 06:28 PM
wxPython beginners problem Ivan Reborin Python 5 08-16-2008 07:30 PM
Beginners Query - Simple counter problem David Barr Python 9 09-07-2007 04:52 AM
how to use stoi? which *.h to use? trivial beginners problem... Zahpod C Programming 4 04-21-2006 06:58 PM
Beginners Guides: Website Hosting With Apache Silverstrand Front Page News 0 10-24-2005 01:44 PM



Advertisments