sizeof

Potential problem with C standard malloc'ing chars

本小妞迷上赌 提交于 2019-11-29 15:09:17
问题 When answering a comment to another answer of mine here, I found what I think may be a hole in the C standard (c1x, I haven't checked the earlier ones and yes, I know it's incredibly unlikely that I alone among all the planet's inhabitants have found a bug in the standard). Information follows: Section 6.5.3.4 ("The sizeof operator") para 2 states "The sizeof operator yields the size (in bytes) of its operand" . Para 3 of that section states: "When applied to an operand that has type char,

引用的本质分析

吃可爱长大的小学妹 提交于 2019-11-29 14:59:47
目录 1. 引用的定义 2. 引用的本质 3. 引用的意义 4. 特殊的引用—const引用 5. 引用和指针的关系 1. 引用的定义 C++新增加了引用的概念: 引用可以看作一个已定义变量的别名 引用的语法 Type &name = var; int a = 4; int &b = a; //b为a的别名 b = 5; //操作b就是操作a 2. 引用的本质 引用在C++中的内部实现是一个常量指针 Type &name <==> Type *const name C++编译器在编译过程中使用常量指针作为引用的内部实现,因此引用所占用的内存大小和指针相同 从使用的角度,引用只是一个别名, C++为了实用性而隐藏了引用的存储空间这一细节 #include <cstdio> struct TRef { char &r; }; int main(int argc, char *argv[]) { char c = 'c'; char &rc = c; TRef ref = { c }; printf("sizeof(rc) = %d\n", sizeof(rc)); printf("sizeof(TRef) = %d\n", sizeof(TRef)); printf("sizeof(ref) = %d\n", sizeof(ref)); printf("sizeof(ref.r) =

sizeof continues to return 4 instead of actual size

99封情书 提交于 2019-11-29 14:46:34
#include <iostream> using namespace std; int main() { cout << "Do you need to encrypt or decrypt?" << endl; string message; getline(cin, message); int letter2number; for (int place = 1; place < sizeof(message); place++) { letter2number = static_cast<int>(message[place]); cout << letter2number << endl; } } Examples of problem: I type fifteen letters but only four integers are printed. I type seven letters but only four integers are printed. The loop only occurs four times on my computer, not the number of characters in the string. This is the only problem I am having with it, so if you see

What does sizeof(int[1]) mean?

人盡茶涼 提交于 2019-11-29 14:29:32
问题 I am new to the Linux kernel. I am reading the file ioctl.h , there I encountered a macro _IOC_TYPECHECK(t) , which looks like this: #define _IOC_TYPECHECK(t) \ ((sizeof(t) == sizeof(t[1]) && \ sizeof(t) < (1 << _IOC_SIZEBITS)) ? \ sizeof(t) : __invalid_size_argument_for_IOC) Can you explain me this code? In this code, what does sizeof(t[1]) mean? 回答1: This is used to check the validity of the third parameter to the _IOR / _IOW / _IOWR macros, which is supposed to be a type. It checks that

C++ Size of Array [duplicate]

↘锁芯ラ 提交于 2019-11-29 14:02:32
Possible Duplicate: Sizeof array passed as parameter I am being stupid with this sizeof operator in c++, do you have any idea why it is 4 and 12 ? void function (int arg[]) { cout<<sizeof(arg)<<endl; // 4 } int main () { int array[] = {1, 2, 3}; cout<<sizeof array<<endl; // 12 function (array); return 0; } In main , the name array is an array so you get the size in bytes of the array with sizeof . However, an array decays to a pointer when passed to a function, so you get sizeof(int*) inside the function. Be aware that taking an argument in the form of T arg[] is exactly the same as taking the

Why does sizeof(x++) not increment x?

烂漫一生 提交于 2019-11-29 13:53:39
Here is the code compiled in dev c++ windows: #include <stdio.h> int main() { int x = 5; printf("%d and ", sizeof(x++)); // note 1 printf("%d\n", x); // note 2 return 0; } I expect x to be 6 after executing note 1 . However, the output is: 4 and 5 Can anyone explain why x does not increment after note 1 ? pmg From the C99 Standard (the emphasis is mine) 6.5.3.4/2 The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand

Linux socket编程示例

左心房为你撑大大i 提交于 2019-11-29 13:52:04
一、socket编程    网络功能是Uinux/Linux的一个重要特点,有着悠久的历史,因此有一个非常固定的编程套路。   基于TCP的网络编程:     基于连接, 在交互过程中, 服务器和客户端要保持连接, 不能断开。重发一切出错数据、数据验证, 保证数据的正确性、完整性和顺序性,     缺点是消耗的资源比较大。   基于UDP的网络编程:     无连接协议, 在网络交互过程中不保持连接, 只需要在发送数据时连接一下, 不重发、验证数据。优点是资源消耗少, 数据的可靠性完整性     顺序性得不到保证。 二、编程步骤:     服务器:       ① 创建socket(套接字) socket()       ② 准备通信地址       ③ 将创建的socket和通信地址绑定 bind()       ④ 监听端口 listen()       ⑤ 等待客户端连接 accpet()       ⑥ 通信双方收发数据 read()/write()                  send()/recv()       ⑦ 关闭socket     客户端:       ① 创建socket(套接字) socket()       ② 准备通信地址       ③ 连接服务器 connect()       ④ 收发数据 read()/write()          

What is the sizeof std::array<char, N>? [duplicate]

不打扰是莪最后的温柔 提交于 2019-11-29 13:45:11
This question already has an answer here: Is the size of std::array defined by standard 1 answer What does the C++ standard say about what sizeof(std::array<char, N>) should be (for some constant N )? In a comment to a different question , it was mentioned that std::array is not always "stack allocated". The comment was in response to a different comment that speculated that putting a too large of a constant for std::array that is declared as a local variable could cause the program to abort due to insufficient resources for the "stack allocated" variable. I assume the followup comment meant

C++ -- 第一次作业

╄→尐↘猪︶ㄣ 提交于 2019-11-29 12:36:30
运算符(sizeof 及 运算符的转换) sizeof sizeof 是一个关键字,它是一个编译时运算符,用于判断变量或数据类型的字节大小。 sizeof 运算符可用于获取类、结构、共用体和其他用户自定义数据类型的大小。 使用 sizeof 的语法如下: sizeof ( data type ); 实例: #include <iostream> #include <iostream> using namespace std ; int main () { cout << "Size of char : " << sizeof ( char ) << endl ; cout << "Size of int : " << sizeof ( int ) << endl ; cout << "Size of short int : " << sizeof ( short int ) << endl ; cout << "Size of long int : " << sizeof ( long int ) << endl ; cout << "Size of float : " << sizeof ( float ) << endl ; cout << "Size of double : " << sizeof ( double ) << endl ; cout << "Size of

Why do books say, “the compiler allocates space for variables in memory”?

自闭症网瘾萝莉.ら 提交于 2019-11-29 12:35:22
问题 Why do books say, "the compiler allocates space for variables in memory". Isn't it the executable which does that? I mean, for example, if I write the following program, #include <iostream> using namespace std; int main() { int foo = 0; cout<<foo; return 0; } and compile it, and get an executable (let it be program.exe), now, if I run program.exe, this executable file will itself command to allocate some space for the variable foo. Won't it ? Please explain why books keep on saying, "the