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 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
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>>
"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
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
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
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>>
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
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>>
> 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>>
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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)
{
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
| |