segmenatation fault while allocating memory

S

Sameer

Hello Group,

This is one problem in programming that is troubling me.

there is a segmentation fault just before creating memory to
a structure ..i.e, just after the "allocating memory " statement.

This happens for some inputs and not all.

What can be the reason for such fault ?

Kindly reply soon,
Sameer.


void add(struct node **q)
{
struct node *temp;
temp=*q;

if(*q==NULL)
{
*q=new node;
temp=*q;
}

else
{
cout << "allocating memory";
temp = new node;
cout << "memory allocated";
temp->next=*q;
}

}
 
A

Andreas Kahari

Hello Group,

This is one problem in programming that is troubling me.

there is a segmentation fault just before creating memory to
a structure ..i.e, just after the "allocating memory " statement.

This happens for some inputs and not all.

What can be the reason for such fault ?

Kindly reply soon,
Sameer.

#include <stdio.h>
#include said:
void add(struct node **q)
{
struct node *temp;

if (q == NULL) {
/* take care of error */
}

Remove the line above (unneeded).
if(*q==NULL)
{
*q=new node;

replace with:

*q = malloc(sizeof **q);
if (*q == NULL) {
/* take care of error */
}
temp=*q;
}

else
{
cout << "allocating memory";

replace with:

printf("Allocating memory\n");
temp = new node;

replace with:

temp = malloc(sizeof *temp);
if (temp == NULL) {
/* take care of error */
}
cout << "memory allocated";

replace with:

printf("Memory allocated\n");
temp->next=*q;
}

}


There was an awful lot of C++ -like code in there... and the C++
iostream classes doesn't flush output unless a cout object is
passed an endl or flush object. The error might therefore occur
somewhere else entierly. Switch to proper C syntax and make
sure you flush your output in all your debug output statements.
 
M

Martin Ambuhl

Sameer said:
Hello Group,

This is one problem in programming that is troubling me.

there is a segmentation fault just before creating memory to
a structure ..i.e, just after the "allocating memory " statement.

This happens for some inputs and not all.

What can be the reason for such fault ?

Kindly reply soon,
Sameer.


void add(struct node **q)
{
struct node *temp;
temp=*q;

if(*q==NULL)
{
*q=new node;
temp=*q;
}

else
{
cout << "allocating memory";
temp = new node;
cout << "memory allocated";
temp->next=*q;
}

}

For your code to be topical in comp.lang.c, it would look something like
the following (no guarantees about the correctness of the code):

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

struct node
{
struct node *next;
};

void add(struct node **q)
{
struct node *temp;
temp = *q;

if (!*q) {
printf("allocating root\n");
if ((*q = malloc(sizeof **q))) {
printf("root allocated\n");
(*q)->next = 0;
}
else {
printf("allocation failed\n");
abort();
}
}

else {
printf("allocating new node\n");
if ((temp = malloc(sizeof **q))) {
printf("node allocated\n");
temp->next = 0;
(*q)->next = temp;
}
else {
printf("allocation failed\n");
abort();
}
}

}

int main(void)
{
struct node *k = 0;
add(&k);
return 0;
}
 
A

Al Bowers

Martin said:
For your code to be topical in comp.lang.c, it would look something like
the following (no guarantees about the correctness of the code):

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

struct node
{
struct node *next;
};

void add(struct node **q)
{
struct node *temp;
temp = *q;

if (!*q) {
printf("allocating root\n");
if ((*q = malloc(sizeof **q))) {
printf("root allocated\n");
(*q)->next = 0;
}
else {
printf("allocation failed\n");
abort();
}
}

else {
printf("allocating new node\n");
if ((temp = malloc(sizeof **q))) {
printf("node allocated\n");
temp->next = 0;
(*q)->next = temp;

Oops. You need to be very careful in using this
function. It you try to link more than two nodes
you will case a break in the link and resulting
data loss and memory leak.

}
else {
printf("allocation failed\n");
abort();
}
}

Here, hopefully, is a better example.

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

struct node
{
char name[32];
struct node *next;
};

int add(struct node **q, const char *name);

int main(void)
{
struct node *k = 0;
add(&k,"George Washington");
add(&k, "Abe Lincoln");
add(&k, "George Bush");
while(k)
{
struct node *temp = k->next;
printf("The name is %s\n",k->name);
free(k);
k = temp;
}
return 0;
}

int add(struct node **q, const char *name)
{
struct node *temp;

if((temp = malloc(sizeof *temp)) == NULL) return 0;
strcpy(temp->name,name);
temp->next = *q;
*q = temp;
return 1;
}
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top