我应该使用'malloc(0)'或'free()然后将变量设置为NULL'吗?什么更好?



我目前正在编写一个函数,该函数禁止用户输入不必要的字符,并将数字写入数组,然后从数组转换为int,然后返回。我想做一个基于动态数组的函数。算法如下:我输入一个数字,数组展开,擦除数字,数组缩小。当字符串在擦除后为空并等待输入数字时,就会出现问题。在这种情况下,我应该如何处理?使用malloc(0)还是使用free(),然后将指针设置为等于NULL

我可以将指针设置为NULL,从而清除它吗?答案可能是否定的,我不能,因为我认为记忆细胞仍然充满了数据,但我想听到更详细的答案。

我的想法执行得不好吗?

在代码的开头,有一条注释,如果数字超过值2147483647,那么算法的行为会很奇怪,你知道为什么会发生这种情况吗?

很抱歉问了这么多问题,但这真的让我抓狂。我搜索了整个互联网,没有找到任何问题的答案,即使是在StackOverflow和拟议的副本中。

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!我会让别人来回答的。

相关内容

  • 没有找到相关文章

最新更新