Challenge 2 – What is the error? in C++

Difficulty ![]() |
![]() |
This Challenge was proposed for the now defunct C/C++ programming list CWORLD in 1999.
The source code of the challenge has been updated to modern C++, but maintaining the essence of the challenge, which its original author wanted to transmit.
Date: 13-VIII-2000
Created by: David A. Capello
Challenge winner: Sebastián García
This is very easy. What is the error?
Source Code
#include <cstdio>
#include <cstdlib>
#define MAX_SIZE 32
int main(void)
{
short *buffer = std::malloc(MAX_SIZE);
int i;
for (i = 0; i < MAX_SIZE; i++)
buffer[i] = (i % 224) + ' ';
for (i = 0; i < MAX_SIZE; i++)
std::putchar(buffer[i]);
std::putchar('\n');
return 0;
}
AND FINALLY I CAN ONLY SAY …. BEST OF LUCK AND MAY THE BEST CODE WIN….
If you liked this challenge remember to leave a comment in our guestbook
Update 2024:
Currently, the malloc function is obsolete in modern C++, precisely because it presents problems like the one presented in this challenge. At the beginning of this century, the new and delete operators replaced the old malloc and free that came from the C language; although currently the use of STL containers in C++ has become more popular and, being of a higher level, avoids the problem of manually deleting unused reserved memory, in addition to providing standard algorithms that work on this memory space in a fairly simple way. The C++ standard development team continues working to improve the language and hopefully in the future C++ will have its own garbage collector.