Pessoal eu de novo com o c++…
Estou com o seguinte problema, criei um método que concatena alguns char’s*, até ai tudo bem… ele funciona blz… Vou mostrar o código…
[code]
char* LogErros::montaInsert(int idRegistro, char* tab_origem,
char* tab_destino, char* fase,
char* campo, char* valor,
DescricaoErro descricaoErro, char* sistema,
char* tipo)
{
char* sqlInsert;
sqlInsert = "INSERT INTO LOG_ERROS (REGISTRO_ID, TABELA_ORIGEM, TABELA_DESTINO, FASE, CAMPO, VALOR, DESCRICAO, SISTEMA_ORIGEM, TIPO) VALUES (";
char registroIdChar[15];// = new char[15];
sprintf(registroIdChar,"%d",idRegistro);
char* descricaoErroChar = this->getDescricaoErro(descricaoErro);
int tamanho = strlen(registroIdChar) + strlen(tab_origem) +
strlen(tab_destino) + strlen(fase) +
strlen(campo) + strlen(valor) + strlen(descricaoErroChar) +
strlen(sistema) + strlen(tipo) + 10;
char* retorno = new char[tamanho];
strcpy(retorno, sqlInsert);
strcat(retorno,registroIdChar);
strcat(retorno,",'");
strcat(retorno,tab_origem);
strcat(retorno,"','");
strcat(retorno,tab_destino);
strcat(retorno,"','");
strcat(retorno,fase);
strcat(retorno,"','");
strcat(retorno,campo);
strcat(retorno,"','");
strcat(retorno,valor);
strcat(retorno,"','");
strcat(retorno,descricaoErroChar);
strcat(retorno,"','");
strcat(retorno,sistema);
strcat(retorno,"','");
strcat(retorno,tipo);
strcat(retorno,"')");
return retorno;
}[/code]
No entanto quando eu uso ele e vou fazer mais alguma operações que exigem muita memória da um erro “bad allocation” e o programa para de funcionar… Gostaria de saber como eu poderia fazer para otimizar o código acima, ou liberar a memória ou ainda saber o que estou fazendo errado…
Agradeço a ajuda…