问题
I'm having the strangest issue today. I was working with an example online, and to my lack of surprise, it didn't work (they pretty much never do). I went about fixing it myself, but I seem to be stuck on this error:
Error: Invalid Conversion from int* to unsigned int* [-fpermissive]
I understand this. I'm providing an int*
, it wants an unsigned int*
. However, I don't actually know why the int*
is being generated.
Here's the snippet code throwing the problem:
unsigned char md_value[EVP_MAX_MD_SIZE];
int md_length;
EVP_DigestFinal_ex(md_ctx, md_value, &md_length);
The third argument of that function call, &md_length
, is causing the problem. Looking at the documentation for that function call (From OpenSSL, if it
matters), it expects the argument to be of unsigned int*
type, which makes since, because it wants an address (or at least that how the example I'm working with is using it).
Funny thing is, I thought that the &
operator returned an unsigned int*
, as returning an int*
doesn't make sense, seeing as computers don't have negative addresses in their memory.
Here's the example I'm following along with, if you wish to take a look: https://www.openssl.org/docs/crypto/EVP_DigestInit.html
Below is the Source Code, should you want to try it out yourself. I doubt you'll actually need to read it to solve this problem, but having it here couldn't hurt.
Source Code:
//* Description *//
// Title: Example
// Author: Boom Blockhead
// Example Program to Test OpenSSL
//* Libraries *//
#include <stdio.h>
#include <cstring>
#include <openssl/evp.h>
//* Namespaces *//
using namespace std;
//* Main Method *//
// Runs the Program and Interprets the Command Line
int main(int argc, char* argv[])
{
// Initialize the Messages
char msg1[] = "Test Message\n";
char msg2[] = "Hello World\n";
// Validate Argument Count
if(argc != 2)
{
printf("Usage: %s digestname\n", argv[0]);
exit(1);
}
// Determine Message Digest by Name
OpenSSL_add_all_digests();
const EVP_MD* md = EVP_get_digestbyname(argv[1]);
// Make sure a Message Digest Type was Found
if(md == 0)
{
printf("Unknown Message Digest %s\n", argv[1]);
exit(1);
}
// Create the Message Digest Context
EVP_MD_CTX* md_ctx = EVP_MD_CTX_create();
// Setup the Message Digest Type
EVP_DigestInit_ex(md_ctx, md, NULL);
// Add the Messages to be Digested
EVP_DigestUpdate(md_ctx, msg1, strlen(msg1));
EVP_DigestUpdate(md_ctx, msg2, strlen(msg2));
// Digest the Message
unsigned char md_value[EVP_MAX_MD_SIZE];
int md_length;
EVP_DigestFinal_ex(md_ctx, md_value, &md_length); // <--- ERROR
// Destroy the Message Digest Context
EVP_MD_CTX_destroy(md_ctx);
// Print the Digested Text
printf("Digest is: ");
for(int i = 0; i < md_length; i++)
printf("%02x", md_value[i]);
printf("\n");
// Clean up the Message Digest
EVP_cleanup();
// Exit the Program
exit(0);
return 0;
}
Also, putting the explicit cast of (unsigned int*)
seems to just make things worse, as I then get the follow error:
example.cpp:(.text+0x91): undefined reference to `OpenSSl_add_all_digests`
...
example.cpp:(.text+0x1ec): undefined reference to `EVP_cleanup`
Basically, it complains about all of the OpenSSL functions.
Lastly, (again, this is just to give you guys everything you could possibly need) I'm not sending in any funny arguments to the compiler. I'm just using:
gcc example.cpp -o example.out
Since there's an error, the example.out
never actually gets created.
回答1:
Funny thing is, I thought that the & operator returned an unsigned int*, as returning an int* doesn't make sense, seeing as computers don't have negative addresses in their memory.
That's not "funny"; it's just wrong.
The address-of operator applied to an object of type T
gives you a pointer of type T*
.
Period.
Whether T
is an unsigned or signed type doesn't come into it, and has nothing to do with philosophical debate involved about whether computers may have negative addresses. In fact, pointers are generally signed because, if they weren't, you'd soon get stuck when you try to take the difference between two addresses, or walk backwards in the address space.
But that has nothing to do with the use of the term unsigned
in your code.
回答2:
as returning an int* doesn't make sense, seeing as computers don't have negative addresses in their memory.
You are misunderstanding what the type name means.
unsigned int*
is a pointer to an unsigned int
. unsigned does not refer to the pointer value.
So the solution is to change your int to unsigned int.
回答3:
Funny thing is, I thought that the & operator returned an unsigned int*, as returning an int* doesn't make sense, seeing as computers don't have negative addresses in their memory
It's not the signed-ness of the memory address number (the pointer's underlying value), it's the signed-ness of the datatype stored within the memory addresses, in this case integers.
Change your md_length
to an unsigned int
as per the spec and it should be OK.
来源:https://stackoverflow.com/questions/29417531/compiler-error-invalid-conversion-from-int-to-unsigned-int-fpermissive