外部DLL,静态函数和C ++中的char *关系

时间:2013-07-23 21:32:55

标签: c++ pointers dll delete-operator

我正在试图弄清楚如何正确编码这种情况:

在C ++应用程序中,我通过LoadLibrary调用外部DLL,然后:

char * _input = new {... is  created here with some data}
char *aallocchrar =new char[10000];
fnHello(_input,aallocchrar);

fllHellois在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");
}

1 个答案:

答案 0 :(得分:0)

听起来你正在对未初始化的字符串执行“strcat()”。

可能导致分段违规的Methinks,嗯?

问:如果你替换“strcpy()”会怎么样?你还遇到这个问题吗?