sizeof

OpenGL光照3:光照贴图

 ̄綄美尐妖づ 提交于 2019-11-27 12:56:45
本文是个人学习记录,学习建议看教程 https://learnopengl-cn.github.io/ 非常感谢原作者JoeyDeVries和多为中文翻译者提供的优质教程 的内容为插入注释,可以先跳过 前言 之前我们讨论了让每个物体都拥有自己独特的材质从而对光照做出不同的反应的方法,这样子能够很容易在一个光照的场景中给每个物体一个独特的外观,但是这仍不能对一个物体的视觉输出提供足够多的灵活性 我们将整个物体的材质定义为一个整体,但现实世界中的物体通常并不只包含有一种材质,而是由多种材质所组成,也就是说物体在不同的部件上都有不同的材质属性 所以,我们目前的材质系统是肯定不够的,它只是一个最简单的模型,所以我们需要拓展之前的系统,引入 漫反射 和 镜面光贴图(Map) ,这允许我们对物体的漫反射分量(以及间接地对环境光分量,它们几乎总是一样的)和镜面光分量有着更精确的控制 漫反射贴图 我们希望通过某种方式对物体的每个片段单独设置漫反射颜色,有能够让我们根据片段在物体上的位置来获取颜色值得系统吗? 这可能听起来很熟悉,而且事实上这个系统我们已经使用很长时间了,这听起来很像在之前详细讨论过的 纹理 ,其实的确就是如此,我们仅仅是 对同样的原理使用了不同的名字 :纹理和贴图其实都是使用一张覆盖物体的图像,让我们能够逐片段索引其独立的颜色值,在光照场景中,它通常叫做一个 漫反射贴图

C sizeof char* array

…衆ロ難τιáo~ 提交于 2019-11-27 12:49:08
问题 I have a char* array as follows: char *tbl[] = { "1", "2", "3" }; How do I use the sizeof operator to get the number of elements of the array, here 3? The below did work, but is it correct? int n = sizeof(tbl) / sizeof(tbl[0]) 回答1: Yes, size_t n = sizeof(tbl) / sizeof(tbl[0]) is the most typical way to do this. Please note that using int for array sizes is not the best idea. 回答2: The shorter and, arguably, cleaner version would look as sizeof tbl / sizeof *tbl :) 回答3: Yes, it will give you

Socket中数据的传输和 accept()、 connect()等函数的理解

谁都会走 提交于 2019-11-27 12:36:36
以下内容纯属个人理解,请大神勿喷,还请大神在评论区指教,谢谢 server.cpp #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/socket.h> #include <netinet/in.h> int main(){ //创建套接字 int serv_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //将套接字和IP、端口绑定 struct sockaddr_in serv_addr; memset(&serv_addr, 0, sizeof(serv_addr)); //每个字节都用0填充 serv_addr.sin_family = AF_INET; //使用IPv4地址 serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); //具体的IP地址 serv_addr.sin_port = htons(1234); //端口 bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); //进入监听状态,等待用户发起请求

Size of pid_t, uid_t, gid_t on Linux

我的梦境 提交于 2019-11-27 12:20:55
On Linux systems (either 32- or 64-bit), what is the size of pid_t , uid_t , and gid_t ? Dave #include <stdio.h> #include <sys/types.h> int main() { printf("pid_t: %zu\n", sizeof(pid_t)); printf("uid_t: %zu\n", sizeof(uid_t)); printf("gid_t: %zu\n", sizeof(gid_t)); } EDIT: Per popular request (and because, realistically, 99% of the people coming to this question are going to be running x86 or x86_64)... On an i686 and x86_64 (so, 32-bit and 64-bit) processor running Linux >= 3.0.0, the answer is: pid_t: 4 uid_t: 4 gid_t: 4 On intel architectures, sizes are defined in /usr/include/bits

What should be the sizeof(int) on a 64-bit machine? [duplicate]

半城伤御伤魂 提交于 2019-11-27 11:32:08
Possible Duplicate: size of int, long, etc Does the size of an int depend on the compiler and/or processor? What decides the sizeof an integer? I'm using a 64-bit machine. $ uname -m x86_64 $ file /usr/bin/file /usr/bin/file: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, stripped $ When I ran the following program, I got the sizeof(int) as 4-bytes . #include <stdio.h> int main(void) { printf("sizeof(int) = %d bytes\n", (int) sizeof(int)); return 0; } If I'm running a 16- , 32- and 64- bit machine, then doesn't it mean that the

sizeof class with int , function, virtual function in C++?

空扰寡人 提交于 2019-11-27 11:30:24
This is an online C++ test question, which has been done. #include<iostream> using namespace std; class A { }; class B { int i; }; class C { void foo(); }; class D { virtual void foo(); }; class E { int i ; virtual void foo(); }; class F { int i; void foo(); }; class G { void foo(); int i; void foo1(); }; class H { int i ; virtual void foo(); virtual void foo1(); }; int main() { cout <<"sizeof(class A) : " << sizeof(A) << endl ; cout <<"sizeof(class B) adding the member int i : " << sizeof(B) << endl ; cout <<"sizeof(class C) adding the member void foo() : " << sizeof(C) << endl ; cout <<

How can I print the result of sizeof() at compile time in C?

旧城冷巷雨未停 提交于 2019-11-27 11:19:50
How can I print the result of sizeof() at compile time in C? For now I am using a static assert (home brewed based on other web resources) to compare the sizeof() result to various constants. While this works... it is far from elegant or fast. I can also create an instance of the variable/struct and look in the map file but this is also less elegant and fast than a direct call/command/operator. Further, this is an embedded project using multiple cross-compilers... so building and loading a sample program to the target and then reading out a value is even more of a hassle than either of the

memset函数及其用法,C语言memset函数详解

被刻印的时光 ゝ 提交于 2019-11-27 10:44:08
在前面不止一次说过,定义变量时一定要进行初始化,尤其是数组和结构体这种占用内存大的数据结构。在使用数组的时候经常因为没有初始化而产生“烫烫烫烫烫烫”这样的野值,俗称“乱码”。 每种类型的变量都有各自的初始化方法,memset() 函数可以说是初始化内存的“万能函数”,通常为新申请的内存进行初始化工作。它是直接操作内存空间,mem即“内存”(memory)的意思。该函数的原型为: # include <string.h> void *memset(void *s, int c, unsigned long n); 函数的功能是:将指针变量 s 所指向的前 n 字节的内存单元用一个“整数” c 替换,注意 c 是 int 型。s 是 void* 型的指针变量,所以它可以为任何类型的数据进行初始化。 memset() 的作用是在一段内存块中填充某个给定的值。因为它只能填充一个值,所以该函数的初始化为原始初始化,无法将变量初始化为程序中需要的数据。用memset初始化完后,后面程序中再向该内存空间中存放需要的数据。 memset 一般使用“0”初始化内存单元,而且通常是给数组或结构体进行初始化。一般的变量如 char、int、float、double 等类型的变量直接初始化即可,没有必要用 memset。如果用 memset 的话反而显得麻烦。 当然,数组也可以直接进行初始化,但

Redis 内存管理 源码分析

拥有回忆 提交于 2019-11-27 10:34:18
要想了解redis底层的内存管理是如何进行的,直接看源码绝对是一个很好的选择 下面是我添加了详细注释的源码,需要注意的是,为了便于源码分析,我把redis为了弥补平台差异的那部分代码删了,只需要知道有这个东西便好 下面我会贴上两份源码:一份是我自己的,有删减添加了注释的,一部分是原生的,可以做个参考对照 redis内存管理部分的源码在 zmalloc.h文件和zmalloc.c文件 推荐文章: https://www.cnblogs.com/likui360/p/5272443.html https://www.cnblogs.com/likui360/p/5272975.html http://wiki.jikexueyuan.com/project/redis/memory-data-management.html 我的源码 zmalloc.h: #ifndef __ZMALLOC_H #define __ZMALLOC_H #define __xstr(s) __str(s) #define __str(s) #s #ifndef ZMALLOC_LIB #define ZMALLOC_LIB "libc" #endif /* CPU一次性能读取数据的二进制位数称为字长,也就是我们通常所说的32位系统(字长4个字节)、64位系统(字长8个字节)的由来。 所谓的8字节对齐

sizeof empty structure is 0 in C and 1 in C++ why? [duplicate]

别等时光非礼了梦想. 提交于 2019-11-27 10:18:08
问题 This question already has answers here : Closed 9 years ago . Possible Duplicates: Empty class in C++ What is the size of an empty struct in C? I read somewhere that size of an empty struct in C++ is 1. So I thought of verifying it. Unfortunately I saved it as a C file and used <stdio.h> header and I was surprised to see the output. It was 0. That means struct Empty { }; int main(void) { printf("%d",(int)sizeof(Empty)); } was printing 0 when compiled as a C file and 1 when compiled as a C++