Optimisation Boucle While
Bonjour,
je poste ici un bout de code que j'aimerais optimiser, c'est donc une boucle while qui dans un premier temps calcule la taille d'un message alloue l'espace d'un pointeur avec cette taille puis dans un deuxieme temps remplis le buffer (2 passages donc). Cet algorithme je vais en avoir besoin souvent dans mon programme.
Quelqu'un peut t'il m'aider a optimiser ce code, moins de lignes et quelque chose de peu etre moins crade ?
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| do
{
msg_len = 0;
if (msg) memcpy(msg + msg_len, "", 1); // An empty string selects the non-prepared statement named.
msg_len += 1;
if (msg) memcpy(msg + msg_len, query, strlen(query));// Query string to analyse.
msg_len += strlen(query);
if (count > 0 && paramtypes)
{
if (msg) sql_put_int(sizeof(int16), count, msg + msg_len); // Number format codes following parameters.
msg_len += sizeof(int16);
for (i=0; i<count; i++)
{
if (msg)sql_put_int(sizeof(int32), paramtypes[i], msg + msg_len);// Object ID of the data type of the parameter.
msg_len += sizeof(int32);
}
}
else
{
if (msg) sql_put_int(sizeof(int16), 0, msg + msg_len); // Many types of data specified parameter.
msg_len += sizeof(int16);
}
if (msg)
{
msg[msg_len] = '\0';
msg_len++;
break;
}
msg_len++;
msg = (char *) malloc(msg_len);
}
while (msg); |
char * msg = NULL en debut de boucle.
Merci d'avance. :ccool: