C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 22nd, 2003, 05:15 PM
noslenj123 noslenj123 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Phoenix, AZ
Posts: 484 noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 22 h 12 m 17 sec
Reputation Power: 35
How to handle memcpy exception?

Newb here, proceed with caution!

In some code on a server where I work, we found that an exception occurs on a line with memcpy. The reason is that the source buffer (when all the stars were aligned just right) had FAR more characters in it than the destination buffer had allocated. So it overwrote something in memory and the application just disappeared with no indication why.

So obviously the lengths should be checked for feasability before attempting the memcpy. However, this was inside a __try __except block but it doesn't seem to have been trapped.

There must be a way to trap for this kind of exception. Where would I go to learn how to set up some better exception handling? Books, on line resources, etc.

Tx!
__________________
There are only 10 kinds of people in this world. Those who understand binary, and those who don't!

Reply With Quote
  #2  
Old May 22nd, 2003, 06:26 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 20 m 28 sec
Reputation Power: 14
Hi,

A try block throws a certain type of exception, and the exception catch block must be defined to catch that type. If the catch block does not have a parameter that matches the type of what was thrown, then the catch block is skipped. If you want, you can change the parameter of the catch block to an all encompassing parameter:

catch(...)
{
}

will catch any exception thrown. Or, you can write an additional catch block yourself, which catches the specific exception you saw occur. You do that just like you would write a function:
Code:
try
{
    if(strlen(to_be_copied) > declared_length)
       throw "This is that damn error!"
}

catch(const char* message)
{
    cout<<message<<endl;
}




I would proceed with the second option, so you don't mess up any catch block system in place that routes exceptions to different catch blocks. If an exception thrown in a try block is not caught by any catch blocks, then the standard library function terminate() is called. That function calls a predefined default terminate handler, which in turn calls the standard library function abort(). You're able to redefine the default action of the default terminate handler to do a few things before it calls abort() if you need to.

Books: "Ivor Horton's Beginning C++", Chapter 17: Program Errors and Exception Handling. It's a great reference and tutorial, so it's a good book to have.

If you want more help on determining what type of exception is thrown, post the try block and put a notation by the line that does the throwing, and we can help you write a catch block.

Last edited by 7stud : May 22nd, 2003 at 08:30 PM.

Reply With Quote
  #3  
Old May 22nd, 2003, 06:42 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,402 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 8 h 53 m 6 sec
Reputation Power: 4080

Reply With Quote
  #4  
Old May 22nd, 2003, 08:20 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,252 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 19 h 13 m 3 sec
Reputation Power: 1985
Uh, wait a minute. When you call memcpy, you specify the number of bytes to be copied. After you have allocated the destination buffer. You will be in a position to know both the size of the destination buffer and the number of bytes that you will be copying to it.

That's no exception; that's a programming error. Correct the error and recompile.


In school, our IBM S/370 documentation had a thick manual listing the system error messages. The cause of the error and the corrective action seemed to always be the same: programming error; correct the code and resubmit the job.

Reply With Quote
  #5  
Old May 23rd, 2003, 01:42 PM
noslenj123 noslenj123 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2001
Location: Phoenix, AZ
Posts: 484 noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level)noslenj123 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 22 h 12 m 17 sec
Reputation Power: 35
7stud,

Thanks, I'll play with that. I have Ivor's "Beginning Visual C++" and I'll look for and read the exception chapter asap.

Scorp, wow, that one will require some study. Thanks!

dwise, right, it's a programming error. In fact it's a logic error. Either way it's an opportunity to learn how to properly trap for errors. Remember, this is not my code and i'm learning c++ but am fluent in several other languages. I can always correct the logic and recompile. In fact, I can do that until the cows come home. But, since programmers are never perfect, code should always trap for problems. In this particular case, the server should have notified us of an issue rather than just disappearing from existance. Doh!

Thanks guys! Of course we have corrected that logic problem, but I will learn how to better trap for exceptions and instute that into the existing code.

Reply With Quote
  #6  
Old May 23rd, 2003, 02:30 PM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 20 m 28 sec
Reputation Power: 14
"Thanks, I'll play with that. I have Ivor's "Beginning Visual C++" and I'll look for and read the exception chapter asap."

That's not the same book. Out of necessity, the C++ chapters in "Visual C++" are very cursory. Check p. 213 in that book for a short discussion on exceptions.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How to handle memcpy exception?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap