473,383 Members | 1,829 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,383 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 1451
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) {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.