sizeof

明解C语言 入门篇 第七章答案

对着背影说爱祢 提交于 2019-12-04 23:42:28
练习7-1 #include <stdio.h> int main() { int n; printf("%d\t%d\t%d\n", sizeof 1,sizeof(unsigned)-1,sizeof n+2 ); //此行显示结果为 4 3 6 因为1的字节就是为4,而-1的字节也是4再减去-1所以显示为3,最后是n+2为6 printf("%d\t%d\t%d\n", sizeof +1, sizeof(double) - 1, sizeof(n + 2));//此行显示结果是 4 7 4 因为1的字节是4,double的字节长是8 -1是7,把(n+2)括起来之后使其为一个值,所以是4; printf("%d\t%d\t%d\n", sizeof - 1, sizeof((double)-1), sizeof (n + 2.0) );//此行结果是 4 8 8 因为1的字节是4,把double-1都括起来之后,就是double类型字节为8,同理最后也是8 } 练习7-2 #include <stdio.h> #include<math.h> int main() { unsigned int number; int x; int i; int number1; printf("请输入初始值:" ); scanf("%d", &number); number1 =

What is the return type of sizeof operator?

五迷三道 提交于 2019-12-04 23:37:50
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. C++11, §5.3.3 ¶6 The result of sizeof and sizeof... is a constant of type std:

Java与C++Socket通讯注意

醉酒当歌 提交于 2019-12-04 22:14:56
c++与java进行socket通信时注意事项 因为java发送的都是网络字节序(big-endium),而c++是主机字节序(little-endium),所以当消息中有整型,浮点型(应尽量避免使用)的时候需要用htonl,htons,ntohl,ntohs等函数转换一下,字符串由于是单字节排序的不需要转换,但应注意c++字符串是以'/0'作为结束符的,如果找不到'/0'可能会出现一些乱码,所以接收的时候可以分配一个length+1的buffer用来接收消息. 举例:c++ server, java client,假设开发的是c++ server,那么: java client--------->c++ server: c++ server需要调用ntohs,ntohl c++ server--------->java client: c++ server需要调用htons,htonl 至于浮点型可以使用以下的函数转换: float tcp_htonf(float f) { unsigned char *p, p0, p1; if(htons(1) ==1) return f; p =(unsigned char *)&f; p0 =p[0]; p1 =p[1]; p[0] =p[3]; p[3] =p0; p[1] =p[2]; p[2] =p1; return f; }

Does sizeof(T) == sizeof(const T) and alignof(T) == alignof(const T)

独自空忆成欢 提交于 2019-12-04 22:13:35
It seems reasonable to assume that T and const T would be two types that would be the same size and have the same alignment, but after thinking about some real systems, it seems that they could be different. Let me explain: Suppose you have a system with two types of memory: RAM and Flash (which is read only). The RAM is 8 bit addressable, while the Flash is only 16 bit addressable. Suppose this is T : struct T { uint8_t x; uint16_t y; }; In the byte addressable RAM this struct would be 3 bytes long.... but in the double byte addressable Flash (which is where a const variable would reside)

Internal mechanism of sizeof in C?

喜欢而已 提交于 2019-12-04 22:12:36
I use sizeof to get size of a struct in C, but the result I got is unexpected. struct sdshdr { int len; int free; char buf[]; }; int main(){ printf("struct len:%d\n",(sizeof(struct sdshdr))); return 0; } //struct len:8, with or without buf my question is why does buf not occupy any space and why is the size of the int type still 4 on a 64-bit CPU? here is the output from gcc -v : Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn) Target: x86_64-apple-darwin14.4

C-关键字

怎甘沉沦 提交于 2019-12-04 21:02:26
首先列出C语言中标准定义的32个关键字: 其中的sizeof值得好好说一下,相信大多数对C语言不是很熟悉的人和我一样,最开始都把sizeof当成了一个函数来看了。下面我们打开编译器一探究竟。 发现a,b,c的值都是4,在c中,sizeof后面没有括号,所以得出结论:sizeof绝非函数。 其实sizeof在计算变量所占空间大小时括号可以省略,而在计算类型大小时不能省略。且一般情况下,sizeof是在编译时求值,所以sizeof(i++)不会引起副作用,就相当于sizeof(i)。在一般情况下,还是不要偷懒,乖乖的加上括号,继续装成一个“函数”,哈哈哈... 还有一个比较有意思的关键字是——register。这个关键字请求编译器尽可能地将变量存在CPU内部寄存器中,而不是通过内存寻址访问以提高效率。寄存器就是一块块小的存储空间,只不过其存储速度比内存要快的多(相比于在一大块内存里寻找某个地址上的数据)。虽然register修饰符速度非常快,但是使用它也有限制。register变量必须是能被CPU寄存器所接受的类型,也就是说register变量必须是一个单个的值,并且其长度应小于或等于整型的长度,而且register变量可能不存放在内存中,所以不能用取地址符“&”来获取register变量的地址。 我们来看看static关键字,static关键字看起来很安静,其实一点也不安静

2019-2020-1 20175310 20175317 20175320 实验三 实时系统

好久不见. 提交于 2019-12-04 20:39:27
2019-2020-1 20175310 20175317 20175320 实验三 实时系统 小组成员 20175310 奚晨妍 20175317 钟睿文 20175320 龚仕杰 实验步骤 一、并发程序-1 学习使用Linux命令wc(1) 基于Linux Socket程序设计实现wc(1)服务器(端口号是你学号的后6位)和客户端 客户端传一个文本文件给服务器 服务器返加文本文件中的单词数 上方提交代码 附件提交测试截图,至少要测试附件中的两个文件 client.c #include<netinet/in.h> // sockaddr_in #include<sys/types.h> // socket #include<sys/socket.h> // socket #include<stdio.h> // printf #include<stdlib.h> // exit #include<string.h> // bzero #define SERVER_PORT 175317 #define BUFFER_SIZE 1024 #define FILE_NAME_MAX_SIZE 512 int main() { // 声明并初始化一个客户端的socket地址结构 struct sockaddr_in client_addr; bzero(&client_addr,

2019-2020-1 实验三-并发程序 20175229

余生长醉 提交于 2019-12-04 20:25:12
①实验内容: 一、实验三-并发程序-1 学习使用Linux命令wc(1) 基于Linux Socket程序设计实现wc(1)服务器(端口号是你学号的后6位)和客户端 客户端传一个文本文件给服务器 服务器返加文本文件中的单词数 上方提交代码 附件提交测试截图,至少要测试附件中的两个文件 二、实验三-并发程序-2 使用多线程实现wc服务器并使用同步互斥机制保证计数正确 上方提交代码 下方提交测试 对比单线程版本的性能,并分析原因 三、实验三-并发程序-3 交叉编译多线程版本服务器并部署到实验箱中 PC机作客户端测试wc服务器 提交测试截图 ②实验要求 1、提交实验报告博客,一组写一篇,实验中贡献小的写博客,贡献多的可以给出同组同学的博客链接。 2、博客标题:2017-2018-1 学号1 学号2 实验三 实时系统 3、实验目的,实验步骤 4、实验中的问题及解决过程 5、新学到的知识点 ③实验步骤 1、wc实现截图 wc相关内容: Linux系统中的wc(Word Count)命令 功能为统计指定文件中的字节数、字数、行数,并将统计结果显示输出。 命令格式: wc [选项]文件... 命令功能:统计指定文件中的字节数、字数、行数,并将统计结果显示输出。该命令统计指定文件中的字节数、字数、行数。如果没有给出文件名,则从标准输入读取。wc同时也给出所指定文件的总统计数。 命令参数 -c

2019-2020-1 20175313 20175328 20175329 实验三 实时系统的移植

Deadly 提交于 2019-12-04 20:13:05
2019-2020-1 20175313 20175328 20175329 实验三 实时系统的移植 实验目的 1.掌握uC/OSII(uCLinux..)的移植过程 2.掌握C,汇编的混合编程 实验仪器 嵌入式实验平台UP-TECH S24101 实验内容、步骤与体会: 实验内容 并发程序-1 学习使用Linux命令wc(1) 基于Linux Socket程序设计实现wc(1)服务器(端口号是你学号的后6位)和客户端 客户端传一个文本文件给服务器 服务器返加文本文件中的单词数 上方提交代码附件提交测试截图,至少要测试附件中的两个文件 首先先学习一下 命令wc 使用: 利用 man 命令查看 wc 命令使用 实验过程 实验代码 server #include<netinet/in.h> // sockaddr_in #include<sys/types.h> // socket #include<sys/socket.h> // socket #include<stdio.h> // printf #include<stdlib.h> // exit #include<string.h> // bzero #include<unistd.h> #define SERVER_PORT 155316 #define LENGTH_OF_LISTEN_QUEUE 20 #define

SSE图像算法优化系列三十:GIMP中的Noise Reduction算法原理及快速实现。

橙三吉。 提交于 2019-12-04 19:50:52
  GIMP源代码链接: https://gitlab.gnome.org/GNOME/gimp/-/archive/master/gimp-master.zip   GEGL相关代码链接: https://gitlab.gnome.org/GNOME/gegl/-/archive/master/gegl-master.zip   最近因为要研究下色温算法,顺便下载了最新的GIMP软件,色温算法倒是找到了(有空单独来讲下),也顺便看看GIMP都有些什么更新,嗯,更新还是蛮多的,界面UI上有很多改动,有些已经改的面目全非了。随便瞄了一下Enhance菜单,发现里面有一个Nosie Reduction算法,试了下,还有点效果。于是在github上下载了GIMP的源代码,可是在源代码里搜索相关的关键词确没有发现任何的相关代码,后来才发现很多东西都有个GEGL关键词,结果一百度,原来他是一个单独的软件包,于是有下载了GEGL的源代码,终于在gegl-master\operations\common\里面看到了noise-reduction.c文件。   其核心的代码如下: static void noise_reduction (float *src_buf, /* source buffer, one pixel to the left and up from the starting