sizeof

Number of bits in a data type

守給你的承諾、 提交于 2019-12-03 08:40:01
I have two tasks for an assignment, one return the number of bits in type int on any machine. I thought I would write my function like so: int CountIntBitsF() { int x = sizeof(int) / 8; return x; } Does that look right? The second part is to return the number of any bits of any data type with a macro, and the macro can be taken from limits.h. I looked up limits.h on my machine, and also http://www.opengroup.org/onlinepubs/007908799/xsh/limits.h.html , but I don't think I really understand how any of those would return the number of bits in any data type. Any thoughts? Thanks. It's * , not / .

c# sizeof decimal?

匿名 (未验证) 提交于 2019-12-03 08:35:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Unclear on the sizeof for decimal types. Does the size in bytes vary by precision as in sql server? Is the precision variable for the c# type 'decimal'? I don't want to turn on unsafe code to just call sizeof on a decimal type. How would you approach this? 回答1: As others have said, decimal is always 16 bytes (128 bits). The precision of decimal is always 28/29 digits. It's a floating point type, unlike SQL's DECIMAL type. See my article on it for more details. 回答2: The decimal keyword indicates a 128-bit data type. Source: MSDN 回答3: The size

What is the return type of sizeof operator?

匿名 (未验证) 提交于 2019-12-03 08:33:39
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: What is the return type of sizeof operator? cppreference.com & msdn says sizeof returns size_t. Does it really return a size_t? I'm using VS2010 Professional, and targeting for x64. int main () { int size = sizeof ( int ); // No warning int length = strlen ( "Expo" ); //warning C4267: 'initializing' : conversion from 'size_t' to 'int', possible loss of data return 0 ; } I have this question because first line is not issuing any warning, whereas the second does. Even if I change it to char size, I don't get any warnings. 回答1: The

Loading and Saving vectors to a file

匿名 (未验证) 提交于 2019-12-03 08:30:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am using C++ Builder and I have a vector array of Appointment objects. I want to save it to and load it from a file. Currently, I am using ifstream and ofstream with binary files. I have a header that contains the size of the vector that will be saved alongside the data, so as to know its size when loading. Is serilization a better way to do this? If so, do I need to use the boost library, or another way? Here is my current code: class appointment { public: appointment(); appointment(TDateTime aDate, TDateTime aReminderDateTime, string

float bits and strict aliasing

匿名 (未验证) 提交于 2019-12-03 08:30:34
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am trying to extract the bits from a float without invoking undefined behavior. Here is my first attempt: unsigned foo(float x) { unsigned* u = (unsigned*)&x; return *u; } As I understand it, this is not guaranteed to work due to strict aliasing rules, right? Does it work if a take an intermediate step with a character pointer? unsigned bar(float x) { char* c = (char*)&x; unsigned* u = (unsigned*)c; return *u; } Or do I have to extract the individual bytes myself? unsigned baz(float x) { unsigned char* c = (unsigned char*)&x; return c[0] |

c++ sizeof( … )

匿名 (未验证) 提交于 2019-12-03 08:28:06
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: #include <cstdlib> #include <iostream> int main ( int argc , char * argv []) { cout << "size of String " << sizeof ( string ); system ( "PAUSE" ); return EXIT_SUCCESS ; } Output: size of String = 4 Does that mean that, since sizeof(char) = 1 Byte (0 to 255) , string can only hold 4 characters? 回答1: It isn't clear from your example what 'string' is. If you have: #include <string> using namespace std ; then string is std::string , and sizeof(std::string) gives you the size of the class instance and its data members, not the length of

神奇脑洞题解——HDU1853 Cyclic Tour

流过昼夜 提交于 2019-12-03 08:21:52
对于二分图的最大匹配和对于带权二分图的最优匹配,B站上 CSU ACM—ICPC 的大佬们讲的已经很清楚了。 有需求的同学们可以去B站上学习一下。 悄悄说一声,讲课的陈佳妮小姐姐也曾经是位OIer,曾在CTSC2016上夺得银牌(因为在HN,好像没能去成NOI),讲解的方式也令OIers十分舒适。 题目大意:给一张有向图,要求选择任意多个环(可重复,非自环,环可以任选,不用相连),使得每个点都被访问过,而且经过的边权之和最小,求最小值 。 乍一看和二分图没有关系,但是仔细想一下,如果给图中每个点拆成两个(一个连接这个点的入点,一个连出点),那么因为要走环,那么最优情况肯定是每个环中有一个点会被访问两次,其他点都是一次,那这不就是赤果果的在拆点图上求最优匹配吗? 假设我们有一张图:1——>2——>3——>1,设拆出来的点i,用来连接入点的是i,连接出点的是i+n(此处n=4),那么我们拆点图长这样:5——>2,6——>3, 7——>1,这样就实际上是对拆出来的六个点进行了一次完全匹配。 那么,我们只要先判断拆点图能不能形成完美匹配,然后再跑一边KM就可以了(KM算法要求二分图存在完美匹配,否则会陷入死循环)。 放代码吧: #include<iostream> #include<cstdio> #include<vector> #include<algorithm> #include

Why ENOUGH is enough? (storing an int in a char array)

匿名 (未验证) 提交于 2019-12-03 07:36:14
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: In one of the answers (and its comments) on How to convert an int to string in C the following solution is given char str[ENOUGH]; sprintf(str, "%d", 42); in the comments caf mentions that ENOUGH can be determined at compile time with: #define ENOUGH ((CHAR_BIT * sizeof(int) - 1) / 3 + 2) I get the + 2 because you need to be able to display the minus sign and null terminator but what is the logic behind the other part? Specifically CHAR_BIT ? 回答1: If int type is 32-bit, how many bytes do you need to represent any number (without sign and

DDOS 单例

百般思念 提交于 2019-12-03 06:26:52
DDOS.H #pragma once //g++ ../../../Main.cpp ../../../DDOS.cpp -lpthread #include <stdio.h> #include <ctype.h> #include <unistd.h> #include <fcntl.h> #include <signal.h> #include <sys/time.h> #include <sys/types.h> #include <sys/socket.h> #include <string.h> #include <netdb.h> #include <errno.h> #include <stdlib.h> #include <time.h> #include <arpa/inet.h> #include <pthread.h> // -lpthread // ./a.out www.baidu.com 80 struct ip { unsigned char hl; unsigned char tos; unsigned short total_len; unsigned short id; unsigned short frag_and_flags; unsigned char ttl; unsigned char proto; unsigned short

C++ 杂项

谁说胖子不能爱 提交于 2019-12-03 06:21:28
#include <iostream> #define Main main #define COLOR_GREEN system("color 2"); #include <vector> #include <list> #include <WinSock2.h> #include <WS2tcpip.h> #include <LM.h> #include <winnetwk.h> #include "BitMap.h" #include <wlanapi.h> #include "ATBAudioEngine/ATBAudioEngine.h" #pragma comment(lib,"Ws2_32.lib") #pragma comment(lib,"Mpr.lib") #pragma comment(lib,"netapi32.lib") #pragma comment(lib,"Wlanapi.lib") #include <Windows.h> class disorderly { int m_buf[10] = { 0 }; public: void initBuf() { srand(GetTickCount()); int len = sizeof(m_buf) / sizeof(int); for (int i = 0; i < len; i++) { m_buf