Small DLL / C++ coding question



I have been translating AFL to DLL for reasons of hiding code. In this thread I like to ask small coding questions since my knowledge of C++ is still poor.

I am going through all the warnings I get during compilation (which I have ignored so far). Mostly it are warnings that can simple be corrected for like: conversion from ‘int’ to ‘float’, possible loss of data

how do I get this warning out of the way: “warning C4267: ‘argument’: conversion from ‘size_t’ to ‘unsigned int’, possible loss of data”

the warning points to this line of code:
arg6[0].string = (char *)gSite.Alloc(strlen(“Helvetica”) + 1);

I seemed to have removed the warning using:
arg6[0].string = (char *)gSite.Alloc( (int)(strlen(“Helvetica”) + 1));

is that correct?


Yes it is correct. You are getting warning because on 64-bit platforms strlen returns 64-bit integer (custom size_t type maps to 64-bit int, while on 32-bit it maps to 32-bit int). But of course casting to (int) is safe here because you are not going to use strings with more than 2 billion characters.


thank you.

even though the plugin seems to work fine I am working my way down the warning list to remove all warnings. I am not sure if this is really necessary, for example: when arg24 is defined as

AmiVar arg24[24];

then arg24[4].type = VAR_FLOAT; arg24[4].val = i;

gives the warning: warning C4244: ‘=’: conversion from ‘int’ to ‘float’, possible loss of data

I can remove the warning using:

arg24[4].type = VAR_FLOAT; arg24[4].val = (float)i;

although I am not sure if is really needed.


All those warnings are because of the fact that you are assigning type that have different ranges of possible values and different precision. Warnings are not errors. Warnings are just information so you are aware what you are doing. Floating point type (float) has 7 digits of accuracy. So if you assign integer that has more than 7 digits it may be rounded to nearest representable number. That is what they mean with “loss of data”. You don’t lose data. You may lose precision at least significant place (if integer is greater than 10000000)


thanks for your reply. I got rid of all the warnings anyway. No more warnings, no more errors and the plugin is still working and is very fast


Explicit cast does not cost anything because such cast is done anyway automatically by the compiler if you get the warning so performance does not suffer.