访问违规写入位置
我有以下代码:
#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;
调试器告诉我,我在该行上遇到了“访问冲突写入位置”问题
key[BN_num_bytes(rsa->n)] = '\0';
如果我注释掉该行,该问题就会向下移动到
BN_bn2bin(rsa->n, key);
有关如何解决此问题的任何建议,那就太好了。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于
key
没有指向任何东西,并且您已经使用数组符号下标引用了它,这就是源。 key如何获取value。您正在覆盖或践踏不属于您的其他内存块,因此会被窗口捕获“访问冲突”。仔细检查您的代码并确保该变量已被 malloc 或 new 。顺便说一句,最好是这样声明它,
这样如果您尝试访问
key
而没有对其进行 malloc'd/new'd,您将收到内存异常错误(这可以很容易地确定下来)。考虑一下它使调试变得更加容易。希望这有帮助,
此致,
汤姆.
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
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.
您遇到访问冲突,因为您尝试使用空终止符分配键,但尚未为键分配任何内存。我们需要知道您想要实现什么目标。
You have an access violation because you try to assign key with the null terminator but you haven't allocated any memory for key. We would need to know what you are trying to accomplish.
您没有为该键分配任何内存——第一次使用它是在您尝试将某个元素设置为 0 时。
You aren't allocating any memory for the key -- the first time it's used is when you try to set an element to 0.
在这种情况下分配适量内存的正确解决方案是:
The correct solution to allocate the right amount of memory in this case is: