什么是实现一个很好的&QUOT的正确方法; itoa()"功能? [英] What is the proper way of implementing a good "itoa()" function?

查看:121
本文介绍了什么是实现一个很好的&QUOT的正确方法; itoa()"功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想,如果我实现一个itoa的功能是正确的。也许你可以帮助我得到它更正确的,我是pretty肯定,我失去了一些东西。 (也许已经有做转换我想要它做的方式出库,但是......找不到任何)

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&文件ctype.h GT;字符* itoa(int i)以{
  字符*解析度=的malloc(8 * sizeof的(INT));
  sprintf的(资源,%D,我);
  返回水库;
}INT主(INT ARGC,CHAR *的argv []){
 ...


解决方案

的唯一实际的错误是,你不为空检查的malloc 的返回值。

名称 itoa 是种已经采取了一项功能,是非标准的,但不是说屡见不鲜。它不分配存储器,而将其写入到由呼叫者提供一个缓冲

 的char * itoa(int值,字符* STR,INT基地);

如果你不想依赖具有您的平台,我还是劝下的格局。字符串处理功能,这用C返回新分配的内存一般都比较麻烦比他们的长远价值,因为你们中的大多数最终会做进一步的操作,等你有时间来释放大量的中间结果。例如,比较:

 无效delete_temp_files(){
    字符文件名[20];
    的strcpy(文件名,tmp_);
    字符* endptr =文件名+ strlen的(文件名);
    对(INT I = 0; I&小于10 ++ⅰ){
        itoa(endptr,I,10); // itoa不分配内存
        取消链接(文件名);
    }
}

VS

 无效delete_temp_files(){
    字符文件名[20];
    的strcpy(文件名,tmp_);
    字符* endptr =文件名+ strlen的(文件名);
    对(INT I = 0; I&小于10 ++ⅰ){
        字符*号= itoa(ⅰ,10); // itoa分配内存
        的strcpy(endptr,数);
        自由(数);
        取消链接(文件名);
    }
}

如果您有理由要特别关心性能(例如,如果你正在实施一个STDLIB式图书馆包括 itoa ),或者如果你正在实施碱基的sprintf 不支持,那么你可以考虑不叫的sprintf 。但是,如果你想要一个基地10串,那么你的第一直觉是正确的。有绝对没有不正确关于%d个格式说明。

下面是一个可能实现的 itoa ,用于基地10只:

 的char * itobase10(的char * buf中,int值){
    sprintf的(BUF,%D,值);
    返回BUF;
}

下面是其中之一采用了的snprintf式的方法来缓冲长度:

  INT itobase10n(的char * buf中,为size_t SZ,int值){
    返回的snprintf(BUF,SZ,%D,值);
}

I was wondering if my implementation of an "itoa" function is correct. Maybe you can help me getting it a bit more "correct", I'm pretty sure I'm missing something. (Maybe there is already a library doing the conversion the way I want it to do, but... couldn't find any)

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

char * itoa(int i) {
  char * res = malloc(8*sizeof(int));
  sprintf(res, "%d", i);
  return res;
}

int main(int argc, char *argv[]) {
 ...

解决方案

The only actual error is that you don't check the return value of malloc for null.

The name itoa is kind of already taken for a function that's non-standard, but not that uncommon. It doesn't allocate memory, rather it writes to a buffer provided by the caller:

char *itoa(int value, char * str, int base);

If you don't want to rely on your platform having that, I would still advise following the pattern. String-handling functions which return newly allocated memory in C are generally more trouble than they're worth in the long run, because most of the time you end up doing further manipulation, and so you have to free lots of intermediate results. For example, compare:

void delete_temp_files() {
    char filename[20];
    strcpy(filename, "tmp_");
    char *endptr = filename + strlen(filename);
    for (int i = 0; i < 10; ++i) {
        itoa(endptr, i, 10); // itoa doesn't allocate memory
        unlink(filename);
    }
}

vs.

void delete_temp_files() {
    char filename[20];
    strcpy(filename, "tmp_");
    char *endptr = filename + strlen(filename);
    for (int i = 0; i < 10; ++i) {
        char *number = itoa(i, 10); // itoa allocates memory
        strcpy(endptr, number);
        free(number);
        unlink(filename);
    }
}

If you had reason to be especially concerned about performance (for instance if you're implementing a stdlib-style library including itoa), or if you were implementing bases that sprintf doesn't support, then you might consider not calling sprintf. But if you want a base 10 string, then your first instinct was right. There's absolutely nothing "incorrect" about the %d format specifier.

Here's a possible implementation of itoa, for base 10 only:

char *itobase10(char *buf, int value) {
    sprintf(buf, "%d", value);
    return buf;
}

Here's one which incorporates the snprintf-style approach to buffer lengths:

int itobase10n(char *buf, size_t sz, int value) {
    return snprintf(buf, sz, "%d", value);
}

这篇关于什么是实现一个很好的&QUOT的正确方法; itoa()&QUOT;功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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