We all know that this:
void *p;
if (p=malloc(1)) {
free(p);
p;
}
causes undefined behaviour if malloc() succeeds. But what about this?
void *p;
if (p=malloc(1)) {
free(p);
&p;
}
I *think* this is perfectly safe and defined behaviour but as I have
not read the standard I am not sure.
--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"O pointy birds, O pointy-pointy. Anoint my head, anointy-nointy."
- Dr. Michael Hfuhruhurr 8 1790
Joona I Palaste wrote: We all know that this:
void *p = malloc(1); if (NULL != p) { free(p); p; }
causes undefined behavior if malloc() succeeds. But what about this?
void *p = malloc(1); if (NULL != p) { free(p); &p; }
I *think* this is perfectly safe and defined behavior but, as I have not read the standard, I am not sure.
It is safe.
Pointer p no longer points to a valid object after free(p)
but p is still a valid pointer object
so a pointer to p is a valid pointer object as well.
In article <br**********@oravannahka.helsinki.fi>,
Joona I Palaste <pa*****@cc.helsinki.fi> wrote: We all know that this:
void *p; if (p=malloc(1)) { free(p); p; }
causes undefined behaviour if malloc() succeeds. But what about this?
void *p; if (p=malloc(1)) { free(p); &p; }
I *think* this is perfectly safe and defined behaviour but as I have not read the standard I am not sure.
Of course it is perfectly safe. All you do is taking the address of an
existing object. The fact that the object contains nonsense doesn't
cause any problem when you take its address. You might as well ask
whether
void *p;
if (p = malloc (1)) { free (p); p = NULL; }
causes problems. After all, you are assigning NULL to a variable
containing an undefined value.
Joona I Palaste wrote: We all know that this:
void *p; if (p=malloc(1)) { free(p); p; }
causes undefined behaviour if malloc() succeeds. But what about this?
void *p; if (p=malloc(1)) { free(p); &p; }
I *think* this is perfectly safe and defined behaviour but as I have not read the standard I am not sure.
int i;
int *p;
p = &i;
is that safe? does that answer your question? :-)
-nrk.
--
Remove devnull for email
> We all know that this: void *p; if (p=malloc(1)) { free(p); p; }
causes undefined behaviour if malloc() succeeds.
Call me Mr. Deer-in-Headlights, but I'd always been taught that
p;
is a no-op.
Furthermore, how about :
if (p=malloc(1)) {
int x = (int)p;
free(p);
x;
}
Casting a pointer to an int is implementation-defined (but not undefined), isn't it?
There does not seem to be much difference between the above, and
if (p=malloc(1)) {
free(p);
int x = (int)p;
x;
}
My rationale: the reason it's undefined to use a pointer value (without
dereferencing) is for systems where loading such a value to a memory
access register causes an exception. However, if we are just converting to
int, that register would not have to be used. Since the int cast is
implementation-defined, the implementation could always do this
without doing anything undefined. ol*****@inspire.net.nz (Old Wolf) writes: We all know that this:
void *p; if (p=malloc(1)) { free(p); p; }
causes undefined behaviour if malloc() succeeds. Call me Mr. Deer-in-Headlights, but I'd always been taught that p; is a no-op.
It's not a no-op from the viewpoint of the standard. Rather, it
calculates the value of variable `p'. Because the value of
variable `p' is indeterminate (see C99 6.2.4#2) and thus may be a
trap representation (see C99 3.17.2), that expression invokes
undefined behavior.
Furthermore, how about:
if (p=malloc(1)) { int x = (int)p; free(p); x; }
No pointer is involved, so if the conversion `(int) p' produced
a value that is not a trap representation, it is still not a trap
representation after the free().
Casting a pointer to an int is implementation-defined (but not undefined), isn't it?
Not necessarily, see C99 6.3.2.3#6:
If the result cannot be represented in the integer type, the
behavior is undefined.
There does not seem to be much difference between the above, and if (p=malloc(1)) { free(p); int x = (int)p; x; }
That's undefined too, because the value of `p' is still being
accessed. You could access the bytes in `p' through a character
pointer if you like, though.
My rationale: the reason it's undefined to use a pointer value (without dereferencing) is for systems where loading such a value to a memory access register causes an exception.
That's a reasonable reason.
However, if we are just converting to int, that register would not have to be used.
But it's not required not to be used. Behavior is still
undefined.
Since the int cast is implementation-defined, the implementation could always do this without doing anything undefined.
--
"Welcome to the wonderful world of undefined behavior, where the demons
are nasal and the DeathStation users are nervous." --Daniel Fox
Old Wolf wrote: We all know that this:
void *p; if (p=malloc(1)) { free(p); p; }
causes undefined behaviour if malloc() succeeds. Call me Mr. Deer-in-Headlights, but I'd always been taught that p; is a no-op.
In abstract terms, p is evaluated, and then that value is discarded. Whilst
any compiler /may/ optimise it out, it isn't /required/ to. Joona is right;
the behaviour is undefined. Furthermore, how about:
if (p=malloc(1)) { int x = (int)p; free(p); x; }
Casting a pointer to an int is implementation-defined (but not undefined), isn't it?
Yes. But in this case, all you're doing is giving x a valid (albeit
implementation-defined) value. It /looks/ very similar to the first case,
but in fact it isn't. There does not seem to be much difference between the above, and if (p=malloc(1)) { free(p); int x = (int)p; x; }
Oh, there's a world of difference. This one (quite apart from not even
compiling on C89) exhibits undefined behaviour in line 3, because of the
evaluation of p. My rationale: the reason it's undefined to use a pointer value (without dereferencing) is for systems where loading such a value to a memory access register causes an exception. However, if we are just converting to int, that register would not have to be used.
I can find no such guarantee in the Standard.
Since the int cast is implementation-defined, the implementation could always do this without doing anything undefined.
A cast is a process, taking a value as input. In getting the value for input
to the casting process, you invoke undefined behaviour, so the cast is way
too late.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Joona I Palaste wrote: We all know that this:
void *p; if (p=malloc(1)) { free(p); p; }
causes undefined behaviour if malloc() succeeds.
Correct.
But what about this?
void *p; if (p=malloc(1)) { free(p); &p; }
I *think* this is perfectly safe and defined behaviour but as I have not read the standard I am not sure.
It is. You should. If you did, then you would be.
Probably.
--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
On 16 Dec 2003, Joona I Palaste wrote: causes undefined behaviour if malloc() succeeds. But what about this?
void *p; if (p=malloc(1)) { free(p); &p; }
I *think* this is perfectly safe and defined behaviour but as I have not read the standard I am not sure.
Obviously:
void reincarnate(void **p) { *p=malloc(1); }
void test(void) {
void *p;
if (p=malloc(1)) {
free(p);
reincarnate(&p)
}
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Scott J. McCaughrin |
last post by:
The following program compiles fine but elicits this message from the
linker: "undefined reference to VarArray::funct" and thus fails.
It seems to behave as if the static data-member:...
|
by: duckfreezone |
last post by:
Hi,
I've got a small test program which behaves correctly (IMHO) on all
compilers that I have acccess to, with the exception of the gcc on
Macintosh OSX "gcc version 4.0.0 (Apple Computer, Inc....
|
by: Sumeet |
last post by:
I met a question in a test which invoked undefined behaviour and i was
asked to answer the Expected answer of the question
Specifications := Win98 Os Tc compiler
int i=23;
i=(i++|++i)^(i++ +...
|
by: Nitin Bhardwaj |
last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a
student and have been doing & studying C programming for the last 4
years).....and also a regular reader of "comp.lang.c"
I...
|
by: David Resnick |
last post by:
I had a problem going from gcc 2.96 to gcc 3.2.3 and narrowed it down to
the following code. The question is whether the problem is undefined
behavior on the code's part or a compiler bug.
...
|
by: RoSsIaCrIiLoIA |
last post by:
On Mon, 07 Feb 2005 21:28:30 GMT, Keith Thompson <kst-u@mib.org>
wrote:
>"Romeo Colacitti" <wwromeo@gmail.com> writes:
>> Chris Torek wrote:
>>> In article <4205BD5C.6DC8@mindspring.com>
>>>...
|
by: VK |
last post by:
(see the post by ASM in the original thread; can be seen at
<http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b>
as an option)
As that is not in relevance to...
|
by: Frederick Gotham |
last post by:
I have a general idea of the different kinds of behaviour described by the
C Standard, such as:
(1) Well-defined behaviour:
int a = 2, b = 3;
int c = a + b;
(Jist: The code will work...
|
by: Franz Hose |
last post by:
the following program, when compiled with gcc and '-std=c99', gcc says
test.c:6: error: jump into scope of identifier with
variably modified type
that is, it does not even compile.
...
|
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: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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: 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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |