简体   繁体   English

如何用C中的内存地址初始化char数组?

[英]How do I initialize a char array with a memory address in C?

I'm in a sophomore C class and this project is about dealing with pointers and designing a memory dump function.我在大二 C 班上,这个项目是关于处理指针和设计内存转储函数。 So I've been able to struggle through the pointers and got a beginning and ending address to dump, even bitmasked it, and I wanted to initialize a char array with the beginning memory address.所以我已经能够通过指针挣扎并得到一个开始和结束地址转储,甚至位掩码它,我想用开始内存地址初始化一个字符数组。 I initialize it with the same variable storing my masked beginning address but when I print the array, it contains a different memory address.我使用存储掩码起始地址的相同变量对其进行初始化,但是当我打印数组时,它包含不同的内存地址。 Here's the function:这是函数:

void memDump(void *base, int bytes)
{
unsigned char *begin;
begin = base;//beginning of range of memory
unsigned char *end;// ending range of memory
end = base + bytes;
int a, b;
long long int d=base;
d=d&0xFFFFF0; //trying to bitmask
long long int e=end;
e = e&0xFFFF0; //masked off the beginning and ending range
char c[16]={d}; //loop variables
printf("%x", c);

for (a=begin; a<=end; a+=16)
    {
        printf("\n%016X\n", d);
        printf("%016X\n", a);
        printf("%016X", e);
    }

}

Sorry guys, i can't find something similar and this is my last resort.对不起,伙计们,我找不到类似的东西,这是我最后的手段。 Thanks!谢谢!

Update: Thanks for the insight everyone, reading some more about C and some articles on how to debug helped me out.更新:感谢大家的洞察力,阅读更多关于 C 的内容和一些关于如何调试的文章帮助了我。

You cannot "initialize a char array" with some "memory address."你不能用一些“内存地址”“初始化一个字符数组”。 A char array can only be initialized with characters. char 数组只能用字符初始化。

Stackoverflow is not about doing your homework for you, so I will give you some advice, and then you can try implementing it. Stackoverflow 不是给你做功课,所以我给你一些建议,然后你可以尝试实现它。 If you cannot put the advice into code, then you do not deserve to turn in a completed assignment.如果您不能将建议写入代码,那么您就不配上交已完成的作业。

First of all, once you have bitmasked your "d", you need to store it back into "begin", so that you have a pointer from which you can start reading bytes to dump.首先,一旦你对你的“d”进行了位掩码,你需要将它存储回“begin”,这样你就有了一个指针,你可以从中开始读取要转储的字节。

This instruction:本指令:

printf( "%08p ", begin );

Will render the hexadecimal representation of your "begin" address in 8 characters, followed by a space.将以 8 个字符呈现“开始”地址的十六进制表示,后跟一个空格。 This is how you need to begin each row of your memory dump.这就是您需要如何开始内存转储的每一行。

The instruction:指令:

printf( "%02x ", *(begin++) );

gets the byte pointed by "begin", and renders the hexadecimal representation of that byte in two characters, followed by a space.获取“begin”指向的字节,并以两个字符呈现该字节的十六进制表示,后跟一个空格。 It then increments "begin", to point to the next byte.然后它增加“begin”,指向下一个字节。 You need to do this 8 or 16 times, depending on how wide you want your memory dump to be, then do a printf( "\\n" ) to move to the next line.您需要执行此操作 8 或 16 次,具体取决于您希望内存转储的宽度,然后执行printf( "\\n" )以移至下一行。

Then you need to keep repeating the above until your "begin" has exceeded your "end".然后你需要不断重复以上,直到你的“开始”超过你的“结束”。 (So, you are looking at an outer loop, for each row, and an inner loop, for each byte within the row.) (因此,您正在查看每行的外循环和行内每个字节的内循环。)

I hope this helps.我希望这有帮助。

As @Jean-FrançoisFabre observed,正如@Jean-FrançoisFabre 所观察到的那样,

 char c[16]={d};

probably does not do what you think it does.可能不会做你认为它会做的事情。 That is, unless what you think it does is convert the long long int value stored in d to type char (producing an implementation-defined result drawn from a much smaller range than that of d itself), initializing the first element of array c with that value, and initializing the other fifteen with 0 .也就是说,除非您认为它所做的是将存储在dlong long int值转换为char类型(产生一个实现定义的结果,其范围比d本身的范围小得多),使用以下命令初始化数组c的第一个元素该值,并用0初始化其他十五个。 I can't imagine what you would want to do with the result, but since you actually don't do anything with it, that's probably moot.我无法想象你想对结果做什么,但因为你实际上没有对它做任何事情,这可能没有实际意义。

As I observed myself,据我自己观察,

 printf("%x", c);

also probably does not do what you think it does.也可能不会做你认为它会做的事情。 Indeed, you cannot rely on it to do any particular thing, because its behavior is undefined.事实上,你不能依赖它来做任何特定的事情,因为它的行为是未定义的。 You are passing a pointer to the first element of c as the second argument, but a value of type unsigned int will be expected instead (based on the format).您将一个指向c的第一个元素的指针作为第二个参数传递,但需要一个unsigned int类型的值(基于格式)。 In any case, this neither "print[s] the array" nor tells you anything about what it contains .无论如何,这既不会“打印[s] 数组”也不会告诉您有关它包含的内容的任何信息。

I suspect that what you actually had in mind was to declare c as an array whose address -- not contents -- is that designated by base , truncated to a 16-byte-aligned address.我怀疑您真正想到的是将c声明为一个数组,其地址(而不是内容)是由base指定的,被截断为 16 字节对齐的地址。 You cannot do that, because you cannot specify the address of any variable you declare, but you can declare c as a pointer, like this:你不能这样做,因为你不能指定你声明的任何变量的地址,但你可以将c声明为一个指针,如下所示:

unsigned char *c = d;

(Oh no, more pointers!) There's some implementation-dependency there, but it probably has the result I think you want. (哦,不,更多的指针!)那里有一些实现依赖,但它可能有我认为你想要的结果。 Or if you want to be really clever, you might do this:或者,如果你想变得非常聪明,你可以这样做:

unsigned char (*c16)[16] = d;

That declares c16 as a pointer to an array of 16 unsigned char .这将c16声明为指向 16 个unsigned char数组的指针。 It's as close as you can get to declaring an array at an address specified by you.它尽可能接近在您指定的地址处声明一个数组。 I suspect you'll find it easier to work with the other declaration, however.但是,我怀疑您会发现使用其他声明更容易。

If you want to print the contents of the memory to which such a pointer points (as a "memory dump" function seems wont to do) then you'll need to do a little more work.如果你想打印这样一个指针指向的内存的内容(因为“内存转储”函数似乎不会这样做),那么你需要做更多的工作。 The standard library's formatted I/O functions do not provide directly for printing arrays (for good reasons that I'll not go into here), except C strings, and you do not appear to want to print the data as a C string.除了 C 字符串外,标准库的格式化 I/O 函数不直接提供用于打印数组的功能(出于很好的原因,我不会在这里讨论),并且您似乎不想将数据打印为 C 字符串。 Do, however, consider this call, and how you might modify it for or adapt it to your purpose (assuming my above declaration for c ):但是,请考虑此调用,以及如何根据您的目的修改或调整它(假设我上面对c声明):

printf("%02x", *c);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM