函数抛出写访问异常
Function throwing write access exception
我正在阅读一篇文章或 post 在此处在线找到:Eli Bendersky's Website : Binary Representation of Big Numbers 并遇到一个函数,因此我决定在我的 IDE 中对其进行测试。该函数编译并构建但是当我 运行 它想要抛出异常的代码时:写入访问冲突。
函数如下:
/* Note: in and out may be the same string,
it will still work OK
*/
void longdiv2(const char* in, char* out)
{
int carry = 0;
while (*in)
{
int numerator = *in++ - '0';
numerator += carry;
carry = (numerator % 2) == 0 ? 0 : 10;
*out++ = '0' + (numerator / 2);
}
*out = '[=10=]';
}
我是这样使用的:
#include <iostream>
int main() {
char* value = "12345";
char* binResult = '[=11=]';
longdiv2( value, binResult );
std::cout << *binResult << std::endl;
std::cout << "\nPress any key and enter to quit." << std::endl;
char q;
std::cin >> q;
return 0;
}
此行引发访问冲突:
*out++ = '0' + (numerator / 2);
违规是说 out
是 nullptr
。
我在 MS Visual Studio 2017 CE 上 运行在 Intel 四核至尊 运行ning Win7 Home Premium x64 - 编译和构建为 x86 控制台应用程序。
[注意:] 我用 C 和 C++ 标记了它:我这样标记它是因为文章提到他们是为 C 编写的,但是我使用的是C ++中的相同功能。
*out++
正在访问未指向有效内存的指针。这就是为什么在进行非法内存访问后取消引用它时会发生访问冲突的原因。这会起作用
char binResult[10];
基本上,当您将它传递给函数时,您将传递衰减的 char*
并对其进行更改。
或者这也行
binResult =(char*) malloc(10);
if( binResult == NULL){
perror("Malloc Failed");
}
In detail, the problem boils down to that the pointer was not
pointing to any buffer where you can store the results. When you try
to access that, you were basically trying to write to some memory that
you don't even have permission to. (Write access violation). That's
why you get the error.
更清楚的是,当您将指针值设置为 [=17=]
然后尝试访问它时,这并不奇怪。在第一次迭代中,它本身会导致 nullptr
访问,从而导致错误。
完整 C
代码:
#include<stdio.h>
void longdiv2(const char* in, char* out)
{
int carry = 0;
while (*in)
{
int numerator = *in++ - '0';
numerator += carry;
carry = (numerator % 2) == 0 ? 0 : 10;
*out++ = '0' + (numerator / 2);
}
*out = '[=12=]';
}
int main(void) {
char* value = "12345";
char binResult[10];
longdiv2( value, binResult );
printf("%s\n",binResult);
return 0;
}
这输出
06172
使用gcc 6.3.0
编译的代码:gcc -Wall -Werror progname.c
一个C++
解决方案类似于
/* Here in and out shouldn't point to same thing */
#include <iostream>
#include <string>
void longdiv2(std::string in, std::string& out)
{
int carry = 0;
for(auto x:in)
{
int numerator = x - '0';
numerator += carry;
carry = (numerator % 2) == 0 ? 0 : 10;
out.push_back( '0' + (numerator / 2));
}
}
int main(void) {
std::string value = "12345";
std::string binResult;
longdiv2( value, binResult );
std::cout<<binResult<<std::endl;
return 0;
}
更改以下内容:
char* binResult = '[=10=]';
至:
char binResult[10] {};
我正在阅读一篇文章或 post 在此处在线找到:Eli Bendersky's Website : Binary Representation of Big Numbers 并遇到一个函数,因此我决定在我的 IDE 中对其进行测试。该函数编译并构建但是当我 运行 它想要抛出异常的代码时:写入访问冲突。
函数如下:
/* Note: in and out may be the same string,
it will still work OK
*/
void longdiv2(const char* in, char* out)
{
int carry = 0;
while (*in)
{
int numerator = *in++ - '0';
numerator += carry;
carry = (numerator % 2) == 0 ? 0 : 10;
*out++ = '0' + (numerator / 2);
}
*out = '[=10=]';
}
我是这样使用的:
#include <iostream>
int main() {
char* value = "12345";
char* binResult = '[=11=]';
longdiv2( value, binResult );
std::cout << *binResult << std::endl;
std::cout << "\nPress any key and enter to quit." << std::endl;
char q;
std::cin >> q;
return 0;
}
此行引发访问冲突:
*out++ = '0' + (numerator / 2);
违规是说 out
是 nullptr
。
我在 MS Visual Studio 2017 CE 上 运行在 Intel 四核至尊 运行ning Win7 Home Premium x64 - 编译和构建为 x86 控制台应用程序。
[注意:] 我用 C 和 C++ 标记了它:我这样标记它是因为文章提到他们是为 C 编写的,但是我使用的是C ++中的相同功能。
*out++
正在访问未指向有效内存的指针。这就是为什么在进行非法内存访问后取消引用它时会发生访问冲突的原因。这会起作用
char binResult[10];
基本上,当您将它传递给函数时,您将传递衰减的 char*
并对其进行更改。
或者这也行
binResult =(char*) malloc(10);
if( binResult == NULL){
perror("Malloc Failed");
}
In detail, the problem boils down to that the pointer was not pointing to any buffer where you can store the results. When you try to access that, you were basically trying to write to some memory that you don't even have permission to. (Write access violation). That's why you get the error.
更清楚的是,当您将指针值设置为 [=17=]
然后尝试访问它时,这并不奇怪。在第一次迭代中,它本身会导致 nullptr
访问,从而导致错误。
完整 C
代码:
#include<stdio.h>
void longdiv2(const char* in, char* out)
{
int carry = 0;
while (*in)
{
int numerator = *in++ - '0';
numerator += carry;
carry = (numerator % 2) == 0 ? 0 : 10;
*out++ = '0' + (numerator / 2);
}
*out = '[=12=]';
}
int main(void) {
char* value = "12345";
char binResult[10];
longdiv2( value, binResult );
printf("%s\n",binResult);
return 0;
}
这输出
06172
使用gcc 6.3.0
编译的代码:gcc -Wall -Werror progname.c
一个C++
解决方案类似于
/* Here in and out shouldn't point to same thing */
#include <iostream>
#include <string>
void longdiv2(std::string in, std::string& out)
{
int carry = 0;
for(auto x:in)
{
int numerator = x - '0';
numerator += carry;
carry = (numerator % 2) == 0 ? 0 : 10;
out.push_back( '0' + (numerator / 2));
}
}
int main(void) {
std::string value = "12345";
std::string binResult;
longdiv2( value, binResult );
std::cout<<binResult<<std::endl;
return 0;
}
更改以下内容:
char* binResult = '[=10=]';
至:
char binResult[10] {};