Когда я использую itoa (), ему нужен символ * _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;
}
вывод:
100
100
100
Так может ли кто-нибудь объяснить разницу между char *b = new char;
и char *c = new char[10];
здесь?
Я знаю, char *c
будет динамически выделять 10 символов, но это означает, что char *b
будет динамически выделять только 1 символ, если я прав, почему вывод все правильно?
на самом деле, что является лучшей практикой a, b или c?