Last active
August 28, 2018 00:15
-
-
Save ChunMinChang/b76a61273374a1530bc4d6f3be6a7761 to your computer and use it in GitHub Desktop.
Size of struct/class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cassert> | |
typedef int NewType; | |
struct A { | |
NewType a; | |
}; | |
struct B { | |
NewType b; | |
int add2(int n) { | |
return n + 2; | |
} | |
}; | |
class C { | |
NewType c; | |
public: | |
C(NewType value): c(value) {} | |
}; | |
class D { | |
NewType d; | |
int add3(int n) { | |
return n + 3; | |
} | |
public: | |
D(NewType value): d(value) {} | |
int add5(int n) { | |
return add3(n) + 2; | |
} | |
}; | |
int main() { | |
A a = { 10 }; | |
B b = { .b = 20 }; | |
assert(sizeof(A) == sizeof(NewType)); | |
assert(sizeof(A) == sizeof(B)); | |
assert(sizeof(a) == sizeof(b) && sizeof(a) == sizeof(A)); | |
C c(30); | |
D d = D(40); | |
assert(sizeof(C) == sizeof(NewType)); | |
assert(sizeof(C) == sizeof(D)); | |
assert(sizeof(c) == sizeof(d) && sizeof(c) == sizeof(C)); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![allow(dead_code, unused_imports)] | |
use std::mem; | |
type NewType = u32; | |
struct A(NewType); | |
struct B { | |
inner: NewType, | |
} | |
struct C(NewType); | |
impl C { | |
pub fn foo(&self) { | |
println!("{}", self.sum(1, 2)); | |
} | |
fn sum(&self, a: u32, b: u32) -> u32 { | |
a + b | |
} | |
} | |
trait T { | |
fn bar(&self) -> i32; | |
} | |
struct D { | |
inner: NewType, | |
} | |
impl D { | |
pub fn foo(&self) { | |
println!("{}", self.sum(3, 4)); | |
} | |
fn sum(&self, a: u32, b: u32) -> u32 { | |
a + b | |
} | |
} | |
impl T for D { | |
fn bar(&self) -> i32 { | |
self.inner as i32 | |
} | |
} | |
struct E { | |
a: NewType, | |
b: NewType, | |
} | |
struct F { | |
a: NewType, | |
b: NewType, | |
} | |
impl F { | |
pub fn f1(&self) { | |
println!("{}", self.f2(4)); | |
} | |
fn f2(&self, n: u32) -> u32 { | |
n * n | |
} | |
} | |
impl T for F { | |
fn bar(&self) -> i32 { | |
self.f2(self.a + self.b) as i32 | |
} | |
} | |
#[test] | |
fn test_size() { | |
// The size of tuple struct with one element is same as | |
// the size of its inner element. | |
assert_eq!(mem::size_of::<NewType>(), mem::size_of::<A>()); | |
// The size of struct with one element is same as | |
// the size of its inner element. | |
assert_eq!(mem::size_of::<NewType>(), mem::size_of::<B>()); | |
// Adding methods to a struct doesn't enlarge the size | |
// of the struct. | |
assert_eq!(mem::size_of::<NewType>(), mem::size_of::<C>()); | |
// Adding trait methods to a struct doesn't enlarge the size | |
// of the struct. | |
assert_eq!(mem::size_of::<NewType>(), mem::size_of::<D>()); | |
assert_eq!(mem::size_of::<E>(), mem::size_of::<F>()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment