473,242 Members | 1,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

quick question about pointers

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
10 1449
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
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

"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
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
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
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
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
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
> 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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
16
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last...
0
by: bharathreddy | last post by:
Delegates Here in this article I will explain about delegates in brief. Some important points about delegates. This article is meant to only those who already know delegates, it will be a quick...
6
MonaLisaO
by: MonaLisaO | last post by:
Hi I have a question about pointers: int a = 10; BOOL rc = myMethod (struct_A, &a); My method is like this: BOOL myMethod (STRUCT aStruct, int* numElements) {
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.