297 questions
1
vote
1
answer
54
views
What are "array increments" and "row-major order" in Guile Scheme?
In the documentation for the array_copy function in Guile Scheme is a warning that in the copy, "the array increments may not be the same as those of src". (Where "src" is the ...
0
votes
0
answers
93
views
I am confused by mdspans' "layout policy", "layout mapping" and "layout mapping policy"
I'm looking at the newly-introduced std::mdspan class template (also described here on SO). One of the template parameters is, according to cppreference:
LayoutPolicy - specifies how to convert ...
15
votes
1
answer
916
views
What is correct mental model for [[no_unique_address]] in C++?
I recently found out about [[no_unique_address]] attribute in C++. According to cppreference.com:
Applies to the name being declared in the declaration of a non-static data member that is not a bit-...
9
votes
1
answer
1k
views
What's the difference between #[repr(Rust)], #[repr(C)] and #[repr(packed)]?
I'm trying to understand the difference between these three representations.
So far I've figured out how #[repr(Rust)] and #[repr(C)] differ:
#[repr(Rust)] is a default behavior while initializing, ...
5
votes
4
answers
413
views
Making a C++ struct that has the memory layout of an array
I have a set of structs that looks something like
struct A
{
int x;
int y;
};
struct B
{
int x;
int y;
int z;
};
struct Top
{
A a;
B b;
int* getPtr() { return &a.x; }
}...
4
votes
2
answers
82
views
Two zero bytes instead of three to signal End-of-BASIC?
Jim Butterfield's "Machine Language for Commodore Machines" book states at page 92 in the chapter on BASIC Memory Layout that (emphasis mine):
End-of-BASIC is signaled by three zero bytes ...
1
vote
1
answer
223
views
Why are bitfields not tightly packed in a struct? [closed]
If I have this struct:
struct Tuple {
unsigned int a : 4;
unsigned int b : 4;
};
I expected it to have a size of one bytes with both values being packed together, but on any compiler I have ...
2
votes
1
answer
92
views
How to get `MemoryLayout` for each member/property of a Type?
Motivation
I'm trying to establish an automatic sanity test algorithm which would raise a warning (via Swift Testing) when an arbitrary Type's MemoryLayout is potentially — it depends on the situation ...
1
vote
1
answer
503
views
Privacy property of inherited members affects overall size of class?
Running this code below, the sizeof base class and derived class are both 32.
#include "iostream"
#include "string"
using namespace std;
class base{
//public:
int ...
1
vote
1
answer
89
views
Stack Memory Layout
I understand stack moves from Higher memory address to Lower memory address,To find the size of the structure without using any library calls i used below logic,
#include <stdio.h>
typedef ...
1
vote
0
answers
101
views
Handling the .bss Section in Raw Binary Formats and Freestanding Environments
When working in a raw/freestanding environment, I have a question about the .bss section in ELF files. If I link and set the output format to binary (not ELF), where will the .bss section be in the ...
2
votes
1
answer
305
views
How and why does Rust's Option<Vec<T>> get optimized to 24 bytes?
I was surprised that Option<Vec<T>> has the same size as Vec<T>:
fn main() {
println!(
"u128: {} -> {}",
size_of::<u128>(),
size_of::<Option<...
3
votes
2
answers
309
views
How to Replace Unsafe with VarHandle or Foreign API
I've had a hard time removing usages of Unsafe and replacing with either VarHandle or MemorySegment/MemoryLayout. The goal is to remove usages of Unsafe and replace with nondeprecated API's.
Two ...
3
votes
1
answer
109
views
Create type with same layout and niches but no Drop impl
I ran into the following issue today:
use std::mem::{size_of, MaybeUninit};
struct Foo<'a> {
foo: &'a i32,
}
fn main() {
println!("{}", size_of::<Option<Foo>>()...
1
vote
2
answers
287
views
Error in cv2.rectangle after flipping image with slicing: 'Layout of the output array img is incompatible with cv::Mat'
I am using OpenCV to draw a rectangle on a binary image. My goal is to draw the rectangle on both the original image and its vertically flipped version. However, I encounter an issue when flipping the ...