提问人:Vice_Quiet_013 提问时间:1/1/2023 更新时间:1/1/2023 访问量:97
“malloc”分配的数组在“scanf”后变为 NULL
Array allocated by "malloc" becomes NULL after a "scanf"
问:
这段代码是关于 malloc 函数和动态数组操作的练习,它非常简单:程序必须为 255 个无符号字符分配 255 字节的内存(与普通整数的区别在于字符只占用一个字节,所以当我不需要大于 255 的数字时,我会节省内存,这只是一个细节), 然后它询问用户他想用这个数组做什么,直到他写出 0,现在选项只有 3。一个非常简单的练习,让我学会了使用动态数组,但有一个我无法理解的 rpoblem!当我分配内存时,一切都很顺利,直到执行扫描函数:在扫描变量“select”后,数组的指针变为 NULL,我无法弄清楚原因。这不可能是内存冲突问题,因为数组不会被存储。我能做些什么?
这里是代码,提前感谢您的帮助:
#include <stdio.h>
#include <stdlib.h>
#define TRY
void main(){
//the array is dinamically allocated in the heap memory and when it has a valid address the program starts
unsigned char select = 1; //this variable is useful for the choice of the utent
unsigned char length = 0; //this has no need to be explained
unsigned char index = 0; //this variable can be used as temporary variable in the program
unsigned char * array = (unsigned char*)malloc(255);
if(array){
while(select){
#ifdef TRY
printf("\t- - Array stored in %p memory address - -\n", array);
#endif
printf("What wanna do?\n1 - show array\n2 - insert value\n3 - edit value\n0 - quit\n");
scanf("%d", &select);
//generally the problems start from here: after the scanf the pointer turns into a NULL, you can see by the execution of the next line
#ifdef TRY
printf("\t- - Array stored in %p memory address - -\n", array);
#endif
switch(select){
case 0: printf("Bye!\n");
break;
case 1:
printf("Elements: %u\n", length);
for(index = 0; index < length; index++) printf("%d\t", *(array+index));
break;
case 2:
if(length<255){
printf("Write the value to insert in the %p memory address: ", array+length);
scanf("%d", (array+length));
printf("Value %d added in %p address\n", *(array+length), array+length);
length = length+1;
}
else
printf("Array is full\n");
break;
case 3:
printf("Write the index value: ");
scanf("%d", &index);
if(index<length){
printf("Write the value to insert in the %p memory address: ", array+index);
scanf("%d", array+index);
printf("Value %d written in %p address\n", *(array+length), array+length);
}
else
printf("Invalid index\n");
break;
default: printf("Invalid input\n");
break;
}
}
free(array);
}
else printf("Memory error\n");
}
我还尝试通过相同的 malloc 函数分配同一数组中的其他变量,更好地解释,我创建了三个无符号 char 指针而不是前三个无符号 char 变量,并为它们分配了数组最后三个字节的地址。指针不再是 NULL buuuuuut 数组始终为空:在其中插入值后,每次程序在“选择”上使用扫描功能时,数组总是用于清空。问题总是一样的。
答:
2赞
David Ranieri
1/1/2023
#1
编译并显示警告:
warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘unsigned char *’
扫描变量后,“选择”数组的指针 变成 NULL,我不知道为什么
可能是在您期望接收单个字节的地方写入 4 个字节,覆盖连续的内存区域。scanf
要将交换机扫描到:unsigned char
scanf("%hhu", &select);
和 相同array
index
下一个:动态分配的向量
评论
warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘unsigned char *’
scanf
unsigned char select
是错误的类型。遵循编译器警告。%d