By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,679 Members | 1,063 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,679 IT Pros & Developers. It's quick & easy.

quick question about pointers

P: n/a
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

Jul 22 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Snake wrote:
Hello guys,
I have a quick question about pointers.
When I say
int *p;
What does p now point to?
*p = 40;
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.

--
Phlip
http://www.xpsd.org/cgi-bin/wiki?Tes...UserInterfaces
Jul 22 '05 #2

P: n/a
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?

Snake wrote:

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


--
Bret Pehrson
mailto:br**@infowest.com
NOSPAM - Include this key in all e-mail correspondence <<38952rglkwdsl>>
Jul 22 '05 #3

P: n/a

"Snake" <ha***@rogers.com> wrote in message
news:50******************@twister01.bloor.is.net.c able.rogers.com...
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
Jul 22 '05 #4

P: n/a
In article <40***************@infowest.com>,
Bret Pehrson <br**@infowest.com> wrote:
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.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #5

P: n/a
Thanks alot guys,that really helped
"Snake" <ha***@rogers.com> wrote in message
news:50******************@twister01.bloor.is.net.c able.rogers.com...
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

Jul 22 '05 #6

P: n/a
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 --

Jon Bell wrote:

In article <40***************@infowest.com>,
Bret Pehrson <br**@infowest.com> wrote:
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.

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA


--
Bret Pehrson
mailto:br**@infowest.com
NOSPAM - Include this key in all e-mail correspondence <<38952rglkwdsl>>
Jul 22 '05 #7

P: n/a
KTC
Bret Pehrson <br**@infowest.com> for some reason wrote:
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
--
Experience is a good school but the fees are high.
- Heinrich Heine
Jul 22 '05 #8

P: n/a
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.

KTC wrote:

Bret Pehrson <br**@infowest.com> for some reason wrote:
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
--
Experience is a good school but the fees are high.
- Heinrich Heine


--
Bret Pehrson
mailto:br**@infowest.com
NOSPAM - Include this key in all e-mail correspondence <<38952rglkwdsl>>
Jul 22 '05 #9

P: n/a
> 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 Pehrson wrote:

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.

KTC wrote:

Bret Pehrson <br**@infowest.com> for some reason wrote:
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
--
Experience is a good school but the fees are high.
- Heinrich Heine


--
Bret Pehrson
mailto:br**@infowest.com
NOSPAM - Include this key in all e-mail correspondence <<38952rglkwdsl>>


--
Bret Pehrson
mailto:br**@infowest.com
NOSPAM - Include this key in all e-mail correspondence <<38952rglkwdsl>>
Jul 22 '05 #10

P: n/a

"Snake" <ha***@rogers.com> wrote in message
news:50******************@twister01.bloor.is.net.c able.rogers.com...
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.
Jul 22 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.