P
Paminu
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=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...
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=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...