// classA.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
static unsigned int count = 0;
class A
{
public:
A() {
p = this;
}
~A() {
if (p) {
cout << count++ << endl;
delete p;
p = 0;
}
}
private:
A *p;
};
int _tmain(int argc, _TCHAR* argv[])
{
A a;
return 0;
}
/* compiler: MSC++ compiler in visual c++ 2005 express edition beta
command: cl /nologo /EHsc classA.cpp
*/
/* result:
0
1
2
...
23485
press any key to continue
*/
/*
question: Is it an undefined behavior in C++ Standard?
details: p is a point of class A, when destructor call 'delete p',it will
cause destructor again.
Is there any limit calling destructor recursively?
*/ÿÿÿ 10 1521
"aka" <ak********@hotmail.com> skrev i en meddelelse
news:d0**********@news.yaako.com... // classA.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include <iostream>
using namespace std;
static unsigned int count = 0;
class A { public: A() { p = this; }
~A() { if (p) { cout << count++ << endl; delete p; p = 0; } }
private: A *p; };
int _tmain(int argc, _TCHAR* argv[]) { A a;
return 0; }
/* compiler: MSC++ compiler in visual c++ 2005 express edition beta command: cl /nologo /EHsc classA.cpp */
/* result: 0 1 2 ... 23485 press any key to continue */
/* question: Is it an undefined behavior in C++ Standard?
What is? What you do is calling the destructor of an object more than once
and that causes undefined behaviour. details: p is a point of class A, when destructor call 'delete p',it will cause destructor again. Is there any limit calling destructor recursively?
No. You can do as long as you please. Almost! Your program
Not in your program - it has endless recursion.
Also I fail to sees what youre attempting to do.
/Peter
*/ÿÿÿ
aka wrote: // classA.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include <iostream>
using namespace std;
static unsigned int count = 0;
class A { public: A() { p = this;
Why?
}
~A() { if (p) { cout << count++ << endl; delete p; p = 0; } }
private: A *p; };
int _tmain(int argc, _TCHAR* argv[]) { A a;
return 0; }
/* compiler: MSC++ compiler in visual c++ 2005 express edition beta command: cl /nologo /EHsc classA.cpp */
/* result: 0 1 2 ... 23485 press any key to continue */
/* question: Is it an undefined behavior in C++ Standard?
Yes. details: p is a point of class A, when destructor call 'delete p',it will cause destructor again. Is there any limit calling destructor recursively?
As long as it can. :-)
--
Ioannis Vranos http://www23.brinkster.com/noicys
aka wrote: // classA.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include <iostream>
using namespace std;
static unsigned int count = 0;
class A { public: A() { p = this;
You store a pointer to the object itself as member? What would that be good
for?
}
~A() { if (p) {
p is never 0 unless there is an error in your program.
cout << count++ << endl; delete p;
This would recursively call the destructor. This is not allowed. For each
object, a constructor as well as the destructor gets called exactly once.
Another problem is that you must never use delete on an object that you
didn't get from new. So this isn't valid for any non-dynamically allocated
objects.
p = 0; } }
private: A *p; };
int _tmain(int argc, _TCHAR* argv[]) { A a;
return 0; }
/* compiler: MSC++ compiler in visual c++ 2005 express edition beta command: cl /nologo /EHsc classA.cpp */
/* result: 0 1 2 ... 23485 press any key to continue */
/* question: Is it an undefined behavior in C++ Standard?
Yes.
details: p is a point of class A, when destructor call 'delete p',it will cause destructor again.
For what reason? What are you trying to accomplish?
Is there any limit calling destructor recursively?
Yes. It must _never_ be called recursively. Also, your 'delete p' doesn't
just call the destructor, it also tries to free the memory that the object
occupied, which only works if the object is allocated dynamically.
Actually, I'm surprised that your program doesn't crash.
>> Is this an undefined behavior in C++ Standard?
Yes, it's undefined behavior to delete a pointer that wasn't created
with new. Is there any limit calling destructor recursively?
You shouldn't "call" a destructor, and you shouldn't delete a pointer
that wasn't newed in order for the destructor to be called. The
destructor for an object should only be called one, if it's called
twice it's undefined behavior.
I'm learning Visual C++ 2005. I want to know how it implement ISO C++
standard, such as undefined behavior.
"Peter Koch Larsen" <pk*****@mailme.dk> дÈëÏûÏ¢
news:zR*********************@news000.worldonline.d k... "aka" <ak********@hotmail.com> skrev i en meddelelse news:d0**********@news.yaako.com... // classA.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include <iostream>
using namespace std;
static unsigned int count = 0;
class A { public: A() { p = this; }
~A() { if (p) { cout << count++ << endl; delete p; p = 0; } }
private: A *p; };
int _tmain(int argc, _TCHAR* argv[]) { A a;
return 0; }
/* compiler: MSC++ compiler in visual c++ 2005 express edition beta command: cl /nologo /EHsc classA.cpp */
/* result: 0 1 2 ... 23485 press any key to continue */
/* question: Is it an undefined behavior in C++ Standard? What is? What you do is calling the destructor of an object more than once and that causes undefined behaviour. details: p is a point of class A, when destructor call 'delete p',it
will cause destructor again. Is there any limit calling destructor recursively?
No. You can do as long as you please. Almost! Your program Not in your program - it has endless recursion.
Also I fail to sees what youre attempting to do.
/Peter
*/ÿÿÿ
"aka" <ak********@hotmail.com> wrote... I'm learning Visual C++ 2005. I want to know how it implement ISO C++ standard, such as undefined behavior. [...]
Implement it as you see fit or as you grandma explained to your grandpa.
It does not matter. It is explicitly undefined and that means nothing
is prescribed and nothing _specific_ is expected.
sorry for my poor english, but i wonder what do you mean '...or as you
grandma explained to your grandpa'.
I think a compiler must deal with these undefined behaviors, i just want to
know.
"Victor Bazarov" <v.********@comAcast.net> дÈëÏûÏ¢
news:WJ********************@comcast.com... "aka" <ak********@hotmail.com> wrote... I'm learning Visual C++ 2005. I want to know how it implement ISO C++ standard, such as undefined behavior. [...]
Implement it as you see fit or as you grandma explained to your grandpa. It does not matter. It is explicitly undefined and that means nothing is prescribed and nothing _specific_ is expected.
aka wrote: sorry for my poor english, but i wonder what do you mean '...or as you grandma explained to your grandpa'.
I think a compiler must deal with these undefined behaviors, i just want to know.
How would I put this... No, it does not have to deal with them. There
are no requirements imposed on the compiler or the program it produces.
Whatever they do is OK (from the C++ point of view).
To further complicate the issue: yes, the compiler can throw a shoe (die
or dump core or cause hardware malfunction) or create the code that sends
obscene e-mails to your colleagues. Of course, you are unlikely to see
any of this. While it's allowed, nobody in their right mind would put
such behaviour into their product. Well, they might, only once. After
that it wouldn't be much of a product any more, in a market sense.
If you really want to know the details of Visual C++ implementation, you
have to ask in a newsgroup dedicated to it. But it is still pointless.
Nobody will explain exactly how they implement something that has not been
defined in any way. Perhaps you just have to look the word "undefined" in
the dictionary to understand...
And don't top-post, please. Thank you. "Victor Bazarov" <v.********@comAcast.net> п╢хКоШо╒ news:WJ********************@comcast.com...
"aka" <ak********@hotmail.com> wrote...
I'm learning Visual C++ 2005. I want to know how it implement ISO C++ standard, such as undefined behavior. [...]
Implement it as you see fit or as you grandma explained to your grandpa. It does not matter. It is explicitly undefined and that means nothing is prescribed and nothing _specific_ is expected.
V
aka wrote: sorry for my poor english, but i wonder what do you mean '...or as you grandma explained to your grandpa'.
I think a compiler must deal with these undefined behaviors, i just want to know.
No. That's the whole point. The compiler doesn't have to deal with it.
Anything that the compiler might do or not do is ok. It doesn't need to do
anything specific, it can just let things happen. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: E. Robert Tisdale |
last post by:
In the context of the comp.lang.c newsgroup,
the term "undefined behavior" actually refers to
behavior not defined by the ANSI/ISO C 9 standard.
Specifically, it is *not* true that "anything can...
|
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: Ken Turkowski |
last post by:
The construct
(void*)(((long)ptr + 3) & ~3)
worked well until now to enforce alignment of the pointer to long
boundaries. However, now VC++ warns about it, undoubtedly to help things
work on 64...
|
by: jimjim |
last post by:
Hello,
#include <stdio.h>
int main(int argc, char *argv)
{
int x = 1;
printf("%d %d %d\n", ++x, x, x++);
return 0;
}
|
by: dragoncoder |
last post by:
Hi all,
Does the following code invoke undefined behaviour ?
$ cat a1.cc
#include <iostream>
#include <limits>
int main() {
int a = INT_MAX/2;
|
by: avsharath |
last post by:
In "Bjarne Stroustrup's C++ Style and Technique FAQ" at:
http://www.research.att.com/~bs/bs_faq2.html#evaluation-order
for the statement:
f(v,i++);
he says that "the result is undefined...
|
by: Rajesh S R |
last post by:
Can anyone tell me what is the difference between undefined behavior
and unspecified behavior?
Though I've read what is given about them, in ISO standards, I'm still
not able to get the...
|
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: blargg |
last post by:
Does ~0 yield undefined behavior? C++03 section 5 paragraph 5 seems to
suggest so:
The description of unary ~ (C++03 section 5.3.1 paragraph 8):
But perhaps "one's complement" means the...
|
by: coolguyaroundyou |
last post by:
Will the following statement invoke undefined behavior :
a^=b,b^=a,a^=b ;
given that a and b are of int-type ??
Be cautious, I have not written a^=b^=a^=b ; which, of course, is
undefined....
|
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:
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |