Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > segmenatation fault while allocating memory

Reply
Thread Tools

segmenatation fault while allocating memory

 
 
Sameer
Guest
Posts: n/a
 
      11-03-2003
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;
}

}




 
Reply With Quote
 
 
 
 
Christian Bau
Guest
Posts: n/a
 
      11-03-2003
In article <bo4q7g$179s5a$(E-Mail Removed)-berlin.de>,
"Sameer" <(E-Mail Removed)> wrote:

> cout << "allocating memory";



cout << "You are posting in the wrong newsgroup";
cout << "comp.lang.c++ is next door. ";
 
Reply With Quote
 
 
 
 
Andreas Kahari
Guest
Posts: n/a
 
      11-03-2003
In article <bo4q7g$179s5a$(E-Mail Removed)-berlin.de>, Sameer wrote:
> 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 <stdlib.h>

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


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

> temp=*q;


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.


--
Andreas Kähäri
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      11-03-2003
Sameer wrote:

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





--
Martin Ambuhl

 
Reply With Quote
 
Al Bowers
Guest
Posts: n/a
 
      11-03-2003


Martin Ambuhl wrote:

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

--
Al Bowers
Tampa, Fl USA
mailto: http://www.velocityreviews.com/forums/(E-Mail Removed) (remove the x to send email)
http://www.geocities.com/abowers822/

 
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
Allocating vector of strings seem to crash. Allocating array ofstrings seems to be ok . Rakesh Kumar C++ 5 12-21-2007 10:42 AM
segmenatation fault while allocating memory Sameer C++ 2 11-03-2003 09:24 AM
allocating memory for std::string? Axel C++ 1 10-27-2003 12:35 PM
basic allocating memory question soni29 C++ 6 09-05-2003 05:45 PM
Pushing memory allocating objects into a vector. hall C++ 4 08-20-2003 09:24 PM



Advertisments