quick question about pointers

S

Snake

Hello guys,
I have a quick question about pointers.
When I say
int *p;
*p = 40;
cout << *p;
why does that produce and error message(something about fault address)?
Isnt that I created a pointer and I made it point to the value of 40 and
then display it?!
Thanx alot,any help is really appreciated
Have fun
 
P

Phlip

Snake said:
Hello guys,
I have a quick question about pointers.
When I say
int *p;

What does p now point to?

That says "copy 40 into the location p points to".
cout << *p;
why does that produce and error message(something about fault address)?
Isnt that I created a pointer and I made it point to the value of 40 and
then display it?!

One of the joys of C++ is if you don't assign a variable, it contains
garbage. Future behavior is then undefined.

In this case, p points to anywhere. Assigning could crash, or could work -
that's undefined.
 
B

Bret Pehrson

Pointers either:

- point to something

- point to nothing

By default, pointers point to nothing.

So, in your case, you create a pointer that points to nothing, then try to
dereference nothing and assign value.

The compiler will not flag this as an error, and what actually happens is
undefined (in a protected-mode environment, most times this will cause a crash
similar to what you describe).

So, depending on what specifically you want to do, you first must make your
pointer point to something prior to dereferencing and assiging a value. (The
*p is the dereference, the = 40 is assigning the value.)

So, you could point p to something that already exists, such as:

int i;
int *p = &i;
//etc.

or have your pointer point to some allocated memory, such as:

int *p = new int;
//etc. (don't forget to release the allocated memory through delete p

Dig?
 
S

Sharad Kala

Snake said:
Hello guys,
I have a quick question about pointers.
When I say
int *p;
*p = 40;
cout << *p;
why does that produce and error message(something about fault address)?
Isnt that I created a pointer and I made it point to the value of 40 and
then display it?!
No. You created a pointer and made it point to nowhere (some garbage address).
Then you try to write something into that location. A crash is inevitable.
Pointers should always contain some valid address before you try to do any
operations with them.

int i;
int *p;
p= &i; //OK
// p is now pointing to some valid memory address.

or else
int *p = new int(42); //OK
// p now points to a vaild dynamic store address.
delete p;

Best wishes,
Sharad
 
J

Jon Bell

Pointers either:

- point to something

- point to nothing

By default, pointers point to nothing.

More precisely, by default, a newly-declared pointer points to whatever
address is designated by the sequence of bits that happened to be in the
memory location that you have declared to be a pointer. You have no
control over that sequence of bits; it might as well be random as far as
you are concerned. Therefore, a newly-declared pointer effectively points
to some random memory location.
So, in your case, you create a pointer that points to nothing, then try to
dereference nothing and assign value.

Actually, you'll get a memory address of some kind. It might designate
some memory location that your operating system forbids you from
accessing, in which case your program will crash immediately. Under Unix,
it's called a "segmentation fault."

More insidiously, that address might designate some location within your
program's memory space. It might be the address of some variable that
you're declared elsewhere, in which case if you write to it, you'll
mysteriously change the behavior of some other (possibly remote) part of
the program. It might be the address of some compiled program code, in
which case when your program's execution flow reaches that point, it will
probably dis with an "illegal instruction" error.
 
B

Bret Pehrson

Dude, the original poster is obviously a newbie, that is why I *deliberately*
kept it simple.

The term 'nothing' is more that suitable for this discussion --
 
K

KTC

Bret Pehrson said:
Dude, the original poster is obviously a newbie, that is why I
*deliberately* kept it simple.

The term 'nothing' is more that suitable for this discussion --

But oversimplification (or plain wrong info) doesn't help a newbie.
Ever recall when you were little and at school and they teach you
something and then later tell you it was not actually correct? Did it
ever annony you?

You could have said rubbish, garbage etc. and your answer would still
have made prefect sense to a newbie...
Remember not to underestimate the intelligence of a newbie. Just
because they know less than you at the moment doesn't mean they can't
be clever than you.

KTC
 
B

Bret Pehrson

Cripes!

This isn't an oversimplification. The pointer points to nothing by default.

What makes 'garbage' a more suitable term?

What I said what correct, however this much more behind the statements that
will eventually be learned, without invalidating anything I said.

Goodbye.
 
B

Bret Pehrson

What I said what correct, however this much more behind the statements that

I need to correct my mistyped response:

"What I said what correct, however [there is] much more behind the statements
that..."


Bret said:
Cripes!

This isn't an oversimplification. The pointer points to nothing by default.

What makes 'garbage' a more suitable term?

What I said what correct, however this much more behind the statements that
will eventually be learned, without invalidating anything I said.

Goodbye.
 
J

jeffc

Snake said:
Hello guys,
I have a quick question about pointers.
When I say
int *p;
*p = 40;
cout << *p;
why does that produce and error message(something about fault address)?
Isnt that I created a pointer and I made it point to the value of 40 and
then display it?!

There are 2 values involved here.
1) the value of *p
2) the value of p

These are 2 very different things. You've initialized *p, but you haven't
initialized p.
 

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

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top