itoa()を使用する場合、char * _DstBuffが必要ですが、ここでのベストプラクティスは何ですか?
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int num = 100;
// I'm sure here is no memory leak, but it needs to know the length.
char a[10];
// will this causue memory leak? if yes, how to avoid it?
// And why can itoa(num, b, 10); be excuted correctly since b
// has only allocated one char.
char *b = new char;
// What is the difference between char *c and char *b
// both can be used correctly in the itoa() function
char *c = new char[10];
itoa(num, a, 10);
itoa(num, b, 10);
itoa(num, c, 10);
cout << a << endl;
cout << b << endl;
cout << c << endl;
return 0;
}
出力は次のとおりです。100100100
char *b = new char;
では、誰かがこことの違いを説明できchar *c = new char[10];
ますか?
私char *c
は動的に10文字を割り当てることを知っていますが、それはchar *b
動的に1文字しか割り当てないことを意味します。これが正しければ、出力がすべて正しいのはなぜですか。
実際、a、b、またはcのベストプラクティスはどれですか?