访问冲突写入位置 [英] Access violation writing location
问题描述
我有以下代码:
#include< openssl / bn.h>
#include< openssl / rsa.h>
unsigned char * key;
RSA * rsa = RSA_new();
rsa = RSA_generate_key(1024,65537,NULL,NULL);
// init pubkey
key [BN_num_bytes(rsa-> n)] ='\0';
BN_bn2bin(rsa-> n,key);
printf(RSA Pub:%s\\\
,key);
RSA_free(rsa);
rsa = NULL;
调试器告诉我在线路上有一个问题访问冲突写入位置 p>
key [BN_num_bytes(rsa-> n)] ='\0';
如果我注释掉那一行,问题只会转到
BN_bn2bin(rsa-> n,key);
有关如何解决这个问题的任何建议都是很好的。
<因为
键没有指向任何东西,并且你已经使用数组符号下标引用它,那就是源代码。因为 。 key如何获得值。你正在覆盖或践踏某些其他内存块,这不是你的,因此访问冲突被窗口捕获。请仔细检查您的代码,并确保该变量已被malloc'd或new'd。
注意,最好是你的理智来声明它
unsigned char * key = NULL;
这样,如果您尝试访问键
,而不是malloc'd / new'd,你会得到一个内存异常错误(这可以很容易地归结到这一点)。考虑它使调试更容易。
希望这有助于,
最好的问候,
汤姆。
I have the following code:
#include <openssl/bn.h>
#include <openssl/rsa.h>
unsigned char* key;
RSA* rsa = RSA_new();
rsa = RSA_generate_key(1024,65537,NULL,NULL);
//init pubkey
key[BN_num_bytes(rsa->n)] = '\0';
BN_bn2bin(rsa->n, key);
printf("RSA Pub: %s\n", key);
RSA_free( rsa );
rsa = NULL;
The debugger is telling me that I have an issue "Access violation writing location" on the line
key[BN_num_bytes(rsa->n)] = '\0';
If I comment out that line the issue just moves down to
BN_bn2bin(rsa->n, key);
any suggestions on how to fix this issue would be great.
解决方案 Since key
is not pointing to anything and you have referenced it with array notation subscript, that is the source. How does key get the value. You are overwriting or trampling on some other memory block that is not yours hence the 'Access violation' as trapped by windows. Double check your code and make sure that the variable has been malloc'd or new'd.
As a side note, it is best for your sanity to declare it like this
unsigned char *key = NULL;
In that way if you try access key
without it being malloc'd/new'd, you will get a memory exception error (which can easily be nailed down to this). Consider it makes debugging much easier.
Hope this helps,
Best regards,
Tom.
这篇关于访问冲突写入位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!