473,385 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

throws different exceptions

Hello

When compiling the following example

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib> //declarations of malloc and free
  2. #include <new>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. class C {
  7. public:
  8.   C(); 
  9.   void* operator new (size_t size); //implicitly declared as a static member function
  10.   void operator delete (void *p); //implicitly declared as a static member function
  11. };
  12.  
  13.   void* C::operator new (size_t  size) throw (const char *){
  14.   void * p = malloc(size);
  15.   if (p == 0)  throw "allocation failure";  //instead of std::bad_alloc
  16.   return p; 
  17. }
  18.  
  19. void C::operator delete (void *p){
  20.   C* pc = static_cast<C*>(p); 
  21.   free(p);
  22. }
  23.  
  24. int main() { 
  25.    C *p = new C; // calls C::new
  26.    delete p;  // calls C::delete
  27. }
Expand|Select|Wrap|Line Numbers
  1.  g++ -Wall example.cpp -o example
  2. example.cpp:14: error: declaration of 'static void* C::operator new(size_t) throw (const char*)' throws different exceptions
  3. example.cpp:9: error: from previous declaration 'static void* C::operator new(size_t)'
  4. example.cpp: In static member function 'static void C::operator delete(void*)':
  5. example.cpp:21: warning: unused variable 'pc'

It something wrong with new operator overloading ?
What's this mean
... operator new(size_t) throw (const char*)' throws different exceptions
Feb 15 '08 #1
4 4920
arnaudk
424 256MB
For one thing,
throw ("allocation failure")
instead of:
throw "allocation failure"
and please enclose your code between [CODE=cpp] ... [/code] to make it much easier to read for the rest of us.
Feb 15 '08 #2
Ganon11
3,652 Expert 2GB
Why, why, why are you using malloc and free in a C++ environment, especially with classes? That's what new and delete are for. Using malloc only gets you a chunk of memory, but new calls the constructor of the object. What possible purpose could you have for using these C functions in C++?

Also: Please don't double post your question. Having two threads doesn't help you - it just confuses the people trying to help you.
Feb 15 '08 #3
I am interrested to understand new/delete operators overloading and actually this code is taken from a tutorial. I am still learning :) ...

I have a question:
new operator can be used to allocate a void * pointer ?
Something like that
Expand|Select|Wrap|Line Numbers
  1. void *v;
  2. v=new (1024);//v=malloc(1024) works fine
  3.  
won't be compilled.

For one thing,
throw ("allocation failure")
instead of:
throw "allocation failure"
and please enclose your code between [CODE=cpp] ... [/code] to make it much easier to read for the rest of us.

Nothing changed even if instead of
Expand|Select|Wrap|Line Numbers
  1. throw "allocation failure" 
  2.  
is used
Expand|Select|Wrap|Line Numbers
  1. throw ("allocation failure")
  2.  
Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib> //declarations of malloc and free
  2. #include <new>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. class C {
  7. public:
  8.     C(); 
  9.     void* operator new (size_t size) throw (const char *); //implicitly declared as a static member function
  10.     void operator delete (void *p); //implicitly declared as a static member function
  11. };
  12.  
  13. void* C::operator new (size_t  size) throw (const char *)
  14. {
  15.     void * p = malloc(size);
  16.     if (p == 0)
  17.         throw ("allocation failure") ;  //instead of std::bad_alloc
  18.     return p; 
  19. }
  20.  
  21. void C::operator delete (void *p){    
  22.     C* pc = static_cast<C*>(p); 
  23.     free(p);    
  24. }
  25.  
  26. int main() { 
  27.     C *p = new C; // calls C::new
  28.     delete p;  // calls C::delete
  29. }
  30.  

Expand|Select|Wrap|Line Numbers
  1. $g++ -g3 -Wall example.cpp -o example
  2.  
  3. example.cpp: In static member function 'static void C::operator delete(void*)':
  4. example.cpp:22: warning: unused variable 'pc'
  5. /tmp/cc9LdSLe.o: In function `main':
  6. example.cpp:27: undefined reference to `C::C()'
  7. collect2: ld returned 1 exit status
  8.  
But, oops ! Constructor body is missing.

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib> //declarations of malloc and free
  2. #include <new>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. class C {
  7. public:
  8.     C(){}; 
  9.     void* operator new (size_t size) throw (const char *); //implicitly declared as a static member function
  10.     void operator delete (void *p); //implicitly declared as a static member function
  11. };
  12.  
  13. void* C::operator new (size_t  size) throw (const char *)
  14. {
  15.     void * p = malloc(size);
  16.     if (p == 0)
  17.         throw "allocation failure" ;  //instead of std::bad_alloc
  18.     return p; 
  19. }
  20.  
  21. void C::operator delete (void *p){    
  22.     C* pc = static_cast<C*>(p); 
  23.     free(p);    
  24. }
  25.  
  26. int main() { 
  27.     C *p = new C; // calls C::new
  28.     delete p;  // calls C::delete
  29. }
  30.  
Now everything is compiled OK, without errors.
Feb 15 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
This is a rotten example. You reallty need a better tutorial. One that has no malloc() and no type casts.
Feb 15 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Piotr Bieniek | last post by:
Hello, I have a problem with UDP sockets. It concerns UdpClient class as well. It throws strange exceptions on subsequent Send calls. Exception is SocketException with native error code 10049. I...
1
by: Ian Lazarus | last post by:
Hello, How do I analyze my code to determine which routines throw? Are there tools which automate this? Manual inspection is prone to error. Thanks
0
by: Mike Schilling | last post by:
I have some code that calls methods reflectively (the method called and its parameters are determined by text received in a SOAP message, and I construct a map from strings to MethodInfos). The...
11
by: vijaynats | last post by:
Why isn't there a 'throws' keyword in C# like java - i would like to declare a function and say - public int addup(int a, int b) throws ArithmeticExceptio, DivideByZeroException { ... ... }
1
by: Martin | last post by:
Hi. I've found a few topics on this subject, but still not sure and decided to post mine. Here's my example: #include <iostream> using namespace std;
4
by: indrawati.yahya | last post by:
According to the FAQ, the best way to inform a class user of an error that occurs inside a constructor is to throw an exception. My question is, what happens when an object is instantiated using...
6
by: Emre DİNÇER | last post by:
does c# provide an alternative to the throws keyword in javA?so that my client developer will strictly implement the exceptions that my methods may throw? thanks in advance
0
by: JohnC | last post by:
Hello people... Just for clarification, I'm a C++ newbie, and not really into programming in C++. I was trying to get an open source C++ program to compile (the author didn't respond yet) and...
6
by: =?Utf-8?B?QnJpYW4gTmljaG9sc29u?= | last post by:
How do I know which exceptions a function throws? In my application, I make a call to DirectoryInfo.GetDirectories("\\NetworkPath\Path"). I check for System.IO.DirectoryNotFoundException and...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.