Here's my function:
char *HexCastName(UINT Address) {
char Name[100] = "";
UINT Offset;
for (int i = 0; i < HardNames.size(); i++) {
if (HardNames[i].Array == Hex.CurrentRegion.Array &&
HardNames[i].Start <= Address &&
HardNames[i].Start + HardNames[i].Size > Address) {
Offset = Address - HardNames[i].Start;
sprintf(Name, " : %s[%d]", HardNames[i].Name, Offset);
}
}
return Name;
}
I do this:
char name[100];
sprintf(name, HexCastName(SELECTION_START));
The resulting string name
is only 4 digits, though HexCastName()
returns more. I tried to trace it, and it seems to pass the entire string to sprintf()
, and somewhere inside it, it gets to _output_l(outfile,format,NULL,arglist)
function. Inside it, the format
variable at first contains my whole string, then after reading some variables the execution jumps from 973 to 978 in output.c, and my format
is already truncated. I'm totally confused by that behavior... Why 4 letters? Maybe some failure with pointers and chars?
EDIT:
Here's the version that seems to work:
void HexCastName(char *buff, UINT size, UINT Address) {
sprintf(buff, "");
UINT Offset;
for (int i = 0; i < HardNames.size(); i++) {
if (HardNames[i].Array == Hex.CurrentRegion.Array &&
HardNames[i].Start <= Address &&
HardNames[i].Start + HardNames[i].Size > Address) {
Offset = Address - HardNames[i].Start;
_snprintf(buff, size, " : %s[%d]",HardNames[i].Name, Offset);
}
}
}
char *name = (char *)malloc(100);
HexCastName(name, 100, SELECTION_START);
sprintf(str, "%s: $%06X%s", area,
Hex.AddressSelectedFirst + Hex.CurrentRegion.Offset, name);
free(name);