确定字符数组的大小effeciently使用的snprintf() [英] Determining character array size effeciently to use snprintf()

查看:168
本文介绍了确定字符数组的大小effeciently使用的snprintf()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们必须从大学的小任务,要求我们在C.执行某些工作点¯x
该问题的部分是转换一个unsigned long数,即在程序的过程中,因此没有任何办法predetrmine它产生的,为字符串。当然,我利用的snprintf 的。我初始化数组(STR [50]),这是宽大的,以避免任何形式的缓冲区错误。

We have a small assignment from college that requires us to perform some job X in C. Part of that problem is to convert an unsigned long number, that is generated in the course of the program and hence without any way to predetrmine it, to a string. Naturally, i made use of snprintf. I initialized an array (str[50]) that was generously sized to avoid any sort of buffer errors.

在提交,但是,我的教授说,我避免缓冲区的错误方法是ineffecient。

On submission, however, my professor said that my method of avoiding buffer errors was ineffecient.

我现在的问题是,当我创建一个字符数组持有的无符号long值,让我的它是什么规格?有一些C宏来帮助determind是一个unsigned long可容纳的字符的最大数量?

My question now is, when i create an char array to hold the unsigned long value, what size do i make it as? Is there some C macro to help determind the max number of characters that an unsigned long can hold?

东西也许像,

char str[MAX_NUMBER_OF_DIGITS_OF_UNSIGNED_LONG_ON_MACHINE];

我已经脱脂throught limits.h中和一些博客和这个论坛,但没有一致。任何帮助将是AP preciated!

I've skimmed throught limits.h and a few blogs and this forum but with no accord. Any help would be appreciated!

推荐答案

使用 @BLUEPIXY 去简洁。

有一个更深层次的答案。

A deeper answer.

C允许不同的语言环境这样,从理论上讲,的snprintf(...,鲁%,...)可以打印比预期更长的字符串。而不是1234567,输出可以是1,234,567。

C allows various "locales" such that, in theory, snprintf(..., "%lu",...) could print a longer string than expected. Instead of "1234567", the output could be "1,234,567".

推荐:结果
1.确定位, N ,的最大整数大小。结果
2. N * LOG2(10)四舍五入向上+ 1拿到那么字符计数。结果
3.建立一个缓冲区是2倍的最大需要。结果
4.检查结果的snprintf。结果
5.小生关注:使用带有双通话的snprintf()需要,以确保区域设置和数字不通话之间切换 - 不使用这里作为的snprintf()是一个功能昂贵的调用。

Recommend:
1. Determine the size in bits, n, of the maximum integer.
2. n * log2(10) rounded-up + 1 to get then char count.
3. Set-up a buffer that is 2x max need.
4. Check snprintf result.
5. Niche concern: Using the double call with snprintf() needs to insure the "locale" and number do not change between calls - not use here as snprintf() is a functionally expensive call.

char *ulong_to_buf(char *buf, size_t size, unsigned long x) {
    int n = snprintf(buf, size, "%lu", x);
    if (n < 0 || n >= size) return NULL;
    return buf;
}

// Usage example
void foo(unsigned long x)
  // 1/3 --> ~log2(10)
  #define ULONG_PRT_LEN (sizeof(unsigned long)*CHAR_BIT/3 + 2)
  char buf[ULONG_PRT_LEN*2 + 1];  // 2x for unexpected locales
  if (ulong_to_buf(, sizeof buf, x)) {
    puts(buf);
  }


如果code是的真正的来讲,简单的写你自己的


If code is really concerned, simple write your own

#include <stdlib.h>
#include <limits.h>
#include <string.h>
#define PRT_ULONG_SIZE (sizeof(unsigned long) * CHAR_BIT * 10 / 33 + 3)

char *ulong_strnull(int x, char *dest, size_t dest_size) {
  char buf[PRT_ULONG_SIZE];
  char *p = &buf[sizeof buf - 1];
  // Form string
  *p = '\0';
  do {
    *--p = x % 10 + '0';
    x /= 10;
  } while (x);
  size_t src_size = &buf[sizeof buf] - p;
  if (src_size > dest_size) {
    // Not enough room
    return NULL;
  }
  return memcpy(dest, p, src_size); // Copy string
}

这篇关于确定字符数组的大小effeciently使用的snprintf()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆