sizeof

extern声明语句不会给变量分配存储空间吗?【待解决】

此生再无相见时 提交于 2019-11-27 08:16:21
[ 测试用例 ] #include <stdio.h> extern char c; extern int i; int main() { printf("%d %d\n", sizeof(c), sizeof(i)); return 0; } [ 期望输出 ] 两个一样的值 [ 实际输出 ] [ Q ] 如果没有分配存储空间,那么为什么结果却输出了变量所占用的空间大小。 [ A ] 疑问保留,如果问题解决,欢迎留言 zhyantao@126.com 来源: https://www.cnblogs.com/zhyantao/p/11353904.html

Getting the size of an indiviual field from a c++ struct field

与世无争的帅哥 提交于 2019-11-27 08:08:42
问题 The short version is: How do I learn the size (in bits) of an individual field of a c++ field? To clarify, an example of the field I am talking about: struct Test { unsigned field1 : 4; // takes up 4 bits unsigned field2 : 8; // 8 bits unsigned field3 : 1; // 1 bit unsigned field4 : 3; // 3 bits unsigned field5 : 16; // 16 more to make it a 32 bit struct int normal_member; // normal struct variable member, 4 bytes on my system }; Test t; t.field1 = 1; t.field2 = 5; // etc. To get the size of

c++ sizeof() of a class with functions

萝らか妹 提交于 2019-11-27 08:03:00
I have a C++ question. I wrote the following class: class c { int f(int x, int y){ return x; } }; the sizeof() of class c returns "1". I I really don't understand why it returns 1. Trying to understand better what is going on, I added another function: class c { int f(int x, int y){ return x; } int g(int x, int y){ return x; } }; Now the following really got me confused! sizeof(c) is still 1 (!?!?!?!). So I guess that functions doesn't change the size of the class, but why??? and why does the size is 1 ? And is it compiler specific ? Thanks! :-) The class contains no data members, so it's

What does sizeof (function(argument)) return?

家住魔仙堡 提交于 2019-11-27 07:45:24
问题 What will be the output of program #include <stdio.h> int fun(char *a){ printf("%d\n",sizeof(a)); return 1; } int main(){ char a[20]; printf("%d\n",sizeof (fun(a))); return 0; } 回答1: Except with variable length arrays, sizeof does not evaluate its operand. So it will just yield the size of fun(a) type, i.e. sizeof(int) (without calling the function). C11 (n1570) §6.5.3.4 The sizeof and _Alignof operators 2 [...] If the type of the operand is a variable length array type, the operand is

C2070 - illegal sizeof operand

本秂侑毒 提交于 2019-11-27 07:44:00
问题 The following code looks fine to me: #include <stdio.h> template <typename T> struct A { static float m_kA[]; }; template <typename T> float A<T>::m_kA[] = {1.0f, 2.0f, 3.0f}; int main() { printf("%d\n", sizeof(A<unsigned int>::m_kA) / sizeof(A<unsigned int>::m_kA[0])); return 0; } But when i compile with VC9 i get the following error error C2070: 'float []': illegal sizeof operand I would expect this code to compile. Am i missing something? Does anyone know a way to fix this strange behavior

What's sizeof(size_t) on 32-bit vs the various 64-bit data models?

爷,独闯天下 提交于 2019-11-27 07:12:45
On a 64-bit system, sizeof(unsigned long) depends on the data model implemented by the system, for example, it is 4 bytes on LLP64 (Windows), 8 bytes on LP64 (Linux, etc.). What's sizeof(size_t) supposed to be? Does it vary with data model like sizeof(long) does? If so, how? References: 64-bit data models on Wikipedia size_t is defined by the C standard to be the unsigned integer return type of the sizeof operator (C99 6.3.5.4.4), and the argument of malloc and friends (C99 7.20.3.3 etc). The actual range is set such that the maximum (SIZE_MAX) is at least 65535 (C99 7.18.3.2). However, this

Sizeof vs Strlen

我是研究僧i 提交于 2019-11-27 07:04:33
#include "stdio.h" #include "string.h" main() { char string[] = "october"; // october is 7 letters strcpy(string, "september"); // september is 9 letters printf("the size of %s is %d and the length is %d\n\n", string, sizeof(string), strlen(string)); return 0; } Output: the size of september is 8 and the length is 9 Is there something wrong with my syntax or what? Sean sizeof and strlen() do different things. In this case, your declaration char string[] = "october"; is the same as char string[8] = "october"; so the compiler can tell that the size of string is 8. It does this at compilation

Is there any reason not to use fixed width integer types (e.g. uint8_t)?

别来无恙 提交于 2019-11-27 07:03:52
Assuming you're using a compiler that supports C99 (or even just stdint.h), is there any reason not to use fixed width integer types such as uint8_t? One reason that I'm aware of is that it makes much more sense to use char s when dealing with characters instead of using (u)int8_t s, as mentioned in this question . But if you are planning on storing a number, when would you want to use a type that you don't know how big it is? I.e. In what situation would you want to store a number in a unsigned short without knowing if it is 8, 16, or even 32 bits, instead of using a uint16t ? Following on

图论 test solution

旧巷老猫 提交于 2019-11-27 07:01:09
图论 test solution T1:潜伏 题目背景 小悠回家之后,跟着母亲看了很多抗日神剧,其中不乏一些谍战片。 题目描述 解放前夕,北平城内潜伏着若干名地下党员,他们居住在城市的不同位置。现在身为地下党第一指挥官的你,想知道地下党员之间的最小通信距离,即从某一地下党员住处前往另一地下党员住处的距离的最小值。 我们可以将北平城视为一张N个点M条边的无向图,每条边连接两个点 ,且长度为 \(w_i\) 。 输入格式 每个测试点包含多组数据。 第一行,给出数据组数 ,之后依次输入每组数据。 每组数据的第一行,N,M,K,分别表示点数,边数,地下党员数。 之后M行,每 \(u_i,v_i,w_i\) 表示第i条边。 之后一行,K个整数代表地下党员所在结点。 结点编号为1到N,保证N>=K。 输出格式 对于每组数据,输出一行一个整数,表示地下党员之间的最小通信距离。 如果最小通信距离为∞,请输出-1代替。 样例输入 3 5 10 3 1 3 437 1 2 282 1 5 328 1 2 519 1 2 990 2 3 837 2 4 267 2 3 502 3 5 613 4 5 132 1 3 4 10 13 4 1 6 484 1 3 342 2 3 695 2 3 791 2 8 974 3 9 526 4 9 584 4 7 550 5 9 914 6 7 444 6 8

size of a datatype without using sizeof

我的未来我决定 提交于 2019-11-27 06:50:31
I have a data type, say X , and I want to know its size without declaring a variable or pointer of that type and of course without using sizeof operator. Is this possible? I thought of using standard header files which contain size and range of data types but that doesn't work with user defined data type. To my mind, this fits into the category of "how do I add two ints without using ++, += or + ?". It's a waste of time. You can try and avoid the monsters of undefined behaviour by doing something like this. size_t size = (size_t)(1 + ((X*)0)); Note that I don't declare a variable of type or