#include <cstring>
#include <iostream>
using namespace std;
class X
{
public:
X();
X( char * );
~X();
void print();
private:
char *_str;
};
class Y : public X
{
public:
Y();
Y( char * );
~Y();
void print();
private:
char *_str;
};
X::X()
{
static char *str = "X's str empty";
_str = new char[ strlen( str ) ];
strcpy(_str, str);
cout << "X(), '" << str << "'\n";
}
X::X( char *str )
{
_str = new char[ strlen( str ) ];
strcpy(str, str );
cout << "X(char *), '" << str << "'\n";
}
X::~X()
{
cout << "~X(), deallocating '" << _str << "'\n";
delete _str;
}
void X::print()
{
cout << "X::print():\t' " << _str << "'\n";
}
Y::Y()
{
static char *str = "Y's str empty";
_str = new char[ strlen( str ) ];
strcpy(_str, str);
cout << "Y(), '" << str << "'\n";
}
Y::Y(char *str)
{
_str = new char[ strlen( str ) ];
strcpy( _str, str );
cout << "Y(char *), '" << str << "'\n";
}
Y::~Y()
{
cout << "~Y() deallocating '" << _str << "'\n";
delete _str;
}
void Y::print()
{
cout << "Y::print():\t' " << _str << "'\n";
}
int main()
{
X *x[3];
x[0] = new X( "X's data-index 0" );
x[1] = new Y( "Y's data-index 1" );
// Y y( "y's data-index 2" );
// x[2] = new Y( y );
x[0]->print();
x[1]->print();
// x[2]->print();
delete x[0];
delete x[1];
// delete x[2];
return 0;
} 10 1410 sp********@gmai l.com wrote:
#include <cstring>
#include <iostream>
using namespace std;
class X
{
public:
X();
X( char * );
~X();
void print();
private:
char *_str;
};
[...more code...]
[Subject: Can anyone find the errors?]
Which one?
I guess, the biggest error is not using std::string.
--
Thomas http://www.netmeister.org/news/learn2quote.html sp********@gmai l.com wrote:
Do you want to be more specific?
#include <cstring>
#include <iostream>
using namespace std;
Using declarations are generally bad.
>
class X
{
public:
X();
X( char * );
~X();
Classes that require destructors probably also need copy constructors
and copy assignment operators (rule of three). If you had done
anything that would have invoked copy semantics for your class
you would have had further problems.
void print();
Did you perhaps also want to make this function virtual? Hard to tell
from your program.
>
private:
char *_str;
};
>
X::X()
{
static char *str = "X's str empty";
Deprecated conversion of const char array to char array.
_str = new char[ strlen( str ) ];
strcpy(_str, str);
WHOOP WHOOP... strcpy copies strlen(str) + 1 characters (the
null terminator is not counted by strlen. Undefined behavior
from writing off the end of the string.
cout << "X(), '" << str << "'\n";
ERROR. iostream does not necessarily define ostream functions.
You need <ostreaminclude d.
X::X( char *str )
{
_str = new char[ strlen( str ) ];
strcpy(str, str );
same bugs as before.
>
X::~X()
{
cout << "~X(), deallocating '" << _str << "'\n";
delete _str;
More undefined behavior.
Memory allocated by new[] must be freed with delete[].
Further, you'd have avoided many of these bugs if you'd
have just used std::string rather than making multiple
screw ups with char*. sp********@gmai l.com wrote:
#include <cstring>
#include <iostream>
using namespace std;
class X
{
public:
X();
X( char * );
X(char const);
~X();
void print();
void print() const;
>
private:
char *_str;
};
class Y : public X
{
public:
Y();
Y( char * );
Y(char const *);
~Y();
void print();
void print() const;
>
private:
char *_str;
Hint: You already have a _str in X
};
X::X()
{
static char *str = "X's str empty";
static char str[] = "X's str empty";
_str = new char[ strlen( str ) ];
_str = new char[sizeof str];
strcpy(_str, str);
This copies strlen(str) + 1 characters! strlen() does not count the
terminating zero.
Why don't you use the std::string class?
cout << "X(), '" << str << "'\n";
}
X::X( char *str )
X::X(char const *str)
{
_str = new char[ strlen( str ) ];
_str = new char[strlen(str) + 1];
strcpy(str, str );
cout << "X(char *), '" << str << "'\n";
}
X::~X()
{
cout << "~X(), deallocating '" << _str << "'\n";
delete _str;
}
void X::print()
void X::print() const
{
cout << "X::print():\t' " << _str << "'\n";
}
Y::Y()
X::X() is called here. If you do not want this, you could do:
: X("Y's str empty")
[snip almost equal code]
int main()
{
X *x[3];
x[0] = new X( "X's data-index 0" );
x[1] = new Y( "Y's data-index 1" );
// Y y( "y's data-index 2" );
// x[2] = new Y( y );
x[0]->print();
x[1]->print();
This shall print "X::print()\tX' s str empty\n" http://www.parashift.com/c++-faq-lit...functions.html
// x[2]->print();
delete x[0];
delete x[1];
This line is undefined behaviour: http://www.parashift.com/c++-faq-lit....html#faq-20.7
// delete x[2];
return 0;
}
--
rbh
Ron Natalie wrote:
sp********@gmai l.com wrote:
> cout << "X(), '" << str << "'\n";
ERROR. iostream does not necessarily define ostream functions.
You need <ostreaminclude d.
By this do you mean to imply that a minimal Hello world program using
the C++ IO library would require both <iostreamand <ostream>? That is:
#include <iostream>
#include <ostream>
int main()
{
std::cout << "Hello, world!\n";
}
My reading of the standard agrees with that interpretation. <iostream>
is needed to declare std::cout, but it is declared as extern, which
thanks to 7.1.1.8 means that class std::ostream need not be defined at
that point. Thus we must include <ostreamif we want to use operator<<
of class std::ostream.
This is somewhat surprising to me since there are literally thousands of
examples where only <iostreamis included to allow someone to use
std::cout. Even Stroustrup has such an example: http://www.research.att.com/~bs/hello_world.c
--
Alan Johnson
On Jun 28, 10:01 am, Ron Natalie <r...@spamcop.n etwrote:
spacebe...@gmai l.com wrote:
_str = new char[ strlen( str ) ];
strcpy(_str, str);
WHOOP WHOOP... strcpy copies strlen(str) + 1 characters (the
null terminator is not counted by strlen. Undefined behavior
from writing off the end of the string.
X::X( char *str )
{
_str = new char[ strlen( str ) ];
strcpy(str, str );
same bugs as before.
Actually it is different bugs. The second one
does not overflow _str because it never writes
to it. The undefined behaviour does not come
until this constructor is called with a string
literal as the argument:
> x[0] = new X( "X's data-index 0" );
which I think is the first occurrence of UB
at runtime.
Alan Johnson <aw***@yahoo.co mwrote in message...
Ron Natalie wrote:
sp********@gmai l.com wrote:
cout << "X(), '" << str << "'\n";
ERROR. iostream does not necessarily define ostream functions.
You need <ostreaminclude d.
By this do you mean to imply that a minimal Hello world program using
the C++ IO library would require both <iostreamand <ostream>? That is:
#include <iostream>
#include <ostream>
int main(){
std::cout << "Hello, world!\n";
}
That is a "more correct" way of doing it.
>
My reading of the standard agrees with that interpretation. <iostream>
is needed to declare std::cout, but it is declared as extern, which
thanks to 7.1.1.8 means that class std::ostream need not be defined at
that point. Thus we must include <ostreamif we want to use operator<<
of class std::ostream.
This is somewhat surprising to me since there are literally thousands of
examples where only <iostreamis included to allow someone to use
std::cout.
99 out of 100 compiler implementations have both <istreamand <ostream>
included in <iostream(, but not 'required').
It's that 1 that the wording in the standard goes out of it's way to not
exclude.
<G>
The <ostreamthing has been run-around-the-barn many times in this NG
alone.
--
Bob R
POVrookie
On Jun 28, 1:23 am, Alan Johnson <a...@yahoo.com wrote:
Ron Natalie wrote:
spacebe...@gmai l.com wrote:
cout << "X(), '" << str << "'\n";
ERROR. iostream does not necessarily define ostream functions.
You need <ostreaminclude d.
By this do you mean to imply that a minimal Hello world program using
the C++ IO library would require both <iostreamand <ostream>? That is:
#include <iostream>
#include <ostream>
int main()
{
std::cout << "Hello, world!\n";
}
That's what the standard says.
My reading of the standard agrees with that interpretation. <iostream>
is needed to declare std::cout, but it is declared as extern, which
thanks to 7.1.1.8 means that class std::ostream need not be defined at
that point. Thus we must include <ostreamif we want to use operator<<
of class std::ostream.
In fact, the way I understood the intent was that <iostream>
really should just include <iosfwdand the extern declarations.
The whole point of breaking the thing down into multiple headers
was that you don't pull in tons of lines that you don't need
each time. For whatever reasons, however, all (or at least all
I'm aware of) implementations do include <istreamand <ostream>
when you include <iostream>. So at the last reunion, the
committee voted to bring the standard in line with existing
implementations and most peoples expectations. Unless a future
vote rescends this decision, the #include <ostreamwill not be
necessary in the next version of the standard.
This is somewhat surprising to me since there are literally
thousands of examples where only <iostreamis included to
allow someone to use std::cout. Even Stroustrup has such an
example:http://www.research.att.com/~bs/hello_world.c
I know. And those examples have conditionned user expectations,
and user expectations have conditioned implementations .
Personally, I would have prefered the reverse, that the
implementation only included the minimum, but it's too late now.
Although I'm not sure it's a good general principle, in this
case, I think it's best to align the standard with existing
practice.
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
On Jun 28, 6:42 am, James Kanze <james.ka...@gm ail.comwrote:
>
In fact, the way I understood the intent was that <iostream>
really should just include <iosfwdand the extern declarations.
The whole point of breaking the thing down into multiple headers
was that you don't pull in tons of lines that you don't need
each time. For whatever reasons, however, all (or at least all
I'm aware of) implementations do include <istreamand <ostream>
when you include <iostream>. So at the last reunion, the
committee voted to bring the standard in line with existing
implementations and most peoples expectations. Unless a future
vote rescends this decision, the #include <ostreamwill not be
necessary in the next version of the standard.
This is somewhat surprising to me since there are literally
thousands of examples where only <iostreamis included to
allow someone to use std::cout. EvenStroustruph as such an
example:http://www.research.att.com/~bs/hello_world.c
I know. And those examples have conditionned user expectations,
and user expectations have conditioned implementations .
Personally, I would have prefered the reverse, that the
implementation only included the minimum, but it's too late now.
Although I'm not sure it's a good general principle, in this
case, I think it's best to align the standard with existing
practice.
There is one more piece to this puzzle. At the time where the
standards text for <iostreamwas written, essentially all code was
written using <iostreamand worked. The standard (unintentionall y, as
far as I'm concerned, and surprisingly) broke that code. By making
code using just <iostreamlega l, the standard was simply brought
*back* into line with reality.
-- Bjarne Stroustrup; http://www.research.att.com/~bs
Ron Natalie <ro*@spamcop.ne twrote:
sp********@gmai l.com wrote:
Do you want to be more specific?
>#include <cstring> #include <iostream> using namespace std;
Using declarations are generally bad.
Actually, this is a using *directive*. A using declaration would be
something like:
using std::cout;
Personally, I use using declarations in my .cpp files, but this is
largely personal preference so I will not expect to get into this
debate.
Obviously, using directives (and declarations as well) in headers are
bad, which is what I think you were trying to hint at.
--
Marcus Kwok
Replace 'invalid' with 'net' to reply This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mr Mint |
last post by:
Hi all,
I have a page named register.php, which is a form for collecting user data.
As an example:
- I have the fields Name, Surname, email.
- In the <form> tag I have action =""
- The page register.php has a condition
if (isset($_POST)) {
include('register2.php');
}
|
by: Amol Shambharkar |
last post by:
Hello Everyone,
I am hoping someone could help me out with this.I am using Visual
Studio .NET 2003 to create a web application on a remote IIS 5.0
server using the File Share web access method.The problem is I get the
following two errors everytime I try to create the application:
Error 1:
The web was created successfully, but an error occured trying to
configure the application root of this web.Web projects may not
operate correctly...
|
by: Jim |
last post by:
Hi people. I was hoping someone could help me as this is driving me up
the wall.
I'm trying to write a program that deals with matrix multiplication.
The
Program uses a couple of typedefined structure as follows:
typedef double (*Rowptr); // Holds rows of 4 doubles
|
by: John B |
last post by:
Hi,
I'm getting a file not found error - looks like the app is looking for a
file called 'fuhnxrfp.dll' or 'fuhnxrfp.exe'. Does anyone know what this
file is? I can't find a reference to it anywhere....
Here is the Assembly Load Trace:
=== Pre-bind state information ===
|
by: dbuchanan |
last post by:
Hello,
Why can't my application recognize or find the data layer. These are
the design-time and runtime errors I receive.
\\
"One or more errors encountered while loading the designer. The errors
are listed below. Some errors can be fixed by rebuilding your project,
while others may require code changes.
| |
by: genestarwing |
last post by:
QUESTION:
Write a program that opens and read a text file and records how many
times each word occurs in the file. Use a binary search tree modified
to store both a word and the number of times it occurs. After the
program has read the file, it should offer a menu with three choices.
the first is to list all the words along with the number of occurences.
The second is to let you enter a word, with the program reporting how
many times the...
|
by: Shiva48 |
last post by:
Thanks to Gannon11 and ro351988- Moderator.
I give below the complete Java file and pls help me to rectify the errors.
package com.wrox.proj2ee.ch10.app;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.wrox.proj2ee.ch12.*// The requesthandlers interface in this package.
public class ShowRecordRequesthandler implements RequestHandler
|
by: ARC |
last post by:
Just curious if anyone is having issues with Acc 2007 once the number of
objects and complexity increases? I have a fairly large app, with many
linked tables, 100's of forms, queries, reports, and lots of vba code. I'm
nearly finished with re-doing my app in access 2007, and just imported an
add-in program, which has added even more forms, queries and linked tables.
Every so often now, after opening many different screens, I'll...
|
by: CodeNoob |
last post by:
please help been working on a project got it down to 5 errors from 100 now i have no idea what to do.
Errors:
init:
deps-jar:
Created dir: C:\Users\Tommy\Desktop\build\classes
Compiling 306 source files to C:\Users\Tommy\Desktop\build\classes
C:\Users\Tommy\Desktop\PlasmaMS PET\SeanSource V.5.7\src\net\sf\odinms\provider\xmlwz\FileStoredPngMapleCanvas.java:14: warning: com.sun.imageio.plugins.png.PNGImageReaderSpi is Sun proprietary API...
|
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,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |