更改存储值后修改了Malloc尺寸?
我有一个程序,该程序使用malloc为我的程序分配了一个空白的空间 我传递给malloc的值为1,因此应该分配1个字节。
现在,我将指针投入到int并将其值修改为int(例如,280)。 我很确定INT需要存储4个字节的记忆,我知道一个事实 280至少由2个字节表示,
我的期望是,由于我只有1个字节大小的指针,所以整个整数 我不适合那个空间,我认为会有错误或其他东西(没有) 然后,我认为整数从指针开始并超过分配的内存开始存储为RAM,并且由于我将打印分配的内存的指针,因此
我应该得到一个代表数字的整个第一字节的值(在这种情况下:24)
但是:
当我尝试打印指针的价值时,
我的想法仍然是280,我想以某种方式为该指针自动分配了更多的尺寸,
但我也认为这很奇怪,任何人都可以解释什么是什么在这里发生?
我还想知道如何仅存储280的第一个字节
#include <iostream>
int main() {
void* p = malloc(1); // This should allocate 1 byte
*(int*)p = 280; // This should cast p to an integer, dereference it and set value to 280
std::cout << *(int*)p << std::endl; // This prints 280 but I think it should print 24
free(p);
}
。他们的实际代码
编辑:
我正在使用mingw32-g ++来编译应用程序
GCC版本6.3.0(mingw.org gcc-6.3.0-1)
I have a program that uses malloc to allocate a void-typed space for my program
the value I pass to malloc is 1 so it should allocate 1 byte.
Now I cast the pointer to int and modify it's value to int (eg, 280).
I am pretty sure that an int needs 4 bytes of memory to be stored, and I know for a fact that
280 is represented by at least 2 bytes
My expectations are that since I only have a pointer of 1 byte size, the whole integer
wouldn't fit in that space, I thought that there would be an error or something (there were none)
Then I thought that the integer was stored to RAM starting from the pointer start and exceeding the allocated memory, and since I would print the pointer of the allocated memory
I should get a value that represents the whole 1st byte of the number (in this case: 24)
BUT:
When I try to print the value of the pointer the value is still 280
Now what I am thinking is that somehow the program auto-allocates more size for that pointer
But I also think that's weird, could anyone explain what is happening here?
I would also like to know how to store ONLY the 1st byte of 280.
#include <iostream>
int main() {
void* p = malloc(1); // This should allocate 1 byte
*(int*)p = 280; // This should cast p to an integer, dereference it and set value to 280
std::cout << *(int*)p << std::endl; // This prints 280 but I think it should print 24
free(p);
}
The above is a pseudo-code that should do what a class I made does, instead of sending the whole class I just replaced the constructor, operator=, destructor to their actual code
EDIT:
I am using mingw32-g++ to compile the application
gcc version 6.3.0 (MinGW.org GCC-6.3.0-1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Malloc仅分配一个字节,但没有任何机制可以避免您在其他内存地址上编写的编写,通过在P的地址编写4个字节,您可以编写分配的字节 + 3个连续的字节。
之后,当您尊重指针时,您会读4个字节,而4个字节与您刚刚编写的指针相同。
您正在做的是在程序可能不使用的内存上编写和阅读,但这是不确定的行为,您不应该这样做,因为它可能导致细分故障。
Malloc only allocates one byte but has no mechanism to avoid that you write on other memory addresses, by writing 4 bytes in the address of p you write the allocalted byte + 3 other consecutive bytes.
After that when you deference the pointer you read 4 bytes that are the same ones you just wrote.
What you are doing is writing and reading on memory that the program probably isn't using, but this is undefined behaviour and you shouldn't ever do it as it can lead to segmentation faults.