我试图弄清楚如何正确编码这种情况:
在 C++ 应用程序中,我通过 LoadLibrary 调用外部 DLL,然后:
char * _input = new {... is created here with some data}
char *aallocchrar =new char[10000];
fnHello(_input,aallocchrar);
fnHello 是 dll 中的静态函数。它返回 void,但用一些数据填充 aallocchrar 指针。
在这之后,我做
delete[] _aallocchrar;
它总是抛出内存错误。在静态函数内部,最后,我尝试使用:
return 0;
exit(1);
exit(1) 即使在函数执行时也会崩溃。
如何完美地做到这一点?传递指针,填充数据,退出 dll 函数。为什么那个邪恶的 dll 改变了我的 aallochar 函数,我以后不能删除 [] 它?我应该如何正确结束静态 dll 函数 - 退出?- 所以它应该释放我的指针?
在 dll 函数中,我对 _aallocchrar* 所做的只是:
strcat (str, ";\n\n");
我不会重新分配它或类似的东西。功能定义为:
void __declspec(dllexport) __cdecl runMainRoutine(char* _inputString, char* finalResult)
EDIT1:此函数适用于传递的 char 数组:
char* eqn_output(pPLA PLA, char* str)
{
//
register pcube p, last;
register int i, var, col, len;
int x;
bool firstand, firstor;
if (cube.output == -1)
fatal("Cannot have no-output function for EQNTOTT output mode");
if (cube.num_mv_vars != 1)
fatal("Must have binary-valued function for EQNTOTT output mode");
makeup_labels(PLA);
/* Write a single equation for each output */
for(i = 0; i < cube.part_size[cube.output]; i++)
{
strcat (str, OUTLABEL(i));
strcat (str, " = ");
//printf("%s = ", OUTLABEL(i));
col = strlen(OUTLABEL(i)) + 3;
firstor = TRUE;
/* Write product terms for each cube in this output */
foreach_set(PLA->F, last, p)
if (is_in_set(p, i + cube.first_part[cube.output])) {
if (firstor)
{
strcat (str, "(");
col += 1;
// printf("("), col += 1;
}
else
{
strcat (str, " | (");
col += 4;
//printf(" | ("), col += 4;
}
firstor = FALSE;
firstand = TRUE;
/* print out a product term */
for(var = 0; var < cube.num_binary_vars; var++)
if ((x=GETINPUT(p, var)) != DASH) {
len = strlen(INLABEL(var));
if (col+len > 72)
{
strcat (str, "\n ");
col = 4;
//printf("\n "), col = 4;
}
if (! firstand)
{
strcat (str, "&");
col += 1;
//printf("&"), col += 1;
}
firstand = FALSE;
if (x == ZERO)
{
strcat (str, "!");
col += 1;
//printf("!"), col += 1;
}
//printf("%s", INLABEL(var)), col += len;
strcat (str, INLABEL(var));
col += len;
}
//printf(")"), col += 1;
strcat (str, ")");
col += 1;
}
strcat (str, ";\n\n");
//printf(";\n\n");
}