Hi all,
i didn't get output in the following code while compiling and executing
with g++ version 3.2.3 it doesn't allocate memory to pointer varaible
(x) in class B. and it gives correct output while executing in .Net and
visual slick editor. why it didn't allocate memory? if anybody knows
plz give reply.
thanks in advance
kk
------File Name: les9_5.C--------------
#include <iostream>
#include <string.h>
using namespace std;
class B{
char *x;
public:
B(char *y="xxx"):x(new char[strlen(y)+1]){
try{
// x=new char[strlen(y)+1];
if(*x)
strcpy(x,y);
else
{
cout<<"value at x\t"<<x<<endl;
cout<<y<<endl;//checkig formal
parameter
throw 1;
}
}catch(...){
cout<<"Constructor B(char *y=\"xxx\") "<<endl;
cout<<"memory allocation failure"<<endl;
}
}
B(B *objb):x(new char[strlen(objb->x)+1])
{
cout<<"constructor B(B *objb):x(new
char[strlen(objb->x)+1])"<<end*l;
if(*x)
strcpy(x,objb->x);
else
cout<<"memory allocation failure"<<endl;
}
B(const B& objb)
{
x=new char[strlen(objb.x)+1];
if(*x)
strcpy(x,objb.x);
else
cout<<"memory allocation failure"<<endl;
}
void showx()
{
cout<<"value of x from the object of class
B="<<x<<endl; }
~B(){
try{
if(*x)
delete []x;
else throw 2;
}catch(...){
cout<<"memory already deleted"<<endl;
}
}
};
class A{
char *y;
B *b;
public:
A(char *c="xxx",char *d="bbb"):y(new char[strlen(c)+1]),b(new
B(d)){
try{
// y=new char[strlen(c)+1];
// b=new B(d);
if(*y||*d)
strcpy(y,c);
else
throw 2;
}catch(...){
cout<<"memory allocation failure";
}
}
void showy()
{
cout<<"value of y from the object of class
B="<<y<<endl;
b->showx();
}
~A(){
try{
if(*y)
delete []y;
else throw 2;
/* if(*b)
delete []b;
else throw 2;
*/
}catch(...){
cout<<"memory already deleted in A's
destructor"<<endl;
}
}
};
main()
{
A a("Hello","Gates");
a.showy();
}
compiling: g++ -g -o les9_5 les9_5.C
executing: ./les9_5
output:
value at x
Gates
Constructor B(char *y="xxx")
memory allocation failure
value of y from the object of class B=Hello
value of x from the object of class B= 4 2067
* kk: Hi all, i didn't get output in the following code while compiling and executing with g++ version 3.2.3 it doesn't allocate memory to pointer varaible (x) in class B. and it gives correct output while executing in .Net and visual slick editor. why it didn't allocate memory? if anybody knows plz give reply. thanks in advance
Uhuh. This must be the worst formatted code I've ever responded to here.
Please do something about that, and DON'T post with MIME-coding. ------File Name: les9_5.C--------------
To help your tools help you, use a filename that's recognized as C++ source
code.
#include <iostream> #include <string.h> using namespace std; class B{ char *x; public: B(char *y=3D"xxx"):x(new char[strlen(y)+1]){ try{
// x=3Dnew char[strlen(y)+1];
'x' is already initialized. This new allocation means you're discarding the
pointer to the previously allocated memory, without deallocating it. You're
leaking memory.
if(*x)
Undefined Behavior.
The program can do anything here.
You're referencing memory -- what x points to -- that hasn't been
initialized.
Anyway in standard C++ the 'if' will not be executed if the allocation
fails.
If the allocation fails you get a std::bad_alloc exception, not a null
pointer.
strcpy(x,y);
Use std::string instead.
else { cout<<"value at x\t"<<x<<endl; cout<<y<<endl;//checkig formal parameter
Pass that information via the exception, e.g. a std::runtime_error, instead
of doing i/o down in your classes.
throw 1;
Use standard exception classes.
} }catch(...){ cout<<"Constructor B(char *y=3D\"xxx\") "<<endl; cout<<"memory allocation failure"<<endl;
Here you forgot to rethrow the exception, which means the calling code may
go on to use a B-object that isn't really usable.
Add:
throw;
} }
[snip] main()
'main' must have result type 'int'.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
"kk" <ki**********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hi all,
i didn't get output in the following code while compiling and executing
with g++ version 3.2.3 it doesn't allocate memory to pointer varaible
(x) in class B. and it gives correct output while executing in .Net and
visual slick editor. why it didn't allocate memory? if anybody knows
plz give reply.
thanks in advance
kk
------File Name: les9_5.C--------------
#include <iostream>
#include <string.h>
using namespace std;
class B{
char *x;
public:
B(char *y="xxx"):x(new char[strlen(y)+1]){
try{
// x=new char[strlen(y)+1];
A few things:
1) you've commented out the allocation above, and replaced it with an
initializer list. Why put the "new" in the initializer, but keep this
useless code below which checks if x is allocated or not? In a conforming
compiler, new throws an exception if it fails. Why do you want all this
code below? If the exception happens, it will happen in the initializer
list above, before you enter the try, where you actually _could_ catch it.
2) Why are you BOTH checking if the pointer is nil AND trying
(unsuccessfully) to catch the allocation exception? Remove the if statement
test.
3) What if y is nil? Your code should probably be testing if y is nil (not
if x is nil), and if it's not, THEN allocate memory for x and copy y to it.
(Otherswise I assume x should be nil?)
4) Suppose in your code that you did catch an exception somehow. You're
allowing the constructor to continue, reporting via cout that an error
occurred. But there's still a problem, in that x was never properly
allocated. How's the rest of your program going to like that? If you want
to report an exception via cout, that's fine, but you need to be sure you
either re-throw the exception or else take some appropriate action to make
sure your object is in a viable state.
if(*x)
strcpy(x,y);
else
{
cout<<"value at x\t"<<x<<endl;
cout<<y<<endl;//checkig formal
parameter
throw 1;
}
}catch(...){
cout<<"Constructor B(char *y=\"xxx\") "<<endl;
cout<<"memory allocation failure"<<endl;
}
}
-Howard
kk wrote: class B{ char *x; public: B(char *y="xxx"):x(new char[strlen(y)+1]){ try{
if(*x)
*x doesn't have a deterministic value. Even if the language
default initialized it, *x would be 0. I suspect you really
were trying to see do
if(x)
However, there is no way on a conforming compiler that the
value of your new expression would have returned a null
pointer. New as you have written it should throw bad_alloc
on failure.
Ron Natalie wrote: New as you have written it should throw bad_alloc on failure.
I suspect OP is using VC6, based on an earlier discussion thread. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: hall |
last post by:
I have a question regarding where memory is allocated when arrays are
created. I'll illustrate this by example. I may be wrong on some
details, do feel free to correct me.
The code piece:
int...
|
by: Tron Thomas |
last post by:
As part of applying for a programming position at a company, I
recently I had submitted some code samples to one of the developers
for review.
This is the feedback I received:
One of his...
|
by: John |
last post by:
Hi all:
When I run my code, I find that the memory that the code uses keeps
increasing.
I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
memory by the time it finishes...
|
by: xixi |
last post by:
hi, we are using db2 udb v8.1 for windows, i have changed the buffer
pool size to accommadate better performance, say size 200000, if i
have multiple connection to the same database from...
|
by: Alessandro Monopoli |
last post by:
Hi all,
I'm searching a PORTABLE way to get the available and total physical memory.
Something like "getTotalMemory" and it returns the memory installed on my PC
in bytes, and...
|
by: eyh5 |
last post by:
Hi,
My C code (running on Soalris Unix) has some "segmentation fault"
that I wish to use purify to do it. I poked around the web, and found
some information about adding some lines in a Makefile...
|
by: Hermann Maier |
last post by:
hi,
i need to find out the memory usage of a specific function that i use in
my program. this function does some recursive calculations and i want my
program to display the amount of memory the...
|
by: ballpointpenthief |
last post by:
If I have malloc()'ed a pointer and want to read from it as if it were
an array, I need to know that I won't be reading past the last index.
If this is a pointer to a pointer, a common technique...
|
by: smarty |
last post by:
how can I find the memory allocated dynamically? is there any
possibility of finding it?
|
by: arunajob |
last post by:
Hi all,
If I have a piece of code something like this
void main(void)
{
char * p1="abcdefghijklmn";
.............................................
}
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: 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: 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,...
|
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: 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...
|
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,...
| |