473,386 Members | 1,698 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,386 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 4921
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...
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: 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...
0
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...
0
BarryA
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...
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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 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.