0

I have to prepare a string which has to be sent over socket, that string is constructed using different data types int, char, unsigned char etc... As i have used it in many places my callgrind report says most of the CPU consumed by sprintf and strcat.

Can some one tell me alternate for this sprintf and strcat idea?

Below is the piece of code

    pData_temp = (char *)malloc(200);
    pData = (char *)malloc(500);
    sprintf(pData_temp, "String to be sent over socket at time %u $%04x",seconds,id);
    strncpy(pData,pData_temp,strlen(pData_temp);

    for in t(i=0; i < 1000; i++)
    {
        sprintf(pData_temp,"%02x%04x%08x%08x%08x",var1,var2,var3,var4,var5);
        strcat(pData,pData_temp);
    }
    sprintf(pData_temp,"\n");
    strcat(pData,pData_temp)

    sock_send(pData,strlen(pData);

    free(pData);
    free(pData_temp);

Any help appriciated.

Regards

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161

2 Answers2

3

You're using Shlemiel the painter's algorithm.

Instead, you can advance pData by the length you write, instead of strcating to it over and over again. sprintf returns the number you need to add.

But as @alk points out in the comments, it might be even better just to write pData_temp directly to the socket - don't bother making one big string at all.

AShelly
  • 34,686
  • 15
  • 91
  • 152
  • Yes i did what you suggested that is avoiding stract, it helps, And i cannot send in between information from pData_temp rather i need to create a string of information and forward over socket. But i was looking at how can i avoid sprintf as this seems to be more costlier operation... Thanks – user3631987 May 20 '14 at 10:58
  • If you are only converting to ints to hex strings, you can use a special-purpose print function that doesn't have the overhead of format string parsing. simple example: http://stackoverflow.com/q/4085612/10396 – AShelly May 20 '14 at 11:15
  • Yes that is what i was looking at thanks, i will write my own functions for %02x, %04x, %08x, %016x etc.. – user3631987 May 20 '14 at 12:24
2

You have a buffer overflow in the code. You only allocate 500 bytes for pData and you concatenate 1000 30byte strings to it.

Does the data really need to be ASCII? You can improve the efficiency of the algorithm by using binary data rather than ASCII:-

struct Data // turn off padding!
{
   char var1;
   short var2;
   int var3, var4, var5;
};

void somefunc ()
{
   int i;
   for (i = 0 ; i < 1000 ; ++i)
   {
     struct Data data; 
     // set up the members of data
     socket_write (&data, sizeof data); // not a real function, pseudo-code!
   }
}
Skizz
  • 69,698
  • 10
  • 71
  • 108
  • Lesson learned: don't use "magic numbers" in your code. Define them as constants instead. – Lundin May 14 '14 at 11:32
  • Hi Skizz i agree that there is buffer overflow, it was a sample dummy code that i wrote to explain what i am looking for, in actual code this has been take care. – user3631987 May 20 '14 at 10:50
  • Hi Ludin, I agree with you i always use #define with proper name not the magic numbers, this peice of code was written just to explain what i was looking for, anyhow thanks for comment – user3631987 May 20 '14 at 10:51