Segmentation fault!
- From: Paminu <sdef@xxxxxxx>
- Date: Sat, 11 Feb 2006 14:30:59 +0100
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;
node_t *node_alloc(void *content, int num)
{
node_t *parent;
parent = malloc(sizeof(node_t));
parent->content = content;
parent->num_kids = num;
node_t *new_kids[num];
int i;
// Initialize children to NULL.
for (i = 0; i < num; i++)
{
parent->kids[i]=NULL;
}
return parent;
}
int main(void)
{
printf("test");
node_t *root = node_alloc("Root",4);
return 0;
}
Only if I outcomment the call to node_alloc will it print "test"! Why does
it not print "test" and then afterwards give me the "Segmentation Fault"?
It seems that the call to node_alloc is executed before the printf call...
.
- Follow-Ups:
- Re: Segmentation fault!
- From: Barry Schwarz
- Re: Segmentation fault!
- From: Rod Pemberton
- Re: Segmentation fault!
- From: Vladimir S. Oka
- Re: Segmentation fault!
- Prev by Date: Re: How can I write highly optimized C code for reading and writing?
- Next by Date: Re: efficiency of && vs. array
- Previous by thread: ctrl alt del
- Next by thread: Re: Segmentation fault!
- Index(es):
Relevant Pages
|