memory-layout

Precise memory layout control in Rust?

时光怂恿深爱的人放手 提交于 2021-02-05 20:53:11
问题 As far as I know, the Rust compiler is allowed to pack, reorder, and add padding to each field of a struct. How can I specify the precise memory layout if I need it? In C#, I have the StructLayout attribute, and in C/C++, I could use various compiler extensions. I could verify the memory layout by checking the byte offset of expected value locations. I'd like to write OpenGL code employing custom shaders, which needs precise memory layout. Is there a way to do this without sacrificing

What is the memory layout of structs, tuples and tuple structs?

做~自己de王妃 提交于 2021-01-27 05:54:46
问题 It is clear from the reference manual that the memory layout of structs is unspecified (when the repr attribute is not used). This rule gives the compiler the possibility to pack structures tighter by reordering the fields. What about the memory layout of tuples and tuple structs? How is it (un)specified and why? 回答1: The memory layout of tuples and tuple structs is undefined, just like the layout of normal structs, with one exception: The exception to this is the unit tuple ( () ) which is

Is it legal to cast a struct to an array?

给你一囗甜甜゛ 提交于 2020-12-06 04:15:32
问题 Consider the following: // Just a sequence of adjacent fields of same the type #[repr(C)] #[derive(Debug)] struct S<T> { a : T, b : T, c : T, d : T, } impl<T : Sized> S<T> { fn new(a : T, b : T, c : T, d : T) -> Self { Self { a, b, c, d, } } // reinterpret it as an array fn as_slice(&self) -> &[T] { unsafe { std::slice::from_raw_parts(self as *const Self as *const T, 4) } } } fn main() { let s = S::new(1, 2, 3, 4); let a = s.as_slice(); println!("s :: {:?}\n\ a :: {:?}", s, a); } Is this code

Is it legal to cast a struct to an array?

喜欢而已 提交于 2020-12-06 04:14:20
问题 Consider the following: // Just a sequence of adjacent fields of same the type #[repr(C)] #[derive(Debug)] struct S<T> { a : T, b : T, c : T, d : T, } impl<T : Sized> S<T> { fn new(a : T, b : T, c : T, d : T) -> Self { Self { a, b, c, d, } } // reinterpret it as an array fn as_slice(&self) -> &[T] { unsafe { std::slice::from_raw_parts(self as *const Self as *const T, 4) } } } fn main() { let s = S::new(1, 2, 3, 4); let a = s.as_slice(); println!("s :: {:?}\n\ a :: {:?}", s, a); } Is this code

significance of address 0x8048080

混江龙づ霸主 提交于 2020-08-19 17:00:33
问题 why when i debug asm source in gdb is 0x8048080 the address chosen for the starting entry point into code? this is just a relative offset, not an actual offset of into memory of an instruction, correct? 回答1: There is no special significance to address 0x8048080 , but there is one for address 0x08048000 . The latter address is the default address, on which ld starts the first PT_LOAD segment on Linux/x86. On Linux/x86_64, the default is 0x400000 , and you can change the default by using a

significance of address 0x8048080

好久不见. 提交于 2020-08-19 16:59:34
问题 why when i debug asm source in gdb is 0x8048080 the address chosen for the starting entry point into code? this is just a relative offset, not an actual offset of into memory of an instruction, correct? 回答1: There is no special significance to address 0x8048080 , but there is one for address 0x08048000 . The latter address is the default address, on which ld starts the first PT_LOAD segment on Linux/x86. On Linux/x86_64, the default is 0x400000 , and you can change the default by using a

How do I organize members in a struct to waste the least space on alignment?

房东的猫 提交于 2020-04-07 11:10:14
问题 [Not a duplicate of Structure padding and packing. That question is about how and when padding occurs. This one is about how to deal with it.] I have just realized how much memory is wasted as a result of alignment in C++. Consider the following simple example: struct X { int a; double b; int c; }; int main() { cout << "sizeof(int) = " << sizeof(int) << '\n'; cout << "sizeof(double) = " << sizeof(double) << '\n'; cout << "2 * sizeof(int) + sizeof(double) = " << 2 * sizeof(int) + sizeof(double

Clarification about Bit-field ordering semantics in C

不羁的心 提交于 2020-01-29 14:42:30
问题 I have troubles understanding the exact meaning of a paragraph of C99 draft standard (N1256) about bit-fields (6.7.2.1:10): 6.7.2.1 Structure and union specifiers [...] Semantics [...] An implementation may allocate any addressable storage unit large enough to hold a bit-field. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit

Clarification about Bit-field ordering semantics in C

让人想犯罪 __ 提交于 2020-01-29 14:38:09
问题 I have troubles understanding the exact meaning of a paragraph of C99 draft standard (N1256) about bit-fields (6.7.2.1:10): 6.7.2.1 Structure and union specifiers [...] Semantics [...] An implementation may allocate any addressable storage unit large enough to hold a bit-field. If enough space remains, a bit-field that immediately follows another bit-field in a structure shall be packed into adjacent bits of the same unit. If insufficient space remains, whether a bit-field that does not fit