I need help in understanding when shall I use the following options
char *a = new char();
and
char *a = new char[sizeof(int)+1];
and how the respective memory freeing calls should be made?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The fist one allocates a single char. You delete it with:
The second one allocates an array of chars. The length you have chosen is a little strange. You deallocate it with:
Now… I hope you don’t think you can put a stringified number in the second
a(something like"123456", because you’ll need many more bytes. Let’s say at least 12 if anintis 32 bits. There is a funny formula to calculate the minimum length necessary. It’s an approximation of the log10 https://stackoverflow.com/a/2338397/613130To be clear, on my machine
sizeof(int)== 4, but in anintI can put-2147483648that is 10 digits plus the-, so 11 (plus the zero terminator)