我创建了一个简单的 memtest 程序。
但是,当我尝试运行它(当然是使用 sudo)时,它会在以下行崩溃:
main -> test_all -> test_running -> for (j=0; j<=memsize; j++) p[j]=1-value; //(第一次赋值操作)
它没有指定错误。我在考虑两种可能性:
1)错误的内存范围,应该选择另一个(哪个?)
2)我的代码有问题(在哪里?)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MEM_start 0xFD002000
#define MEM_end 0xFDFFFFFF
#define MEM_size 16769023
int test_running(int value) {
int i, j;
int flag=0;
int *p = (int *)MEM_start;
int memsize = (int)MEM_size;
for (i=0; i<=memsize; i++) {
for (j=0; j<=memsize; j++) p[j]=1-value;
p[i]=value;
if (p[i]!=value) { printf("Error at address: %p\n", &p[i]); flag++; }
for (j=0; j<=memsize; j++) {
if ((j!=i)&&(p[j]==value)) {
printf("Error at address: %p\n", &p[j]);
flag++;
}
}
}
return flag;
}
int test_chess() {
int i;
int flag=0;
int *p = (int *)MEM_start;
int memsize = (int)MEM_size;
for (i=0; i<=memsize; i++) p[i]=i%2;
for (i=0; i<=memsize; i++) if (p[i]!=i%2) { printf("Error at address: %p\n", &p[i]); flag++; }
for (i=0; i<=memsize; i++) p[i]=(i+1)%2;
for (i=0; i<=memsize; i++) if (p[i]!=(i+1)%2) { printf("Error at address: %p\n", &p[i]); flag++; }
return flag;
}
int test_random() {
int i, j;
int flag=0;
int *p = (int *)MEM_start;
int memsize = (int)MEM_size;
int buffer[32];
int buffer_size=32;
for (i=0; i<=memsize; i=i+buffer_size) {
for (j=0; j<=buffer_size; j++) buffer[j]=(0==(rand()%2));
for (j=0; j<=buffer_size; j++) p[i+j]=buffer[j];
for (j=0; j<=buffer_size; j++) if (p[i+j]!=buffer[i]) { printf("Error at address: %p\n", &p[i+j]); flag++; }
}
return flag;
}
int test_all() {
int test=0;
int flag;
flag=test_running(1);
if (flag) { test++; printf("Running 1 - FAIL\n"); }
else printf("Running 1 - SUCCESS\n");
flag=test_running(0);
if (flag) { test++; printf("Running 0 - FAIL"); }
else printf("Running 0 - SUCCESS\n");
flag=test_chess();
if (flag) { test++; printf("Chess - FAIL\n"); }
else printf("Chess - SUCCESS\n");
flag=test_chess();
if (flag) { test++; printf("Random - FAIL\n"); }
else printf("Random - SUCCESS\n");
if (test!=0) { printf("SOME TESTS FAILED: %i", test); return 1; }
else { printf("ALL TESTS PASSED!!!"); return 0; }
}
int main() {
srand(time(NULL));
test_all();
return 0;
}
更新:以下链接非常有帮助:1)FixUnix:写入已知物理地址的设备