Your C++ Homework

R

Raymond Martineau

Gernot Frisch said:
Catalin Pitis said:
Definitely agree with you there! While having a teacher (for anything in
life really) will make you learn faster, still there's no need for the
teacher. I've yet to find a book which has a good explanation on why:


int *p_k;

*p_k = 4;


is illegal.

When you find it, tell me :). I'm curious.

Don't understand: Is there no book that tells you that it's illegal? I'm
gonna write one. Title:
"Dereferencing an uninitialized pointer is illegal".

Content:
Dereferencing an uninitialized pointer is illegal. Example:
int* p_k; *p_k=4; [1]

References:
[1] JKob in comp.lang.c++, 27.10.2004

Am I gonna be rich?
-Gernot

It should be a missundertanding :). By illegal I meant that the compiler
doesn't allow me to do it. But I'm allowed to do it. I surely wouldn't do it
:).

The compiler generally issues a warning in that case. It doesn't block you
from doing it (since the standard merely says it's undefined rather than an
error), but alerts you in case you forgot to do somethine first.

The piece of code is only stated to be undefined under general standards. A
compiler doesn't complain because there is a chance that the variable may
be initialized somehow (but I doubt it). As a result, the only problems
you encounter will be at run-time (or in the case of old dos programs, a
"null pointer assignment" message on shutdown.)
 
I

Ioannis Vranos

Raymond said:
The compiler generally issues a warning in that case. It doesn't block you
from doing it (since the standard merely says it's undefined rather than an
error), but alerts you in case you forgot to do somethine first.

The piece of code is only stated to be undefined under general standards. A
compiler doesn't complain because there is a chance that the variable may
be initialized somehow (but I doubt it). As a result, the only problems
you encounter will be at run-time (or in the case of old dos programs, a
"null pointer assignment" message on shutdown.)


Assignment of an integer to a pointer type is not allowed, unless some
"dirty" casting is used.
 
J

JKop

Jonathan Turkanis posted:
I was trying to express what I thought was a pretty obvious point
without being overtly rude. As is so often the case, I probably should
have kept my mouth shut.


Jonathan


Curious, that's all.


-JKop
 
J

JKop

Mike Wahler posted:
On 10/23/2004


-Mike


I presume that was to a Win32 newsgroup, no?

The term "expert" is relative. Outside of this newsgroup, I would definitely
say that I'm an expert C++ programmer, but if I were to say that right here
then it wouldn't have much weight as there's people here with superior
knowledge of C++ than me (in particular, of the Standard Library... it
really is about time I got a book on it!).


If we throw the Standard Library out of the window for the moment, then I
would be comfortable saying here that I'm an expert C++ programmer - I
pretty much understand and know how to use all of the features of C++.


Anyway Mike, thanks for clarifying whatever the hell you were trying to
clarify.


-JKop
 
J

JKop

Ioannis Vranos posted:
Assignment of an integer to a pointer type is not allowed, unless some
"dirty" casting is used.


void* const address = reinterpret_cast<void* const>(666);


Not ttoooo dirty.


-JKop
 
L

Lieven

Patrick said:
Hi Fred,


Nice. Did it help? Did you learn a lot?

Regards,
Patrick

Isn't there a session where you are to defend your project/assignment.
If yes, start understanding the code, if no, bad school.
 
J

Joe Van Dyk

Catalin Pitis said:
When you find it, tell me :). I'm curious.

Don't understand: Is there no book that tells you that it's illegal?
I'm gonna write one. Title:
"Dereferencing an uninitialized pointer is illegal".

Content:
Dereferencing an uninitialized pointer is illegal. Example:
int* p_k; *p_k=4; [1]

References:
[1] JKob in comp.lang.c++, 27.10.2004

Am I gonna be rich?
-Gernot


So, if I have a pointer to an int that's unitialized, how do I tell it
to point at a given int?
 
J

JKop

Joe Van Dyk posted:
Catalin Pitis said:
Depends on the teacher. With a good teacher(s), you may learn
faster and spend less time for digging in books, manuals and other
materials. I missed that a lot. But I took care to guide other's
study in the companies that I worked.


Definitely agree with you there! While having a teacher (for
anything in life really) will make you learn faster, still there's
no need for the teacher. I've yet to find a book which has a good
explanation on why:


int *p_k;

*p_k = 4;


is illegal.

When you find it, tell me :). I'm curious.

Don't understand: Is there no book that tells you that it's illegal?
I'm gonna write one. Title:
"Dereferencing an uninitialized pointer is illegal".

Content:
Dereferencing an uninitialized pointer is illegal. Example:
int* p_k; *p_k=4; [1]

References:
[1] JKob in comp.lang.c++, 27.10.2004

Am I gonna be rich?
-Gernot


So, if I have a pointer to an int that's unitialized, how do I tell it
to point at a given int?

int* p_k;

int a = 1;
int b = 2;
int c = 3;
int d = 4;

p_k = &a;

*p_k = 4;

p_k = &b;

*p_k = 3;

p_k = &c;

*p_k = 2;

p_k = &d;

*p_k = 1;


-JKop
 
H

Howard

Joe Van Dyk said:
On 2004-10-27 02:17:09 -0700, "Gernot Frisch" <[email protected]> said:



So, if I have a pointer to an int that's unitialized, how do I tell it to
point at a given int?

Two ways:

Point it at an existing variable:

int* p; // uninitialized
....
int a = 6; // stores 6 in variable a
....
p = &a; // now, p points to a, so *p == 6


or,

Create its own memory to point at, and assign a value to that

int* p; // uninitialized
....
p = new int; // create new storage for p to point to
....
*p = 9; // stores 9 in memory set aside for *p
....
delete p; // don't forget to delete when you new!



-Howard

(P.S., you could also set the value of *p in the new statement itself, with
p = new int(9);)
 
K

Kelsey Bjarnason

[snips]

So, if I have a pointer to an int that's unitialized, how do I tell it
to point at a given int?

Assign it.

int *p; // uninitialized
int x;

*p = 3; // invalid, p hasn't been pointed at anything
p = &x; // perfectly good, points p at x
if ( *p ) // bad again, as x (aka *p) has no defined value
x = 3; // perfectly good
if (*p) // okay now, as x is initialized
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,518
Latest member
TobiasAxf

Latest Threads

Top