Segmentation fault!

R

Rod Pemberton

Keith Thompson said:
Keith Thompson said:
Rod Pemberton said:
[...]
Rod, a personal observation: your recent postings here have actually
been reasonable.

My posts were reasonable in the beginning too, until 'off topic' abuse
killed every conversation here. You and a supportive group of cronies were
a prime factor in this.

Have you decided not to be an abusive troll?

Reasons: A to K.

[snip]

In other words, no.

Bye.

That was probably just a little bit too harsh.

You did make *some* valid points in your A to K list.

I care about this newsgroup.

You rarely help newbs, declaring everything OT. How is that caring?
You've made some useful contributions to
it, but you have also behaved in a distinctly trollish manner.

IMHO, you too.
You
came into a newsgroup that's been working quite well for many years
and loudly insisted that you're right and we're all wrong about how it
should be run.

You and others, scared off alot of guys who posted. There are 10-15 threads
like that still on the newservers I use. Now, alot of guys who afraid and
just sitting on the sidelines post. I'd say that is a major improvement.
I'm sorry you don't like the new format.
You have also cooperated with Kenny McCormack, who
does *nothing* but deliberately disrupt it.

Perhaps. IMO, his humor is on the mark. And, he usually only interjects
when other people are being assholes and bastards.
And you have repeatedly
insulted me personally, and I do not choose to ignore that.

Yes I did. I thought you needed a loud wake up call to get through your
complacent perspective.
That's why you're in the killfiles of a number of the regulars, and
it's why I don't intend to spend any more of my time communicating
with you.

If by regulars you mean, CBFalconer, Flash Gordon, Mark McIntyre, et.al. I
don't need their help. I don't need my time wasted by their posts. And, if
I'm in their 'killfiles', or OE or Mozilla filter, that's great, because
they won't be harassing me.
I don't choose to use a killfile myself; if I did, you'd be
in it. Because I have *some* respect for you, I wanted you to
understand that.

I read everything. I'm sure your skills are probably much better than I
previously proclaimed. And, I was recently made aware that some of mine are
a bit rusty.


Rod Pemberton
 
O

Old Wolf

Paminu said:
1)
node_t *np;

This line allocates 4 bytes (or whatever) of memory, gives it a
type "node_t *" gives it a name "np".
np = NULL; // INITIALIZE A POINTER TO NULL

This line makes that pointer point somewhere (or nowhere,
if you like).
2)
node_t *np;
np=malloc(sizeof(node_t));
np->kids[0] = NULL; // INITIALIZE A POINTER TO NULL

In the last line "np->kids[0]" is a pointer to node_t which is allowed to
point to NULL. I compiles fine but when I run it I get a segmentation
fault. I can't see the difference between 1) and 2).

'np->kids' is a pointer which is currently pointing to random memory,
since you have not initialized it to anything. When you write

np->kids[0]

you are de-referencing a random pointer so of course you get garbage
results. Regardless of whether you then try to assign a value to it
or not.

As someone else pointed out, this is much like writing:

int **p;
p[0] = NULL;

which is obviously an error.
 
J

Joe Wright

Robin said:
If I have:

int main(void)
{
int **ip;
ip[2] = NULL;
return 0;
}

this compiles and runs fine


Not for me, but anyway. What about this?

#include <stdio.h>
int main(void)
{
int **ip;
int j;
ip[2] = NULL;
ip[100000] = NULL;
ip[46578483] = NULL;
for ( j = 0 ; j < 1000000 ; j++ ) {
ip[j] = NULL;
}
scanf("%d", &j);
ip[j] = NULL;
return 0;
}

Leaving aside the question of whether it actually works, do you think it
ought to work? If not, where are you drawing the line?
You're both wrong.

int **ip;

This defines ip but does not initialize it. It doesn't point to anything
and there are no arrays in sight.

ip[2] = NULL;

and everything after it is nonsense.
 
S

stathis gotsis

Joe Wright said:
Robin said:
What about this?

#include <stdio.h>
int main(void)
{
int **ip;
int j;
ip[2] = NULL;
ip[100000] = NULL;
ip[46578483] = NULL;
for ( j = 0 ; j < 1000000 ; j++ ) {
ip[j] = NULL;
}
scanf("%d", &j);
ip[j] = NULL;
return 0;
}

Leaving aside the question of whether it actually works, do you think it
ought to work? If not, where are you drawing the line?
You're both wrong.

Yes, that was supposed to be wrong. The poster tried to make a point by
presenting a wrong program.
 
B

Barry Schwarz

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!

The absence of output is a side effect of a poor call to printf. The
segmentation fault is the result of undefined behavior. The two
problem are unrelated.
#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));

You should check for malloc failures here. If it did fail (even
though unlikely), the rest of your code would invoke undefined
behavior.
parent->content = content;
parent->num_kids = num;
node_t *new_kids[num];

Many of our compilers still enforce the C89 requirement that
definitions precede executable statements. Get in the habit of
putting your definitions and declarations at the beginning of the
block. It will allow more people to help you.
int i;

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


Here is where you invoke undefined behavior. parent points to an area
of memory that you allocated. That area is not initialized by malloc.
You are using the area to hold a node_t. You initialize some members
of the structure but not kids. Therefore the value of this pointer is
indeterminate. In the practical sense, it is unlikely to point to any
valid memory you own.

Attempting to evaluate this value invokes undefined behavior.
Even though it is not likely to cause a segmentation fault on most
home systems, it is still to be avoided because there is no recovery
once you invoke undefined behavior.

HOWEVER, attempting to dereference this value will (if you are
lucky) cause some type of run time failure like a segmentation fault.
Since kids is a pointer to pointer to struct, kids is, by
definition, the i-th pointer to struct that kids points to. Since
kids was never initialized, it doesn't point anywhere meaningful.
Attempting to store a value (NULL in this case) into an object
(kids in this case) which you have never defined or allocated is
always a bad idea.
}
return parent;
}


int main(void)
{

printf("test");

The absence of a \n in the format string or a subsequent call to
fflush allows your output to remain in the buffer until some other
event forces it to your screen.
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"?

The output probably appears on your screen as a result of main
returning to your OS through your C run time startup and terminate
processes. When you have the segmentation fault, the OS takes control
immediately and kills your task.
It seems that the call to node_alloc is executed before the printf call...

Bad code produces misleading appearances.


Remove del for email
 
M

Mark McIntyre

Keith Thompson said:
[...]
Rod, a personal observation: your recent postings here have actually
been reasonable.

My posts were reasonable in the beginning too,

Please don't start again. Perhaps your words were polite, but your
posts were not either reasonable or polite. If you find that still
offends you, feel free to post elsewhere.
Mark McIntyre
 
M

Mark McIntyre

On Sat, 11 Feb 2006 21:57:04 -0500, in comp.lang.c , "Rod Pemberton"

(Keith wrote)
You rarely help newbs, declaring everything OT. How is that caring?

Thats beyond all resaonableness. You are now into offensive fuckwit
territory.
IMHO, you too.

You have absolutely no idea what you are talking about. I stronfgly
suspect that your posts have annoyed and antagonised most of the
regulars here, many of whom are probably no longer even reading your
nonsense. Thats your loss.

*plonk*
Mark McIntyre
 
K

Kenny McCormack

Mark McIntyre said:
Thats beyond all resaonableness. You are now into offensive fuckwit
territory.

Get a grip, fella! Rod has already stated that he doesn't "get" your
position (And believe me, he's just being nice, saying it that way), so
I think you should assume that you are in his killfile.

And thus wasting your breath breathing fire as you normally do...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,490
Latest member
Finplus

Latest Threads

Top