我应该使用 'malloc(0)' 还是 'free() and then set variable to NULL'?什么更好?
Should i use 'malloc(0)' or 'free() and then set variable to NULL'? What's better?
我目前正在写一个函数,禁止用户输入不必要的字符,将数字写入数组,然后从数组转换为int
,然后returns。我想创建一个基于动态数组的函数。算法如下:我输入一个数字,数组扩大,擦除数字,数组缩小。当擦除后字符串为空并等待输入数字时,就会出现问题。在这种情况下我应该如何处理?使用malloc(0)
或使用free()
,然后设置指针等于NULL
?
我可以将指针设置为 NULL
并从而清除它吗?可能,答案是否定的我不能,因为我认为记忆单元仍然充满了数据,但我想听到更详细的答案。
我的想法执行不好吗?
在代码的开头,有一个注释,如果数字超过值 2147483647
那么算法会表现得很奇怪,知道为什么会这样吗?
抱歉提出问题,但这真的让我抓狂。我搜索了整个 Internet 并没有找到我的问题的任何答案,即使在 Whosebug 和建议的重复项中也是如此。
long long int number_input(int limit)
{
// Limit set the number of digits in the number
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// I have no idea why is that happening
// 2.147.483.647 = 2147483647
// 2.147.483.648 = -2147483648
// -2.147.483.648 = -2147483648
// -2.147.483.649 = -2147483647
char c; // current entered symbol
int length = 0; // length of number (digit capacity, minus included), but index for digits is length - 1
char* number_array_pointer = NULL;
long long int number_array_integer = 0;
int index; // index for converting string to integer
if ((number_array_pointer = (char*)realloc(number_array_pointer, sizeof(char) * length)) == NULL) {
cout << "Fatal: failed to allocate " << sizeof(char) * length << " bytes.\n";
abort();
}
for (;;) {
c = _getch();
if (c == 13 && length != 0) { // Enter breaks down the loop
if (number_array_pointer[0] == '-' && length < 2) {
continue;
}else {
break;
}
}
else if (c == '\b' && length != 0) { // Backspace
cout << "\b \b";
length--;
if (length == 0)
{
// malloc(0) vs free() then ' = NULL'
if ((number_array_pointer = (char*)malloc(sizeof(char) * length)) == NULL) {
cout << "Fatal: failed to allocate " << sizeof(char) * length << " bytes.\n";
abort();
}
} // We cannot realloc 0 bytes
else if ((number_array_pointer = (char*)realloc(number_array_pointer, sizeof(char) * length)) == NULL) {
cout << "Fatal: failed to reallocate " << sizeof(char) * length << " bytes.\n";
abort();
}
}
else if (c == 45 && length == 0) { // Minus for negative number
length++;
if ((number_array_pointer = (char*)realloc(number_array_pointer, sizeof(char) * length)) == NULL) {
cout << "Fatal: failed to reallocate " << sizeof(char) * length << " bytes.\n";
abort();
}
number_array_pointer[length - 1] = c;
cout << c;
}
else if (c > 47 && c < 58 && length < limit) { // Allow to enter only digits
length++;
if ((number_array_pointer = (char*)realloc(number_array_pointer, sizeof(char) * length)) == NULL) {
cout << "Fatal: failed to reallocate " << sizeof(char) * length << " bytes.\n";
abort();
}
number_array_pointer[length - 1] = c;
cout << c;
}
else {
continue;
}
}
if (number_array_pointer[0] == '-') {
index = 1;
}
else {
index = 0;
}
for (index; index < length; index++)
{
number_array_integer *= 10;
// get the actual digit from ascii code
number_array_integer += (long long int)number_array_pointer[index] - 48;
}
if (number_array_pointer[0] == '-') {
number_array_integer *= -1;
}
free(number_array_pointer);
return number_array_integer;
}
这是你第二部分的答案,关于如果数字超过值 2147483647
为什么事情会偏离轨道
#include <iostream>
#include <limits>
int main() {
std::cout<<"The answer is "<<std::numeric_limits<int>::max();
}
The answer is 2147483647.
这是 32 位整数的最大值(平台上通常的 int
是 32 位)。
关于你的第一部分,如果没有禁忌,请用std::vector!我会让别人回答的。
我目前正在写一个函数,禁止用户输入不必要的字符,将数字写入数组,然后从数组转换为int
,然后returns。我想创建一个基于动态数组的函数。算法如下:我输入一个数字,数组扩大,擦除数字,数组缩小。当擦除后字符串为空并等待输入数字时,就会出现问题。在这种情况下我应该如何处理?使用malloc(0)
或使用free()
,然后设置指针等于NULL
?
我可以将指针设置为 NULL
并从而清除它吗?可能,答案是否定的我不能,因为我认为记忆单元仍然充满了数据,但我想听到更详细的答案。
我的想法执行不好吗?
在代码的开头,有一个注释,如果数字超过值 2147483647
那么算法会表现得很奇怪,知道为什么会这样吗?
抱歉提出问题,但这真的让我抓狂。我搜索了整个 Internet 并没有找到我的问题的任何答案,即使在 Whosebug 和建议的重复项中也是如此。
long long int number_input(int limit)
{
// Limit set the number of digits in the number
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// I have no idea why is that happening
// 2.147.483.647 = 2147483647
// 2.147.483.648 = -2147483648
// -2.147.483.648 = -2147483648
// -2.147.483.649 = -2147483647
char c; // current entered symbol
int length = 0; // length of number (digit capacity, minus included), but index for digits is length - 1
char* number_array_pointer = NULL;
long long int number_array_integer = 0;
int index; // index for converting string to integer
if ((number_array_pointer = (char*)realloc(number_array_pointer, sizeof(char) * length)) == NULL) {
cout << "Fatal: failed to allocate " << sizeof(char) * length << " bytes.\n";
abort();
}
for (;;) {
c = _getch();
if (c == 13 && length != 0) { // Enter breaks down the loop
if (number_array_pointer[0] == '-' && length < 2) {
continue;
}else {
break;
}
}
else if (c == '\b' && length != 0) { // Backspace
cout << "\b \b";
length--;
if (length == 0)
{
// malloc(0) vs free() then ' = NULL'
if ((number_array_pointer = (char*)malloc(sizeof(char) * length)) == NULL) {
cout << "Fatal: failed to allocate " << sizeof(char) * length << " bytes.\n";
abort();
}
} // We cannot realloc 0 bytes
else if ((number_array_pointer = (char*)realloc(number_array_pointer, sizeof(char) * length)) == NULL) {
cout << "Fatal: failed to reallocate " << sizeof(char) * length << " bytes.\n";
abort();
}
}
else if (c == 45 && length == 0) { // Minus for negative number
length++;
if ((number_array_pointer = (char*)realloc(number_array_pointer, sizeof(char) * length)) == NULL) {
cout << "Fatal: failed to reallocate " << sizeof(char) * length << " bytes.\n";
abort();
}
number_array_pointer[length - 1] = c;
cout << c;
}
else if (c > 47 && c < 58 && length < limit) { // Allow to enter only digits
length++;
if ((number_array_pointer = (char*)realloc(number_array_pointer, sizeof(char) * length)) == NULL) {
cout << "Fatal: failed to reallocate " << sizeof(char) * length << " bytes.\n";
abort();
}
number_array_pointer[length - 1] = c;
cout << c;
}
else {
continue;
}
}
if (number_array_pointer[0] == '-') {
index = 1;
}
else {
index = 0;
}
for (index; index < length; index++)
{
number_array_integer *= 10;
// get the actual digit from ascii code
number_array_integer += (long long int)number_array_pointer[index] - 48;
}
if (number_array_pointer[0] == '-') {
number_array_integer *= -1;
}
free(number_array_pointer);
return number_array_integer;
}
这是你第二部分的答案,关于如果数字超过值 2147483647
#include <iostream>
#include <limits>
int main() {
std::cout<<"The answer is "<<std::numeric_limits<int>::max();
}
The answer is 2147483647.
这是 32 位整数的最大值(平台上通常的 int
是 32 位)。
关于你的第一部分,如果没有禁忌,请用std::vector!我会让别人回答的。