本人现在要写一个模仿内存管理的程序现在被一个地方卡住了,请帮帮忙:现假设模拟内存空间的首地址和末地址分别为:spaceBegin 和 spaceEnd
当用户进入释放程序内存空间的操作时,要求用户输入程序的起始地址,
先用一函数: void Input (char * addr) 来实现接受用户输入的地址,
此程序需要对各种非法输入进行识别报错,并要求用户再次输入,我写的程序如下: void Input (char * addr) {
while (1) {
printf("\n Please input the ADDRESS of the program: ");
scanf("%u", &addr);
if (addr < spaceBegin || addr > sapceEnd) {
printf("\n Sorry, you submitted a wrong address \n");
}
else return;
}
}当输入:aklsduidf... 等非法输入时,
屏幕会出现无限打印:
Please input the ADDRESS of the program:
Sorry, you submitted a wrong address Please input the ADDRESS of the program:
Sorry, you submitted a wrong address Please input the ADDRESS of the program:
Sorry, you submitted a wrong address Please input the ADDRESS of the program:
Sorry, you submitted a wrong address Please input the ADDRESS of the program:
Sorry, you submitted a wrong address ... ...请问有什么方法解决上述非法输入!
恳求那位高手能帮我解决!谢谢
当用户进入释放程序内存空间的操作时,要求用户输入程序的起始地址,
先用一函数: void Input (char * addr) 来实现接受用户输入的地址,
此程序需要对各种非法输入进行识别报错,并要求用户再次输入,我写的程序如下: void Input (char * addr) {
while (1) {
printf("\n Please input the ADDRESS of the program: ");
scanf("%u", &addr);
if (addr < spaceBegin || addr > sapceEnd) {
printf("\n Sorry, you submitted a wrong address \n");
}
else return;
}
}当输入:aklsduidf... 等非法输入时,
屏幕会出现无限打印:
Please input the ADDRESS of the program:
Sorry, you submitted a wrong address Please input the ADDRESS of the program:
Sorry, you submitted a wrong address Please input the ADDRESS of the program:
Sorry, you submitted a wrong address Please input the ADDRESS of the program:
Sorry, you submitted a wrong address Please input the ADDRESS of the program:
Sorry, you submitted a wrong address ... ...请问有什么方法解决上述非法输入!
恳求那位高手能帮我解决!谢谢
scanf("%u", &addr);
为:
scanf("%u", addr);
注意addr已经是指针了。
scanf("%u", &addr);
为:
scanf("%u", addr);
注意addr已经是指针了。
----------------------------------
同意
我要输入的是地址,目的是要传给addr, 而非*addr, 所以肯定是scanf("%u", &addr); "改成%s试试", 我试过了,不行。 "%p",无限循环的速度比我的还要快。
#include <stdio.h>char * spaceBegin, * spaceEnd;void Input (char * addr) {
...
}void main () {
char * a = (char *)malloc(sizeof(char) * 100);
// 界定空闲区范围
spaceBegin = a;
spaceEnd = a + sizeof(char) * 100;
char * pAddr;
Input(pAddr); // 输入用户程序的首地址
// 上面函数通过则继续别的操作
...
}
test.cpp
E:\My Visual C++\Test\test.cpp(43) : error C2664: 'Input' : cannot convert parameter 1 from 'char ** ' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.test.exe - 1 error(s), 0 warning(s)