July 25th, 2013, 06:37 PM
Run-time errors with linked lists
All I'm trying to do is print a value in a linked list.
What's wrong with this code?
node_t *n1_p, *n2_p, *n3_p;
n1_p = (node_t *)malloc(sizeof(node_t));
n1_p->volts = 115;
n2_p = (node_t *)malloc(sizeof(node_t));
n2_p->volts = 12;
n2_p = n3_p;
n1_p->linkp = n2_p;
#define PRT_TRUE(statement) \
printf("%s: %d\n", #statement, statement)
#define PRT_INT(label) \
printf("%s = %d\n", #label, (label))
typedef struct node_s
struct node_s *linkp;
#endif // INCL
July 25th, 2013, 06:48 PM
You have this line:
n3_p is uninitialized and you're losing all the stuff you allocated and set in n2_p before this.
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
July 25th, 2013, 06:52 PM
n3_p is an uninitialized pointer.
Also, with this,
n2_p = n3_p;
you are dropping n2_p, which creates a memory leak. Everything you do with pointers must work to avoid memory leaks, just as everything you do with a power saw must work to keep all your extremities attached to your body.
July 25th, 2013, 06:54 PM
I somehow switched the left and right of my assignment statements from what I intended.
I'll read up on memory leaks.