c++ - What is the best practice of using itoa() -
c++ - What is the best practice of using itoa() -
when utilize itoa() needs char* _dstbuff, best practice here?
#include "stdafx.h" #include <iostream> using namespace std; int main() { int num = 100; // i'm sure here no memory leak, needs know length. char a[10]; // causue memory leak? if yes, how avoid it? // , why can itoa(num, b, 10); excuted correctly since b // has allocated 1 char. char *b = new char; // difference between char *c , char *b // both can used correctly in itoa() function char *c = new char[10]; itoa(num, a, 10); itoa(num, b, 10); itoa(num, c, 10); cout << << endl; cout << b << endl; cout << c << endl; homecoming 0; }
the output is: 100 100 100
so explain differenct between char *b = new char;
, char *c = new char[10];
here?
i know char *c
dynamiclly allocate 10 chars, means char *b
dynamically allocate 1 char, if i'm right this, why output correct?
actually best practice of a, b, or c?
best practice: don't utilize @ all.
why? because it's not in standard.
what should instead? utilize std::to_string
.
(if you're stuck having utilize itoa
, utilize big local static buffer, char[512]
or -- if want really really safe can create array size sizeof(unsigned long long int) * char_bit + 2
or can hold number expressed in base, plus sign.)
c++ memory-leaks memory-management itoa
Comments
Post a Comment