I wanted to make Random number from Openssl.
So. I made program :
#include "openssl/rand.h"
struct randomData
{
char str[32];
}
int main()
{
std::vector<randomData> vecStr;
randomData rd;
vecStr.emplace_back(rd);
RAND_bytes((unsigned char *)vecStr[i].str, sizeof(vecStr[i].str));
std::string ss = to_hex(vecStr[i].str, sizeof(vecStr[i].str));
std::cout << "Random Nonce Hex Data : " << ss << " // Key Length : " << ss.length()<<std::endl;
return 0;
}
// [out put]
// Random Nonce Hex Data : f0f5e38e596fdb2f7cef79d3706fcbf111decaa844154295b89b90eb65925a53 // Key Length : 64
But I don't want to make struct. Just use vector. So, I tried.
std::vector<char> str;
str.reserve(33);
RAND_bytes((unsigned char *)&str, sizeof(str));
std::string ss = to_hex((char*)&str, sizeof(str));
std::cout << "Random Nonce Hex Data : " << ss << " // Key Length : " << ss.length() << std::endl;
// [ output ]
// Random Nonce Hex Data : f1935de540b5a75bcaa9eab4b173abcb6a840bf83c4181ee // Key Length : 48
// Segmentation fault (core dumped)
You can see the error log. I searched a difference "reserve" with "resize". But those function caused same error.
Why this happen??
RAND_bytes? - Blaze(unsigned char *), you do something wrong. - S.M.&strshould be&str[0]orstr.data(). You want the address of the first element/data not the address of the vector itself. - rveerd