sizeof

newbie questions about malloc and sizeof

喜欢而已 提交于 2019-12-17 02:50:50
问题 Can someone explain to me why my call to malloc with a string size of 6 returns a sizeof of 4 bytes? In fact, any integer argument I give malloc I get a sizeof of 4. Next, I am trying to copy two strings. Why is my ouput of the copied string (NULL)? Following is my code: int main() { char * str = "string"; char * copy = malloc(sizeof(str) + 1); printf("bytes allocated for copy: %d\n", sizeof(copy)); while(*str != '\0'){ *copy = *str; str++; copy++; } copy = '\0'; printf("%s\n", copy); } 回答1:

When a function has a specific-size array parameter, why is it replaced with a pointer?

拈花ヽ惹草 提交于 2019-12-16 18:34:28
问题 Given the following program, #include <iostream> using namespace std; void foo( char a[100] ) { cout << "foo() " << sizeof( a ) << endl; } int main() { char bar[100] = { 0 }; cout << "main() " << sizeof( bar ) << endl; foo( bar ); return 0; } outputs main() 100 foo() 4 Why is the array passed as a pointer to the first element? Is it a heritage from C? What does the standard say? Why is the strict type-safety of C++ dropped? 回答1: Yes it's inherited from C. The function: void foo ( char a[100]

C++之运算符优先级顺序表

随声附和 提交于 2019-12-16 15:48:31
[注] sizeof 的操作数不能是 C 风格转型:表达式 sizeof (int) * p 无歧义地解释成 (sizeof(int)) * p ,而非 sizeof((int)*p) 。 条件运算符中部( ? 与 : 之间)的表达式分析为如同其带有括号:忽略其相对于 ?: 的优先级。 另外: 运算符优先级 不受运算符重载影响 。 C++ 运算符优先级,详情参考 cppreference 。 [注] sizeof 的操作数不能是 C 风格转型:表达式 sizeof (int) * p 无歧义地解释成 (sizeof(int)) * p ,而非 sizeof((int)*p) 。 条件运算符中部( ? 与 : 之间)的表达式分析为如同其带有括号:忽略其相对于 ?: 的优先级。 另外: 运算符优先级 不受运算符重载影响 。 C++ 运算符优先级,详情参考 cppreference 。 来源: https://www.cnblogs.com/phillee/p/11540741.html

第69课.技巧:自定义内存管理

落爺英雄遲暮 提交于 2019-12-16 10:19:21
1.统计对象中某个成员变量的访问次数 注意:对象(普通对象,只读对象) eg: #include <iostream> #include <string> using namespace std; class Test { int m_value; int * const m_pCount; public: Test(int value = 0) : m_pCount(new int(0)) { m_value = value; } int getValue() const { *m_pCount = *m_pCount + 1; return m_value; } void setValue(int value) { *m_pCount = *m_pCount + 1; m_value = value; } int getCount() const { return *m_pCount; } ~Test() { delete m_pCount; } }; int main() { // 普通对象 Test t; t.setValue(100); cout << "t.m_value = " << t.getValue() << endl; cout << "t.m_count = " << t.getCount() << endl; // 只读对象 const Test ct

Why position of `[0]byte` in the struct matters?

放肆的年华 提交于 2019-12-14 03:53:24
问题 [0]byte in golang should not take any memory space. But these two structs have different sizes. type bar2 struct { A int _ [0]byte } type bar3 struct { _ [0]byte A int } So why the position of [0]byte matters here? By the way, I use unsafe.Sizeof() method to check the struct size. See the full example . 回答1: This is due to a tricky padding. First please allow me to slightly rename the structs and fields so it'll be easier to talk about them: type bar1 struct { A [0]byte I int } type bar2

Go语言学习(二)Go语言常量

时间秒杀一切 提交于 2019-12-14 01:52:31
定义:常量是一个简单值的标识符,在程序运行时,不会修改的量(常量是经常使用的量,一般情况下不会发生改变的) 1.常量的应用: 总结: 常量的关键字是const 2.常量可以用len(), cap(), unsafe.Sizeof()函数计算表达式的值。 常量表达式中,函数必须是内置函数,否则编译不过: 说明: 可以看出 Go 语言中 unsafe.Sizeof() 函数: (1)对不同长度的字符串,unsafe.Sizeof() 函数的返回值都为 16,这是因为 string 类型对应一个结构体,该结构体有两个域,第一个域指向该字符串的指针,第二个域为字符串的长度,每个域占 8 个字节,但是并不包含指针指向的字符串的内容,这就解释了unsafe.Sizeof() 函数对 string 类型的返回值始终是16。 (2)对不同长度的数组,unsafe.Sizeof() 函数的返回值随着数组中的元素个数的增加而增加,这是因为unsafe.Sizeof() 函数总是在编译期就进行求值,而不是在运行时,这就意味着,unsafe.Sizeof() 函数的返回值可以赋值给常量,在编译期求值,意味着可以获得数组所占的内存大小,因为数组总是在编译期就指明自己的容量,并且在以后都是不可变的。 (3)对所含元素个数不同的切片unsafe.Sizeof() 函数的返回值都为 24,这是因为对切片来说

Trying to Wrap My Head Around String Sizes in C

徘徊边缘 提交于 2019-12-13 19:32:13
问题 A friend and I are doing a C programming unit for college. We understand that there is no "string" per se in C, and instead, a string is defined by being an array of characters. Awesome! So when dealing with "strings" is obvious that a proper understanding arrays and pointers is important. We were doing really well understanding pointer declaration, when and when not to dereference the pointer, and played around with a number of printf 's to test our experiments. All with great success.

Determine the size of object without its virtual table pointers

主宰稳场 提交于 2019-12-13 15:19:26
问题 Is there a generic way (not platform dependent) to get at compile time the size of a class object in the memory, without counting the vtable pointers? 回答1: As you are asking for a portable way: class MyClass { private: struct S { DataMemberType1 dataMember1; ... DataMemberTypeN dataMemberN; } m; public: static const size_t MemberSize = sizeof(S); }; 回答2: Use sizeof on this class , it doesn't include size of the vtable just the pointer. 来源: https://stackoverflow.com/questions/28433973

sizeof applied to array types

江枫思渺然 提交于 2019-12-13 15:12:48
问题 The c11 standard says that sizeof, "when applied to an operand that has array type, the result is the total number of bytes in the array" (6.5.3.4, bullet 4). The foot note (103) says: "When applied to a parameter declared to have array or function type, the sizeof operator yields the size of the adjusted (pointer) type". I take from this that when applied to an array type, sizeof gives the size of the array (number of elements x size of elements), but applied to parameters declared to have

why constant size of struct despite having a vector of int

点点圈 提交于 2019-12-13 13:22:26
问题 I have defined a struct which contains a vector of integer. Then I insert 10 integers in the vector and check for the size of struct. But I see no difference. Here is my code: struct data { vector<int> points; } int main() { data d; cout << sizeof(d) << endl; for (int i=0; i< 10; ++i) d.points.push_back(i) cout << sizeof(d) << endl; In both the cases I am getting the same result : 16 Why is it so? Shouldn't the size of struct grow? 回答1: The sizeof operator is a compile time operation that