Linux and Windows snprintf區別

來源:互聯網
上載者:User
今天使用snprintf函數時想到了:
    strcpy, strncpy
    strcmp, strncmp
    strcat, strncat
    sprintf, snprintf

最喜歡用的就是snprintf, 因為它會自動在後面加'/0'. 在網上看了一下. 原來VC中_snprintf不是這樣的.

VC中的_snprintf函數並沒有按照這樣的規定來做,它在輸出緩衝區不夠大時就不會輸出結尾的'/0'(跟strncpy的行為類似)。所以要讓上面的程式工作正常,必須做相應的修改。

  char buf[5];
  _snprintf(buf, 5, "This is a test string."); // buf becomes "This ", buf[4] is ' '
  buf[4] = 0;                              // buf[4] is '/0' now. 為了安全, 在windows下要手動添加
  
  _snprintf(buf, 6, "This is a test string."); // ERROR: buffer overflow
  _snprintf(buf, 5, "abc"); // buf becomes "abc", the value of buf[3] is '/0', buf[4] is undefined.

如果要保證可移植性,就得按VC的寫法,做一次對於標準來說很多餘的"填0"的操作,再通過定義宏來區別平台分別選用snprintf或_snprintf。 

一個小的測試代碼:
可以看到有兩個地方不一樣:
    1>. 結尾的'/0'處理!
    2>. 函數傳回值不一樣!

#include <stdio.h>
#include <string.h>

#define STR_LEN 32

#ifdef _WIN32
#define snprintf _snprintf
#endif

/**
 * dump string
 */
static void dump_str(const char *title, const char *str, int len)
{
  int i;

  printf("%s[%d].%p /n", title, len, str);
  for (i = 0; i < len; i++)
    printf("<%c,%02x>", str[i], (unsigned char)str[i]);

  printf("/n");
}

int main(int argc, char **argv)
{
  int t;
  char str1[STR_LEN], str2[STR_LEN];

  strcpy(str1, "123456789");
  dump_str("str1", str1, STR_LEN);

  t = snprintf(str2, STR_LEN, "%s", str1);
  dump_str("str2", str2, STR_LEN);
  printf("snprintf() ret.%d /n", t);

  t = snprintf(str2, 5, "%s", str1);
  dump_str("str2", str2, STR_LEN);
  printf("snprintf() ret.%d /n", t);

  return (0);
}

/*
 Test Env:
   gcc version 3.4.5 20051201 (Red Hat 3.4.5-2)

  Result:
str1[32].0xbffb9190
<1,31><2,32><3,33><4,34><5,35><6,36><7,37><8,38><9,39>< ,00><?fb><?bf><?d5><?82><,04><,08>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00><?c8><?91><?fb><?bf><&,26><?85><,04><,08>
str2[32].0xbffb9170
<1,31><2,32><3,33><4,34><5,35><6,36><7,37><8,38><9,39>< ,00><w,77><,01>< ,00><?92><?fb><?bf><?f8><d,64><?ad>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>
snprintf() ret.9
str2[32].0xbffb9170
<1,31><2,32><3,33><4,34>< ,00><6,36><7,37><8,38><9,39>< ,00><w,77><,01>< ,00><?92><?fb><?bf><?f8><d,64><?ad>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>
snprintf() ret.9

 -------------------------------------------------------------------------
 Test Env:
   Microsoft Windows XP [版本 5.1.2600]
   Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86

 Result:
str1[32].0012FF60
<1,31><2,32><3,33><4,34><5,35><6,36><7,37><8,38><9,39>< ,00><,12>< ,00><,10><,1d><@,40>< ,00>< ,00><,08>< ,00>< ,00><,04>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00>< ,00><?e0><,1e><@,40>< ,00>
str2[32].0012FF3C
<1,31><2,32><3,33><4,34><5,35><6,36><7,37><8,38><9,39>< ,00><?93><|,7c><&#63733;,ff><&#63733;,ff><&#63733;,ff><&#63733;,ff><?eb><,06><?93><|,7c><?ef><0,30><@,40>< ,00>< ,00>< ,00><7,37>< ,00><    ,09>< ,00>< ,00>< ,00>
snprintf() ret.9
str2[32].0012FF3C
<1,31><2,32><3,33><4,34><5,35><6,36><7,37><8,38><9,39>< ,00><?93><|,7c><&#63733;,ff><&#63733;,ff><&#63733;,ff><&#63733;,ff><?eb><,06><?93><|,7c><?ef><0,30><@,40>< ,00>< ,00>< ,00><7,37>< ,00><    ,09>< ,00>< ,00>< ,00>
snprintf() ret.-1
 */

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.