|
Hello,
I need to multiply integer value with a factor and want to do an overflow check with INT_MAX, INT_MIN for this.
Integer contains 4 bytes on my machine an this is the max variable-size.
I need to do this check for signed integer and unsigned integer. Only integer math/variables allowed.
The factor always comes as fraction with a nominator and denominator. The denominator always is 10
Factor might be greater 1 or less than 1
So for example fraction is 25/10, varible a is signed integer
a = (a * 25)/10 -> may cause overflow, because (a * 25) can be > INT_MAX ( or < INT_MIN )
Overflow checks:
if (a > (INT_MAX * 10)/25 -> not possible because of overflow, INT_MAX is the maximum number the compiler can store.
if (a > INT_MAX * (10/25) -> not possible because 10/25 is 0.4 which will be 0 in integer math.
So any idea how to solve my problem?
Thanks for any hint!
|
|
|
|
|
One way would be to perform the multiplication as a double-precision operation, and then examine the high word. For unsigned values (in pseudocode):
// HI_HALF - returns the upper N/2 bits of an N-bit integer
// LO_HALF - returns the lower N/2 bits of an N-bit integer
// MAKE_DIGIT - combines two N/2-bit values to make an N-bit integer
// multiply a DIGIT a by a DIGIT b, returning a two-DIGIT result
// a, b must be unsigned
DIGIT alow = LO_HALF(a)
DIGIT ahigh = HI_HALF(a)
DIGIT blow = LO_HALF(b)
DIGIT bhigh = HI_HALF(b)
DIGIT carry
DIGIT accumulator
DIGIT result[4]
accumulator = alow * blow
carry = HI_HALF(accumulator)
result[0] = LO_HALF(accumulator)
accumulator = alow * bhigh + carry
result[1] = LO_HALF(accumulator)
result[2] = HI_HALF(accumulator)
accumulator = ahigh * blow + result[1]
result[1] = LO_HALF(accumulator)
carry = HI_HALF(accumulator)
accumulator = ahigh * bhigh + result[2] + carry
result[2] = LO_HALF(accumulator)
result[3] = HI_HALF(accumulator)
// combine result[3] and result[2] into 1 DIGIT
// combine result[1] and result[0] into 1 DIGIT
return MAKE_DIGIT(result[3], result[2]), MAKE_DIGIT(result[1], result[0])
Note that many optimizations may be performed on the above code; it is laid out like this for easy comprehension.
For the operation a * b, if the high digit is non-zero, the result has overflowed
For the operation a * b / c, if the high DIGIT of (a * b) >= c, then the entire operation will overflow.
Signed values are left as an exercise for the student.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
All that matters is if a * factor overflows, so the /10 can be ignored. I believe you just need to check if (a <= (INTMAX / factor)).
|
|
|
|
|
It will take more effort and cpu cycles to pick off the overflow than simply move it to next size up integer, do the mult on a that type and look at the result for overflow
So for 16 bit int and 32bit long as an example
long b = a;
b *=25
b /=10
if (b & 0x7FFF0000 != 0)
{
}
a = (int) b;
If you want to do it proper anal uses standard ints
#include <stdint.h>
int16_t a = ????;
int32_t b = a;
b *=25
b /=10
if (b & 0x7FFF0000 != 0)
{
}
a = (int16_t) b;
In vino veritas
modified 10-Jul-19 9:41am.
|
|
|
|
|
Nice.
|
|
|
|
|
hi I m getting this error any suggestion:
AccessibilityObject = 'object.AccessibilityObject' threw an exception of type 'System::Runtime::InteropServices::COMException^'
|
|
|
|
|
You don't have privilege to run it. Log in or run the program as administrator.
In vino veritas
|
|
|
|
|
Update
I linked only part of the /lib folder , my mistake.
Still like to know if my original question - linking to network resource is feasible.
This is basically result of my inability to ".configure" for non - native architecture.
The "configure" kept asking for dependency after dependency.
So I ended up running .configure on required architecture.
After a detour I am back to tying to implement / crosscompile C++ code.
I can option a prefix for desired architecture , but still having an issue linking to specific desired architecture libraries.
I was unable to use ".configure " to create correct foreign architecture library on "local" architecture.
I did a hack by copying an entire "foreign" folder to local device and was partially successful linking to it.
Still missing some library and could do same hack.
Is there a more "elegant" way to accomplish that?
Here is what is still failing to link correctly.
Sorry for the mess.
gcc version 5.4.0 20160609 (Ubuntu/Linaro 5.4.0-6ubuntu1~16.04.9)
COMPILER_PATH=/usr/lib/gcc-cross/arm-linux-gnueabihf/5/:/usr/lib/gcc-cross/arm-linux-gnueabihf/5/:/usr/lib/gcc-cross/arm-linux-gnueabihf/:/usr/lib/gcc-cross/arm-linux-gnueabihf/5/:/usr/lib/gcc-cross/arm-linux-gnueabihf/:/usr/lib/gcc-cross/arm-linux-gnueabihf/5/../../../../arm-linux-gnueabihf/bin/
LIBRARY_PATH=/usr/lib/gcc-cross/arm-linux-gnueabihf/5/:/usr/lib/gcc-cross/arm-linux-gnueabihf/5/../../../../arm-linux-gnueabihf/lib/../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc-cross/arm-linux-gnueabihf/5/../../../../arm-linux-gnueabihf/lib/:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-L/media/z/KINGSTON/BULEZ_LIB_RPI' '-L/usr/arm-linux-gnueabihf/lib' '-L/media/z/DEV/BLUEZ/BLUEZ_LIB' '-v' '-o' 'VNAR_1971_JULY' '-shared-libgcc' '-march=armv7-a' '-mfloat-abi=hard' '-mfpu=vfpv3-d16' '-mthumb' '-mtls-dialect=gnu'
/usr/lib/gcc-cross/arm-linux-gnueabihf/5/collect2 -plugin /usr/lib/gcc-cross/arm-linux-gnueabihf/5/liblto_plugin.so -plugin-opt=/usr/lib/gcc-cross/arm-linux-gnueabihf/5/lto-wrapper -plugin-opt=-fresolution=/tmp/ccUbRWT4.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --sysroot=/ --build-id --eh-frame-hdr -dynamic-linker /lib/ld-linux-armhf.so.3 -X --hash-style=gnu --as-needed -m armelf_linux_eabi -z relro -o VNAR_1971_JULY /usr/lib/gcc-cross/arm-linux-gnueabihf/5/../../../../arm-linux-gnueabihf/lib/../lib/crt1.o /usr/lib/gcc-cross/arm-linux-gnueabihf/5/../../../../arm-linux-gnueabihf/lib/../lib/crti.o /usr/lib/gcc-cross/arm-linux-gnueabihf/5/crtbegin.o -L/media/z/KINGSTON/BULEZ_LIB_RPI -L/usr/arm-linux-gnueabihf/lib -L/media/z/DEV/BLUEZ/BLUEZ_LIB -L/usr/lib/gcc-cross/arm-linux-gnueabihf/5 -L/usr/lib/gcc-cross/arm-linux-gnueabihf/5/../../../../arm-linux-gnueabihf/lib/../lib -L/lib/../lib -L/usr/lib/../lib -L/usr/lib/gcc-cross/arm-linux-gnueabihf/5/../../../../arm-linux-gnueabihf/lib ./src/MODULES/SOURCE_CODE/src/ESA_BASE_BAD.o ./src/MODULES/M_WIRE/CLASSWIRE.o ./src/MODULES/M_VNAR/CAD8302Detector.o ./src/MODULES/M_VNAR/C_DirectionalCoupler.o ./src/MODULES/M_VNA/CVNA.o ./src/MODULES/M_UTILITY/CUtility.o ./src/MODULES/M_TFT/CTEMPLATECLASS.o ./src/MODULES/M_TEMPLATE/CParameter.o ./src/MODULES/M_TEMPLATE/CParameter_test.o ./src/MODULES/M_TCP_IP/CTCPIPSERVER.o ./src/MODULES/M_TCP_IP/CTCPITCLIENT.o ./src/MODULES/M_SPI_TEST/CLASSSPITEST.o ./src/MODULES/M_SPI_LCM1602/C_TEMP_SPI.o ./src/MODULES/M_SPI_LCM1602/SAMPLE_CODE.o ./src/MODULES/M_SPI/CLASSFBDRIVER.o ./src/MODULES/M_SPI/CLASS_SPI.o ./src/MODULES/M_SPI/CLASS_SPI_BAD.o ./src/MODULES/M_SPI/C_FB.o ./src/MODULES/M_SPI/C_SPI.o ./src/MODULES/M_SPI/C_TFT.o ./src/MODULES/M_SPI/_touch.o ./src/MODULES/M_PCF8574/CLASSPCF8574.o ./src/MODULES/M_LCM1602_I2C/CLASSLCM1602.o ./src/MODULES/M_IOCTL/CIOCTL.o ./src/MODULES/M_IOCTL/CIOCTLGPIO.o ./src/MODULES/M_IOCTL/CIOCTLSPI.o ./src/MODULES/M_IOCTL/CLASSI2C.o ./src/MODULES/M_ILI9340_SPI_TFT/ili9340spi_rpi-master/demo.o ./src/MODULES/M_ILI9340_SPI_TFT/ili9340spi_rpi-master/fontx.o ./src/MODULES/M_ILI9340_SPI_TFT/ili9340spi_rpi-master/ili9340.o ./src/MODULES/M_ILI9340_SPI_TFT/ili9340spi_rpi-master/touch.o ./src/MODULES/M_ILI9340_SPI_TFT/ili9340spi_rpi-master/xpt.o ./src/MODULES/M_ILI9340_SPI_TFT/ili9340spi_rpi-master/xpt2046.o ./src/MODULES/M_ILI9340_SPI_TFT/CILI9340SPITFT.o ./src/MODULES/M_I2CIO/CLASSI2CIO.o ./src/MODULES/M_DDS_60/CDDS60.o ./src/MODULES/M_BLUETOOTH/CBT.o ./src/MODULES/M_BLUETOOTH/CBTTOOLS.o ./src/MODULES/M_BLUETOOTH/CDBUS.o ./src/MODULES/M_BLUETOOTH/CHCI.o ./src/MODULES/M_BCM2835_SPI_TFT/CBCM2835SPITFT.o ./src/MODULES/M_BCM2835_SPI_TFT/CBCMGPIO.o ./src/MODULES/M_BCM/CBCM.o ./src/MODULES/M_BASE_TEST/CBASE.o ./src/MODULES/M_BASE_TEST/CDEVICE.o ./src/MODULES/M_BASE_TEST/CINHER.o ./src/MODULES/M_ADS1115/CADS1115.o ./src/MODULES/M_ADA_ILI9341/ADAPORTILI9341.o ./src/MODULES/M_ADA/CADAILI9341.o ./src/MODULES/M_1602_HPP/M_1602_HPP.o ./src/MODULES/MODULE_SPI_DRIVER/CSPIDRIVER.o ./src/MODULES/MODULE_SPI/CSPI.o ./src/MODULES/MODULE_MAP_GPIO/CMAPGPIO.o ./src/MODULES/MODULE_INHERITED_GPIO_MAP/INHERITANCEBASE.o ./src/MODULES/MODULE_INHERITED_GPIO_MAP/INHERITANCEDERIVED.o ./src/MODULES/MODULE_INHERITED_GPIO_MAP/MODULEINHERITEDGPIOMAP.o ./src/MODULES/MODULE_I2C/CI2C.o ./src/MODULES/MODULE_GPIO/CGPIO.o ./src/MODULES/MODULE_BASE_GPIO_MAP/MODULEBASEGPIOMAP.o ./src/MODULES/MODULE_1602/C_1602.o ./src/MODULES/MODULE_1602/C_LCD2_CPP.o ./src/MODULES/MODULE_1602/C_SPI.o ./src/MODULES/MODULE_1602/C_SPI_LCD.o ./src/MODULES/MODULE_1602/C_SSP.o ./src/MODULES/MODULE_1602/C_gpio.o ./src/VNA_1022_BASE.o -lbluetooth -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc-cross/arm-linux-gnueabihf/5/crtend.o /usr/lib/gcc-cross/arm-linux-gnueabihf/5/../../../../arm-linux-gnueabihf/lib/../lib/crtn.o
/usr/lib/gcc-cross/arm-linux-gnueabihf/5/../../../../arm-linux-gnueabihf/bin/ld: cannot find /lib/arm-linux-gnueabihf/libc.so.6
/usr/lib/gcc-cross/arm-linux-gnueabihf/5/../../../../arm-linux-gnueabihf/bin/ld: cannot find /usr/lib/arm-linux-gnueabihf/libc_nonshared.a
/usr/lib/gcc-cross/arm-linux-gnueabihf/5/../../../../arm-linux-gnueabihf/bin/ld: cannot find /lib/arm-linux-gnueabihf/ld-linux-armhf.so.3
collect2: error: ld returned 1 exit status
Ideally to be able to option crosscomplier to use "foreign" links.
Would something like adding another prefix to the "_L" option work ?
modified 5-Jul-19 12:37pm.
|
|
|
|
|
Look at the error messages; there appear to be some files missing from the default location. You need to find out where they are stored and add that path to the linker options.
|
|
|
|
|
Do your standard apt-get update and install on libc somethings missing.
In vino veritas
|
|
|
|
|
System.Windows.Markup.XamlParseException: ''The invocation of the constructor on type 'CommonXFS.Interface.Utility.MainWindow' that matches the specified binding constraints threw an exception.' Line number '5' and line position '9'.'
InnerException:
FileNotFoundException: Could not load file or assembly 'Interop.NXCameraXLib.1.0, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
-----------------------------------------------------------------------------------------------------
Note I have tried both (x86) and (x64)
My DLl is not able to load as "Interop.NXCameraXLib.1.0 "(file from OCX adeed as reference) is not loading properly.
My Query !: Do we have any way to create an managed code(C++/CLI dll which don't need any dependency from any 3rd party if yes please share an example.
2) is there any way to sesolve siscussed error.
|
|
|
|
|
Hi
IS there a way to return from DoModal without having a PUSH BUTTON with the value of IDOK ,IDCACEL or IDABORT I would like to have 3 radio buttons specifying 3 choice and return the value of one of those to DoModal
thanks
|
|
|
|
|
ForNow wrote: IS there a way to return from DoModal without having a PUSH BUTTON with the value of IDOK ,IDCACEL or IDABORT
Yes. Just press the Enter button.
|
|
|
|
|
Hi,
ForNow wrote: Hi
Is there a way to return from DoModal without having a PUSH BUTTON with the value of IDOK?
Yes.
You would need to call the EndDialog function with the second argument set to the ID of your radio button. If you are not using Windows API and are using MFC instead... you would call CDialog::EndDialog
Best Wishes,
-David Delaune
|
|
|
|
|
I thought enddialog takes the value returned from DoModal
How can I pass return value to DoModal without one the predefined ways/values Microsoft is forcing me
I for instance would like to return the ID of a radio button which tell me which one of the options the user selected
|
|
|
|
|
ForNow wrote: I thought enddialog takes the value returned from DoModal
You have that backwards... DoModal() receives the return value from EndDialog()
ForNow wrote: How can I pass return value to DoModal without one the predefined ways/values Microsoft is forcing me
EndDialog(YourWindowHandle,YOUR_RADIO_ID); EndDialog(YOUR_RADIO_ID);
Best Wishes,
-David Delaune
|
|
|
|
|
Thanks
My DoModal is being called from
My CMainFrame once returning to DoModal via EndDialog the Dialog box is destroyed but the class/object is still around ( I created the modal Dialog on the stack ) so the data in the class is still available until I exit the CMainFrame Routine
Also the only way to have a bad return code from UpdateData is to call pDx->fail which has the side effect of an exception which I would have to delete. if everything is okay I just call EnDialog notifying the CMainFrame which called DoModal of my selection
Thanks
|
|
|
|
|
Hi,
I am happy to hear that you have it working.
ForNow wrote: Also the only way to have a bad return code from UpdateData is to call pDx->fail which has the side effect of an exception which I would have to delete. if everything is okay I just call EndDialog notifying the CMainFrame which called DoModal of my selection
OK, let me know if you have any problems here. But from what you've described it sounds like the desired outcome.
Best Wishes,
-David Delaune
|
|
|
|
|
only one Issue UpdateData seems to always return TRUE even if I call pDX->fail
maybe I should set a global BOOL in my Modal Dialog Class and bypass checking return code from UpdateData
Thanks
|
|
|
|
|
ForNow wrote: only one Issue UpdateData seems to always return TRUE
That means UpdateData() succeeded. Can you explain to me why you need UpdateData to fail?
ForNow wrote: even if I call pDX->fail
With my experience I am going to make a strong inference that your pDX variable is a pointer to a CDataExchange object. Can you explain to me why you need this to fail? If this fails a runtime error will be thrown... I can't even think of a valid reason you would want this.
The source for UpdateData() is inside wincore.cpp and you will find that the UpdateData() function constructs a new CDataExchange object and attaches it to your Cwnd object and performs DDX through there.
In other words... your line of code inside your class:
pDX->Fail();
You should to open [Your Visual Studio Path]\VC\atlmfc\src\mfc\wincore.cpp and scroll down to the Updatedata() implementation where you will immediately understand why UpdateData() always returns TRUE; It creates a new CDataExchange object inside the function. You cannot force UpdateData() to fail by manipulating an external object.
Best Wishes,
-David Delaune
|
|
|
|
|
ForNow wrote: only one Issue UpdateData seems to always return TRUE even if I call pDX->fail
maybe I should set a global BOOL in my Modal Dialog Class and bypass checking return code from UpdateData
Why do you need the "return code from UpdateData"?
Why do you need the UpdateData at all? There was a great essay about Avoid UpdateData of Joe Newcomer. I'd recommend you to check it out!
|
|
|
|
|
Hi Victor,
I've been meaning to tell you that I am happy to see you have finally moved over here from CodeGuru. I followed Chris over to his new site some 18+ years ago soon after Zafir Anjum sold codeguru and I rarely go back to the old site anymore.
Victor Nijegorodov wrote: Why do you need the UpdateData at all? There was a great essay about Avoid UpdateData of Joe Newcomer. I'd recommend you to check it out!
Actually in this case he may need to call UpdateData() because he is bypassing both the CWnd::OnOk and also exiting the dialog with EndDialog(SOME_RADIO_ID); With that knowledge go and read the final two paragraphs in the debate between Doug Harrison and Dr. Newcomer.
If we are going to suggest that the poster "ForNow" avoid using UpdateData() then we need to ensure that he is using control variables.
Best Wishes,
-David Delaune
P.S.
I haven't used MFC in nearly 10 years, not sure why I even remember these things.
|
|
|
|
|
Greetings.
I'm a newbie here and also into C++ programming but I would like to say this is a very helpful site to watch and learn and I would like to thank to all you out there for sharing your knowledge.
As part of my interests about C++ and COM objects I was looking some tutorials and I have found this one nicely done:
COM from scratch - PART TWO[^]
I'm trying to re-create the client part of the improved example but it's not working. When I tried to do some debugging I was looking at some values and I've found this:
hr=-2147221164
Looking for some explanation I made some research and I understood that -among other things- this might be happening because of some uninitialized pointers, but I tried that suggestion and still no luck with MSVS 2008 and this simple code for a client-server application.
void main()
{
HRESULT hr;
IUnknown* pIUnknown;
IComponent* pIComponent;
IClassFactory* pIClassFactory;
::CoInitialize(NULL);
hr=CoGetClassObject(CLSID_Component,CLSCTX_INPROC_SERVER,
NULL,IID_IClassFactory,(void**)&pIClassFactory);
if (SUCCEEDED(hr))
{
hr=pIClassFactory->CreateInstance(NULL,
IID_IComponent,(void**)&pIComponent);
if(SUCCEEDED(hr))
pIComponent->Print("COM from scratch.");
}
::CoUninitialize ();
}
What could be the reason(s) this
hr variable is returning that value and how to fix it? Thank you in advance
|
|
|
|
|
Perhaps there is something useful in this[^] thread.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Hey, thank you very much for that fast reply.
I saw that discussion and I see all those errors are related to a poor server registration. In my case, I double checked that my server is registered when I was doing some debugging. If you see the paths provided in the sources there is certainly something like this:
REGEDIT
HKEY_CLASSES_ROOT\Codeproject.Component.1 =
Codeproject Component Version 1.0
HKEY_CLASSES_ROOT\Codeproject.Component.1\CLSID =
{49BF12F1-5041-48da-9B44-AA2FAA63AEFB}
HKEY_CLASSES_ROOT\Codeproject.Component = Codeproject Component
HKEY_CLASSES_ROOT\Codeproject.Component\CurVer = Codeproject.Component.1
HKEY_CLASSES_ROOT\CLSID\{49BF12F1-5041-48da-9B44-AA2FAA63AEFB} =
Codeproject Component 1.0
HKEY_CLASSES_ROOT\CLSID\{49BF12F1-5041-48da-9B44-AA2FAA63AEFB}\InprocServer32 =
c:\codeproject\component.dll
HKEY_CLASSES_ROOT\CLSID\{49BF12F1-5041-48da-9B44-AA2FAA63AEFB}\ProgID =
Codeproject.Component.1
HKEY_CLASSES_ROOT\CLSID\{49BF12F1-5041-48da-9B44-AA2FAA63AEFB}\
VersionIndependentProgID = Codeproject.Component
All my entries are OK with my server registered so I suppose the problem is somewhere but I can't find it because my lack of expertise. Looking at the MSDN/documentation the value expected should be one of these:
S_OK
Location and connection to the specified class object was successful.
REGDB_E_CLASSNOTREG
The CLSID is not properly registered. This error can also indicate that the value you specified in dwClsContext is not in the registry.
E_NOINTERFACE
Either the object pointed to by ppv does not support the interface identified by riid, or the QueryInterface operation on the class object returned E_NOINTERFACE.
REGDB_E_READREGDB
There was an error reading the registration database.
CO_E_DLLNOTFOUND
Either the in-process DLL or handler DLL was not found (depending on the context).
CO_E_APPNOTFOUND
The executable (.exe) was not found (CLSCTX_LOCAL_SERVER only).
E_ACCESSDENIED
There was a general access failure on load.
CO_E_ERRORINDLL
There is an error in the executable image.
CO_E_APPDIDNTREG
The executable was launched, but it did not register the class object (and it may have shut down).
and not that negative value
hr=-2147221164
What could be happening with this variable
hr if all variables/pointers are initialized and the server/COM Object is well registered with its CLSID for a Win32 app target?
|
|
|
|