473,804 Members | 3,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(somethi ng 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 1492
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(somethi ng 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(somethi ng 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**@inf owest.com
NOSPAM - Include this key in all e-mail correspondence <<38952rglkwdsl >>
Jul 22 '05 #3

"Snake" <ha***@rogers.c om> wrote in message
news:50******** **********@twis ter01.bloor.is. net.cable.roger s.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(somethi ng 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.co m>,
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 "segmentati on 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*******@pres by.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.c om> wrote in message
news:50******** **********@twis ter01.bloor.is. net.cable.roger s.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(somethi ng 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.co m>,
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 "segmentati on 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*******@pres by.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA


--
Bret Pehrson
mailto:br**@inf owest.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 oversimplificat ion (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 oversimplificat ion. 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 oversimplificat ion (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**@inf owest.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 oversimplificat ion. 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 oversimplificat ion (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**@inf owest.com
NOSPAM - Include this key in all e-mail correspondence <<38952rglkwdsl >>


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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
3414
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 as ff: typedef struct { float *data ; int count ;
16
6086
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 element in the container? Or is there a cleaner way of getting an iterator to the last element?
0
4776
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 review not a detailed one. Delegates quite simply are special type of object, a delegate just contains the details of a method. One good way to understanding delegates is by thinking of delegates as something that gives a name to a method...
6
1363
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
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9582
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9157
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2993
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.