The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
How to handle memcpy exception?
Discuss How to handle memcpy exception? in the C Programming forum on Dev Shed. How to handle memcpy exception? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 22nd, 2003, 05:15 PM
|
|
Contributing User
|
|
Join Date: May 2001
Location: Phoenix, AZ
|
|
|
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!
|

May 22nd, 2003, 06:26 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 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.
|

May 22nd, 2003, 06:42 PM
|
 |
Banned ;)
|
|
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
|
|
|

May 22nd, 2003, 08:20 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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.
|

May 23rd, 2003, 01:42 PM
|
|
Contributing User
|
|
Join Date: May 2001
Location: Phoenix, AZ
|
|
|
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.
|

May 23rd, 2003, 02:30 PM
|
|
Contributing User
|
|
Join Date: Feb 2001
Posts: 1,365

Time spent in forums: 18 h 9 m 25 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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|