sizeof

zeroing derived struct using memset

余生长醉 提交于 2019-12-01 11:50:34
I want to zero out all members of a derived structure. There are hundreds of members and more are added every once in a while so I feel that initializing them explicitly is error-prone. The structures have no virtual functions and all the member fields are built-in. However, they are not POD by virtue of having non-trivial constructors. Apart from the standard frowning on the practice, do you see any issues with the following? struct Base { // Stuff }; struct Derived : public Base { // Hundreds of fields of different built-in types // including arrays Derived() { ::memset(reinterpret_cast<char

How I return the size of the pointer that I have allocate with malloc?

微笑、不失礼 提交于 2019-12-01 10:56:54
See this example! int main( int argc, char ** argv ) { int *ptr = malloc(100 * sizeof (int)); printf("sizeof(array) is %d bytes\n", sizeof(ptr)); } The printf function return only 4 bytes! What is wrong? Thanks so much!!! Nothing is wrong. You are asking for, and getting, the size of the pointer on your platform. It is not in general possible to get the size of the memory block that a pointer points at, you must remember it yourself if you need it later. On some platforms there is the "msize" function that returns the size of an area allocated by malloc/calloc/strdup. But this is not standard.

空类,含有虚函数的类的大小

自作多情 提交于 2019-12-01 10:18:55
1、为何空类的大小不是0呢? 为了确保两个不同对象的地址不同,必须如此。 类的实例化是在内存中分配一块地址,每个实例都有独一无二的内存地址。空类也会实例化,为保证空类实例化后的独一无二性,编译器会给空类隐含的添加一个字节。所以,空类的sizeof为1,而不是0. 2、继承关系中的类大小: case 1: 父类有虚函数,子类继承。 class A{ virtual void f(){} }; class B:public A{} 此时,类A和类B都不是空类,其sizeof都是4,因为它们都具有虚函数表的地址。 case 2: 父类是空类,子类以虚基类方式继承。 class A{}; class B:public virtual A{}; 此时,A是空类,其大小为1;B不是空类,其大小为4.因为含有指向虚基类的指针。 case 3: 多重空类继承 class Father1{}; class Father2{}; class Child:Father1, Father2{}; 它们的sizeof都是1. 5、何时共享虚函数地址表: 如果派生类继承的第一个是基类,且该基类定义了虚函数地址表,则派生类就共享该表首址占用的存储单元。对于除前述情形以外的其他任何情形,派生类在处理完所有基类或虚基类后,根据派生类是否建立了虚函数地址表,确定是否为该表首址分配存储单元。 来源: https:/

c语言 sizeof 和 strlen

橙三吉。 提交于 2019-12-01 10:06:08
传送门:[ https://blog.csdn.net/follow_blast/article/details/79084726 ] strlen: 用来 计算字符串的长度,遇到第一个NULL('\0')为止 ’。 sizeof: 用来计算变量或者对象、类型所占字节的多少,占内存的大小 。 需要注意的是sizeof会把'\0'计算进去 strlen没有 对于数组,sizeof是计算该数组所占字节数,而不是数组元素个数。 strlen而言,不管是数组还是指针,只要遇到第一个‘\0’就为止 sizeof 是一个关键字不是函数 ,发生在编译时刻,所以可以用作常量表达式。 sizeof返回的值表示的含义如下: 1. 数组——编译时分配的数组空间大小; 2.指针——存储该 指针所用的空间大小(在32位系统是4,在64系统是8) ; 3.类型——该类型所占的空间大小; 4.对象——对象的实际占用空间大小; 5.函数——函数的返回类型所占的空间大小。函数的返回类型不能是void strlen 是一个函数,并且所传入的参数必须是char*,发生在运行时刻 sizeof只关心这块内存的大小,不关心这块内存存放了什么数据,strlen只关心这块内存存放的数据,不关心这块内存的大小,直到遇到第一个NULL为止。 #include<bits/stdc++.h> using namespace std;

Why size of int pointer is different of size of int array? [duplicate]

萝らか妹 提交于 2019-12-01 08:27:00
This question already has an answer here: How to find the 'sizeof' (a pointer pointing to an array)? 13 answers Lets be the following code: int x; int *p = &x; int t[3]; Then sizeof returns: sizeof(x) -> 4 sizeof(p) -> 8 sizeof(t) -> 12 I suppose that sizeof(t) is the result of 3 * sizeof(int) . But as t is a pointer to his first element, its size should be sizeof(p) . Why sizeof(t) returns the size of the memory block which represents the array ? Thanks. As the variable t is declared as having an array type int t[3]; then sizeof( t ) yields a value equal to 3 * sizeof( int ) The C Standard (6

Why is -2147483648 automatically promoted to long when it can fit in int?

▼魔方 西西 提交于 2019-12-01 08:23:40
#include <stdio.h> int main() { printf("%zu\n", sizeof(-2147483648)); printf("%zu\n", sizeof(-2147483647-1)); return 0; } The above code gives as output (gcc): 8 4 Why is -2147483648 automatically promoted to long in 1 st printf even when it can fit in an int ? Also, I tried the same in MinGW and it gives the output: 4 4 Can someone please explain what's going on? The number 2147483648 is too large to fit into an int , so it is promoted to long . Then, after the number has already been promoted to long , its negative is computed, yielding -2147483648. If you're curious, you can look at limits

D3D 9 绘制一个简单图形

两盒软妹~` 提交于 2019-12-01 08:19:10
#include <windows.h> #include<tchar.h> #include<d3d9.h> #pragma comment( lib, "d3d9.lib") #define null NULL #define RETURN return LPDIRECT3D9 g_pD3D = NULL; LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; struct CUSTOMVERTEX { float x, y, z, rhw; DWORD color; }; #define FVF ( D3DFVF_XYZRHW | D3DFVF_DIFFUSE ) HRESULT InitD3D(HWND hWnd) { g_pD3D = Direct3DCreate9(D3D_SDK_VERSION); D3DPRESENT_PARAMETERS d3dpp; ZeroMemory(&d3dpp, sizeof(d3dpp)); d3dpp.Windowed = TRUE; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; g_pD3D-

Please explain the output of sizeof()

最后都变了- 提交于 2019-12-01 08:01:49
I am new in c programming, and I am trying to understand the sizeof function. Please help me understand how this program works: #include<stdio.h> main( ) { printf ( "\n%d %d %d", sizeof ( '3'), sizeof ( "3" ), sizeof ( 3 ) ) ; } I am getting output as 4 2 4 . However, I am not able to understand the reason I get this output. Kindly explain it. sizeof ( '3') , it is the size of character constant which is int so you are getting value as 4 on your machine. sizeof ( "3" ) , it is size of string i.e. 2 . String "3" is made of 2 character ('3'+'\0') = "3" . And we know sizeof(char) is 1. sizeof ( 3

Why size of int pointer is different of size of int array? [duplicate]

一个人想着一个人 提交于 2019-12-01 07:45:46
问题 This question already has answers here : How to find the 'sizeof' (a pointer pointing to an array)? (13 answers) Closed 2 years ago . Lets be the following code: int x; int *p = &x; int t[3]; Then sizeof returns: sizeof(x) -> 4 sizeof(p) -> 8 sizeof(t) -> 12 I suppose that sizeof(t) is the result of 3 * sizeof(int) . But as t is a pointer to his first element, its size should be sizeof(p) . Why sizeof(t) returns the size of the memory block which represents the array ? Thanks. 回答1: As the

Linux网络编程三、 IO操作

人盡茶涼 提交于 2019-12-01 07:13:32
  当从一个文件描述符进行读写操作时,accept、read、write这些函数会阻塞I/O。在这种会阻塞I/O的操作好处是不会占用cpu宝贵的时间片,但是如果需要对多个描述符操作时,阻塞会使同一时刻只能处理一个操作,从而使程序的执行效率大大降低。一种解决办法是使用多线程或多进程操作,但是这浪费大量的资源。另一种解决办法是采用非阻塞、忙轮询,这种办法提高了程序的执行效率,缺点是需要占用更多的cpu和系统资源。所以,最终的解决办法是采用IO多路转接技术。   IO多路转接是先构造一个关于文件描述符的列表,将要监听的描述符添加到这个列表中。然后调用一个阻塞函数用来监听这个表中的文件描述符,直到这个表中有描述符要进行IO操作时,这个函数返回给进程有哪些描述符要进行操作。从而使一个进程能完成对多个描述符的操作。而函数对描述符的检测操作都是由系统内核完成的。   linux下常用的IO转接技术有:select、poll和epoll。 select:   头文件:#include <sys/select.h>、#include <sys/time.h>、#include <sys/types.h>、#include <unistd.h>   函数:     int select(int nfds, fd_set *readfds, fd_set *writefds,fd_set