如何修复:“OverflowException发生算术运算导致溢出” [英] How to fix: "OverflowException occurred Arithmetic operation resulted in an overflow"

查看:1091
本文介绍了如何修复:“OverflowException发生算术运算导致溢出”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C#(asp)代码是:



My C# (asp) code is :

int real_buffer_size=0;
				// allocate a dump memory space in order to retrieve nb of connexion
				int BufferSize = 100*CMIB_TCPEXROW.size_ex+4;//NumEntries*CMIB_TCPEXROW.size_ex+4
				IntPtr lpTable = Marshal.AllocHGlobal(BufferSize);
				
				}
				//get the number of entries in the table
Error Line==>	Int32 NumEntries= (int)Marshal.ReadIntPtr(lpTable);
				real_buffer_size=NumEntries*CMIB_TCPEXROW.size_ex+4;

				// check if memory was enougth (needed buffer size: NumEntries*MIB_UDPEXROW.size_ex +4 (for dwNumEntries))
				if (BufferSize<real_buffer_size)
				{
					// free the buffer
					Marshal.FreeHGlobal(lpTable);
	                
					// get the needed buffer size: NumEntries*MIB_TCPEXROW.size_ex +4 (for dwNumEntries)
					BufferSize = real_buffer_size;
					// Allocate memory
					lpTable = Marshal.AllocHGlobal(BufferSize);





当我运行我的C#代码(ASP)时它会因错误而停止:



when I run my C# code (ASP) it stop with error :

引用:

发生溢出异常

算术运算导致溢出。

OverflowException occurred
Arithmetic operation resulted in an overflow.





in line:



in line:

Int32 NumEntries= (int)Marshal.ReadIntPtr(lpTable);





我知道错误是什么意思,但我不知道如何解决它!我应该做哪些更改来修复此错误? (例如,我应该更改哪个参数?)



您的帮助将被感谢,



I know what the Error mean, but I do not know how to resolve it! which changes should I make to fix this error? (for example, which parameter should I change?)

Your Help will be Appreciated,

推荐答案

TL; DR:你将64位IntPtr转换为Int32 ...导致溢出



起初,我认为这很可能是你的'+ 4',你假设填充是4字节宽。除非你明确地编译为针对x86的平台(而不是x86_64或任何CPU),否则你的lp指针大小(你正在迭代的缓冲区)将是64位(在任何CPU上,它在运行32位或64位的O / S之间翻转。使用您选择的任何技术(即... + IntPtr.Size而不是... + 4)将您的+4更改为更动态(32位+4,64位+8)。



关于那个...



然后,我意识到你得到了你试图将一个64位指针(IntPtr)转换为Int的一个转换异常(然后,你将它分配给Int32,虽然int是我记得的System.Int32的别名)。它适用于32位O / S,或者当您显式编译/构建为x86但是当您执行任何CPU或64位模式时,您会在运行时获得异常。



祝你好运&&和平!
TL;DR: You're casting 64-bits IntPtr to a Int32... Which causes "overflow"

At first, I thought it's most likely your '+4', you're assuming that the padding is 4 bytes wide. Unless you've explicitly compiled as platform targeted for x86 (and not x86_64 or "Any CPU"), your "lp" pointer size (of your buffer you're iterating) would be 64-bits (on "Any CPU", it flips between O/S running 32-bit or 64-bits). Change your +4 to be more dynamic (+4 for 32-bit, +8 for 64-bits) in whatever techniques you chose (i.e. "... + IntPtr.Size" instead of "... + 4").

On that note...

But then, I've realized that you're getting a casting exception in which you're attempting to cast a 64-bits pointer (IntPtr) into an "Int" (and then, you assign it to Int32, though "int" is alias to System.Int32 from I remember). It works on your 32-bits O/S or when you explicitly compile/build as "x86" but when you do "Any CPU" or 64-bits mode, you get exceptions during runtime.

Good luck && peace!


这看起来像是在网页上运行的可怕代码。它在做什么?你正在做一个演员,ReadIntPtr会返回什么?我建议将你的返回值填入正确的类型,而不是强制转换。然后,您将确定呼叫本身是否失败,或者是演员。你确定lpTable有很好的价值吗?你调试了吗?
This looks like horrible code to run in a web page. What is it doing ? You are doing a cast, what does ReadIntPtr return ? I suggest stuffing your return value in to the right type, without casting. Then you will know for sure if the call itself is failing, or the cast. Are you sure that lpTable has a good value in it ? Have you debugged this ?


转到应用程序池=>提前设置并启用32位应用程序为true。它为我解决了。
Go to Application pool=>Advance settings and enable 32 bit application to true. It resolved it for me.


这篇关于如何修复:“OverflowException发生算术运算导致溢出”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆