pointer problem

L

Lalatendu Das

Hi ,
Any way i have problem related to pointer
let's say i have a double pointer like
node_t **headptr =NULL ; //global one
// let's say this is the defination of node_t
typedef struct list {
int a;
int b;
} node_t ;
Say i declare a variable like
node_t *head;
head =*headptr ;
Is this going to show any error or segment fault . what i assume that
once i declare headptr a adress will be associated with it . and i can
say it to point to nothing by assigning NULL and the same i am
assigning to head pointer now .
Is this alowed to use pointer like this . is this implimentable in both
user program and programs running in kernel land like Device drivers .

i checked in implimenting this in a user applcation it is working fine
without any warning also
OS - MP-RAS ( a unix variant) . using cc
 
V

Vladimir Oka

Lalatendu said:
Hi ,
Any way i have problem related to pointer
let's say i have a double pointer like
node_t **headptr =NULL ; //global one
// let's say this is the defination of node_t
typedef struct list {
int a;
int b;
} node_t ;
Say i declare a variable like
node_t *head;
head =*headptr ;
Is this going to show any error or segment fault . what i assume that
once i declare headptr a adress will be associated with it . and i can
say it to point to nothing by assigning NULL and the same i am
assigning to head pointer now .
Is this alowed to use pointer like this . is this implimentable in both
user program and programs running in kernel land like Device drivers .

i checked in implimenting this in a user applcation it is working fine
without any warning also
OS - MP-RAS ( a unix variant) . using cc

You're OK assigning NULL and uninitialised pointers around, but you
have to be careful not to dereference them. It's probably safest to
initialise all to NULL, and check for that before dereferencing.
 
F

Fred Kleinschmidt

Lalatendu Das said:
Hi ,
Any way i have problem related to pointer
let's say i have a double pointer like
node_t **headptr =NULL ; //global one
// let's say this is the defination of node_t
typedef struct list {
int a;
int b;
} node_t ;
Say i declare a variable like
node_t *head;
OK, 'head' declared as a pointer to a node_t. It is not initialized to point
to any actual space.
head =*headptr ;
Crash! 'headptr' does not point to anything - it was initialized to NULL.
You cannot dereference NULL.
 
K

Keith Thompson

Vladimir Oka said:
You're OK assigning NULL and uninitialised pointers around, but you
have to be careful not to dereference them. It's probably safest to
initialise all to NULL, and check for that before dereferencing.

Actually, copying the value of an uninitialized pointer invokes
undefined behavior. For example:

{
int *p1; /* p1 is uninitialized */
int *p2;
p2 = p1; /* undefined behavior */
}

The assignment will *probably* just copy the bits, but there's no
guarantee that it won't cause a trap (e.g., if the processor uses some
special opcode for pointer assignments). Just referring to the value
of an uninitialized pointer, whether you dereference it or not,
invokes undefined behavior.

That's just as well, though. If you're referring to the value of an
uninitialized variable, there's a bug in your code, and you need to
fix it anyway.
 

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

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top