I am having an issue with malloc and gcc. Is there something wrong
with my code or is this a compiler bug ?
I am running this program:
#include <stdio.h>
#include <stdlib.h>
typedef struct pxl {
double lon, lat;
double x,y,z;
double px,py;
} pixel;
struct chn {
int index;
struct nde *node;
};
struct nde {
pixel position;
struct chn *forward;
struct chn *reverse;
int *chain_num;
int size;
};
typedef struct chn chain;
typedef struct nde node;
node *createnode( );
main()
{
node *startn;
startn = createnode( );
startn->size = 2;
startn->forward = malloc( 2 * sizeof( chain * ) );
startn->reverse = malloc( 2 * sizeof( chain * ) );
printf("sf %p\nsr %p\n\n",
startn->forward,
startn->reverse
);
printf("sf %p\nsr %p\n\n",
startn->forward,
startn->reverse
);
}
node *createnode( )
{
node *node;
node = malloc( sizeof(node) );
return node;
}
I get the following output.
sf 0x660168
sr 0x660178
sf 0xa383731
sr 0x660178
The startn->forward point changes between the two printfs despite
there being no code between them.
with my code or is this a compiler bug ?
I am running this program:
#include <stdio.h>
#include <stdlib.h>
typedef struct pxl {
double lon, lat;
double x,y,z;
double px,py;
} pixel;
struct chn {
int index;
struct nde *node;
};
struct nde {
pixel position;
struct chn *forward;
struct chn *reverse;
int *chain_num;
int size;
};
typedef struct chn chain;
typedef struct nde node;
node *createnode( );
main()
{
node *startn;
startn = createnode( );
startn->size = 2;
startn->forward = malloc( 2 * sizeof( chain * ) );
startn->reverse = malloc( 2 * sizeof( chain * ) );
printf("sf %p\nsr %p\n\n",
startn->forward,
startn->reverse
);
printf("sf %p\nsr %p\n\n",
startn->forward,
startn->reverse
);
}
node *createnode( )
{
node *node;
node = malloc( sizeof(node) );
return node;
}
I get the following output.
sf 0x660168
sr 0x660178
sf 0xa383731
sr 0x660178
The startn->forward point changes between the two printfs despite
there being no code between them.
Comment