|
Make at least sure you don't have any optimizing turned on. It might remove your entire code block. Step through the code in the disassembly window, instruction by instruction, and inspect what happens and make sure all the code you expect is there.
|
|
|
|
|
That'll work if you're using managed code as that exception comes from the .NET runtime. However as you originally said you want to catch a CException then you're using MFC which (AFAIK, I may be misinformed) doesn't play nicely with .NET and managed code.
Cheers,
Ash
|
|
|
|
|
Karthika85 wrote: we can catch divide by zero error. please chk this link
I did; did you check the note that states: Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample.
This example is for C++.NET and will only work in managed code, you are trying to use CException[^] which is only for MFC exceptions.
It's time for a new signature.
|
|
|
|
|
you also could write such methords or classes which handles such cases and than throws exception.
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
There isn't a C++ exception for what you want to catch. You have to use an operating specific method to trap that sort of error. You can convert the operating system error into a C++ exception if the mechanism allows it but it won't happen by default.
As an example you can use structured exception handling (SEH) (the OS specific thing that happens on a divide by zero) on windows to throw a C++ exception.
Cheers,
Ash
|
|
|
|
|
As Ash said, the division by zero is not a C++ exception, so you should use platform specific API to deal with it and translate it to a C++ exception.
Read this from the MSDN: _set_se_translator (CRT)[^] (it requires to compile with the right flags to make it work properly).
Another way to achieve your goal is to use the __try /__except statements (see try-except Statement (C++)[^]).
|
|
|
|
|
No exceptions will be thrown in your code.I would recommend your are targeting VC10 and you don't need the best performance possible consider using SafeInt class.It automatically checks for overflows and attempts to divide by zero.
#include <safeint.h>
...
using namespace msl::utilities;
try{
SafeInt<int> i = 10;
SafeInt<int> j = 0;
SafeInt<int> k = i/j;
}
catch(SafeIntException& e)
{
switch(e.m_code)
{
case SafeIntDivideByZero:
AfxMessageBox(L"Devide by zero occured!");
break;
case SafeIntArithmeticOverflow:
AfxMessageBox(L"Overflow error occured!");
break;
case SafeIntNoError:
AfxMessageBox(L"Other error occured!");
break;
}
}
Life is a stage and we are all actors!
|
|
|
|
|
Hi all,
i am reading a file with help of CStdioFile using while loop and read the file till file reached end of file,in the file somewhere symbol exist,when reading this line with ReadString ,while loop becomes infinite loop and not return and application goes in notresponding state.
please tell me how can i read the full file with this symbol or how can i remove this symbol from file before reading the file.
thanks in advance.
|
|
|
|
|
Please post some relevant code.
|
|
|
|
|
CStdioFile file;
CString str="";
file.Open(file_name,CFile::modeRead,0);
while(file.end)
{
file.ReadString(str);
}
file.Close();
|
|
|
|
|
Le@rner wrote: while(file.end)
I don't know where the "end" is coming from (I never used CStdioFile in fact), but if you check the documentation of ReadString[^], you can see that the function returns FALSE when the end of file was reached. You should try to use that.
|
|
|
|
|
if i do this ,function not read the full file and return when this symbol occur becoz here ReadFile return 0,but i want to read full file data.
|
|
|
|
|
Sorry, your sentence is not clear at all .
There's no ReadFile function, I guess you are talking about ReadString ? Furthermore, the function doesn't read the complete file but only one line, so it is normal that the function return before the end of the file. You have to call it repeatedly until the function returns FALSE.
|
|
|
|
|
There are several issues with what is going on here..
After looking the code its important to note the file.end is not doing what you want it to do. If you look at the definition of the data member it is returning an enumeration to the end of the file. It is not whether or not you are currently at the EOF.
Also the methods you are using STOP on a EOF character because you are reading in the default text mode.
You should be opening the file in binary read mode " CFile::typeBinary".
Because of the special characters I would suggest that you tseek the end of file get the length of the file and then do a for loop to retrieve the data out of the file.
|
|
|
|
|
Dear SnowHow,
i have observe same result.
|
|
|
|
|
Hi All,i want to capture mouse move over a CButton(Button Controls) in a dialog.Whenever i move mouse over a button i want to change the button properties eg : button color, button picture(Bitmap). Please any one have any idias or any examples, which helps me.
Thanks in Advance
|
|
|
|
|
hapyy coding with
TRACKMOUSEEVENT tme;//class member !!!
tme.cbSize = sizeof(tme);
tme.dwFlags = TME_LEAVE|TME_HOVER;
tme.dwHoverTime = 1;
_TrackMouseEvent(&tme);//
and
ON_MESSAGE(WM_MOUSELEAVE, OnMouseLeave)
ON_MESSAGE( WM_MOUSEHOVER, OnMouseHover )
Press F1 for help or google it.
Greetings from Germany
|
|
|
|
|
I am working on a very large project and would like to know if their is a rule of thumb for the proper sequence for doing #includes.
I have always put my #include's in headers and done no #includes in any of my .cpp files. When my projects get large and I use a lot of the same included files I put a master header and include it in every other header.
For example:
Utils.h
#include <io.h>
#include <pgmspace.h>
#include <delay.h>
-Other helper functions that are
useful such as a custom printf()
Display.h
#include "Utils.h"
#include "ctime.h"
Display.cpp
#include Display.h
The reason I am asking is my target is VERY memory limited and I need to get things as small as possible so useless inclusions that and bulk I need to get rid of. My other method of doing things is to only include exactly what that .h/.cpp set requires and use a Globals.h with external decelerations for all cross file variables and definitions.
I still do not understand how the linker pieces objects together when their are unused functions. I would imagine that the compiler runs through your code and only generates asm for the functions and variables you use. If their is a function that uses a 10x32bit array but your not using it the compiler skips it? Then in the linker stage if it notices their are duplicate functions in each of the objects it strips out the others and only includes one? If this is the case then my above question is just semantics and I should not worry and do what is easiest to maintain and cleanest.
|
|
|
|
|
One way to make sure you include only required headers. is by using #define macros.
|
|
|
|
|
I already use the #ifndef/#define inclusion guards. If you have a function in a say 'abs.h/abs.cpp' and include it in 'part1.cpp' and 'part2.cpp' and use it in each part upon compiling 'part1.o' and 'part2.o' will each have that function. When the linker combines 'part1.o & part2.o into final.exe' will the function still be included twice or does it weed out duplicates and just point them all to one?
|
|
|
|
|
You're actually doing things a bit backwards - in C and C++ the general rule is to only include the minimum number of other files in headers. You only want declarations included that are actually going to be used by clients of that header. While C isn't as bad as C++ I've seen large (250kloc) projects have their build times cut by a factor of 100 by using these techniques. It also avoids dependency fear - "aaarggghh, I can't change this header as absolutely everything includes it directly or indirectly and everything will build, bollocks, I'll just hack something in this C file to get 'round it..."
Interestingly and generally (this depends on your compiler, most I use work this way) what you include doesn't usually have much effect on the code size. Declarations and macros don't occupy space in an object file - it's definitions that do. Exactly what the linker does depends on the linker (and how the compiler packages functions) but most modern linkers remove multiple definitions of objects with the same name. However some require command line switches to turn this on so it's a good idea to look at your compiler and linker docs and find out what all the switches do. One good rule to follow is that if your compiler is a C++ compiler it'll support multiple definition removal (saves the embarrassment of template explosion on code size), if it's just a C compiler then it may take some fiddling to do so.
Cheers,
Ash
|
|
|
|
|
I think I'm picking up what your throwing down now. For code size if you included every single c library header but didn't use a single function they provided the size of the code would not go up with every additional inclusion but because the compiler must do its job on every included file the compile time will increase will every include.
So for my projects I should only include the minimum amount of headers for each file to keep things compiling as fast as possible and make maintenance easier.
Originally my layout was:
#include <stdio.h>
#include <stdints.h>
#include <ctype.h>
#include <string.h>
#include "Utils.h"
#include "ctime.h"
#include "Utils.h"
And I should get in the habit of this way:
#include <stdints.h>
#include <stdio.h>
#include <stdints.h>
#include "Utils.h"
#include "ctime.h"
#include <stdints.h>
#include <ctype.h>
#include <string.h>
#include "Utils.h"
|
|
|
|
|
Hey, this is my code:
char szBuffer[1024];
recv(sock,szBuffer,sizeof(szBuffer),NULL);
printf("%s", szBuffer);
now the size of the websites vary, how would i allocate just enough memory for the actual content so the rest of the buffer wont be filled with junk ?
Thanks.
|
|
|
|
|
Good question ... without an answer.
The physics has its limits, and computer aren't paragnostic.
You have only two chances: know the size of what you are receiving in advance (and allocate the required) or, "guess it" and store as you can. You may waste something, or you may be required to relocate.
In both of the cases, dynamic allocation must be used (so an on-stack array cannot help).
There are dynamic memory allocation operations (search for "new and delete") or dynamic allocating classes (see std::vector).
Search for their documentation.
2 bugs found.
> recompile ...
65534 bugs found.
|
|
|
|
|
Depending on how you are getting the content the answer is different. Reading from a file is simple.
But, if you are getting the website content from a web server then all you have to do is parse the HTTP return headers for the Content-Length header option. This will give you the size of the buffer that is required. From here you can make the new buffer and store the rest of the web page into the newly allocated buffer, or you can take the size and resend the same request after finding out the required buffer size. Skipping the headers by looking for \r\n\r\n in the buffer and once this is found to begin storing the website contents.
So, in both cases you need to dynamically allocate the buffer as stated before, but it all depends on how you are reading the website content, from file or from a web server.
|
|
|
|