Re: Segmentation fault!



"Rod Pemberton" <do_not_have@xxxxxxxxxxxxxxxxxxx> wrote in message
news:43ee3586$1@xxxxxxxxxxxxxxx

"Vladimir S. Oka" <novine@xxxxxxxxxxxxxxx> wrote in message
news:dskqvf$bc1$1@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Paminu wrote:

I have a wierd problem.

In my main function I print "test" as the first thing. But if I run
the call to node_alloc AFTER the printf call I get a segmentation
fault and test is not printed!

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

typedef struct _node_t {
int num_kids;
void *content;
struct _node_t **kids; // Makes a pointer to a pointer of node_ts.
} node_t;

*** kids is an array of pointers to _node_t, correct? Wouldn't kids be
better defined like so:
struct _node_t *(kids[]);

I think those parenthesis are not necessary, but i would stick to: struct
_node_t **kids;

node_t *node_alloc(void *content, int num)
{
node_t *parent;
parent = malloc(sizeof(node_t));

You should test whether malloc() returns NULL.

parent->content = content;
parent->num_kids = num;
node_t *new_kids[num];
int i;

*** He's changing the type of content from 'const char *' to 'void *'.
Also, if he doesn't allocate space for content, he'll continue to get
segmentation faults.

That (may) happen later on his code:
node_alloc("Root",4);

I think string litterals are not of type "const char[]" but of "char[]". The
OP may want parent->content to point to some "elsewhere" malloc'd memory,
what would be the problem with that? I think it is also alright if it
contains the address of a string litteral. Furthermore, the OP may want
parent->content to point to different types of data, not just char.

You're missing this line:

parent->kids = new_kids;

But then, you'd really want:

parent->kids = malloc(num * sizeof (node_t *));
if (parent->kids) ...

As `new_kids` will disappear after you exit the function.


// Initialize children to NULL.
for (i = 0; i < num; i++)
{
parent->kids[i]=NULL;

}
return parent;
}


int main(void)
{

printf("test");

If you don't terminate printf() with \n you may not get anything out.

node_t *root = node_alloc("Root",4);
return 0;
}


I think his program should look more like this:

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

typedef struct _node_t
{
int num_kids;
char *content;
struct _node_t *(kids[]);

I would use: struct _node_t **kids;

} node_t;

void node_alloc(node_t *parent, char *content, int num)
{
int i;

parent = malloc(sizeof(node_t));
parent->num_kids = num;
parent->content = malloc(strlen(content));
strcpy(parent->content,content);

I think this is: parent->content = malloc((strlen(content)+1)*sizeof(char));

parent->kids[0] = malloc(sizeof(node_t *)*num);

That is not correct, maybe you meant:
parent->kids=malloc(...). That works with "struct _node_t **kids;

// Initialize children to NULL.
for(i=0;i<num;i++)
parent->kids[i]=NULL;
}


int main(void)
{
node_t *root=NULL,*next=NULL;
char temp[32];

printf("test\n");
sprintf(temp,"%s","Root");
node_alloc(root,temp,4);

That call will not change root, you are passing it by value.

sprintf(temp,"%s","First");
node_alloc(next,temp,3);
root->kids[0]=next;

As a result, the previous statement could lead into trouble.

sprintf(temp,"%s","Second");
node_alloc(next,temp,2);
root->kids[1]=next;
return 0;
}

Malloc'd memory should be freed at some point.


.



Relevant Pages

  • Re: About sleep() in a child process
    ... While the printf() seems not to work at all. ... Parent: PID is 14181 ... int main{ ...
    (comp.unix.programmer)
  • Re: Segmentation fault!
    ... the call to node_alloc AFTER the printf call I get a segmentation ... int num_kids; ... parent = malloc); ... If you don't terminate printf() with \n you may not get anything out. ...
    (comp.lang.c)
  • Re: Segmentation fault!
    ... the call to node_alloc AFTER the printf call I get a segmentation ... int num_kids; ... parent = malloc); ... If you don't terminate printf() with \n you may not get anything out. ...
    (comp.lang.c)
  • Re: Segmentation fault!
    ... int num_kids; ... parent = malloc); ... If you don't terminate printf() with \n you may not get anything out. ... does it not print "test" and then afterwards give me the "Segmentation ...
    (comp.lang.c)
  • Segmentation fault!
    ... int num_kids; ... parent = malloc); ... Only if I outcomment the call to node_alloc will it print "test"! ... It seems that the call to node_alloc is executed before the printf call... ...
    (comp.lang.c)