sizeof

Sizeof returning incorrect array length [duplicate]

冷暖自知 提交于 2019-12-19 12:08:50
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Sizeof an array in the C programming language? I've been fiddling with C to become better acquainted with it and think I may have stumbled upon a initialization/pointer issue that I'm unsure of how to resolve. The below program is an implementation of ROT13, so it takes an input string, and shifts each letter by 13, resulting in the cipher text. The output of my program displays the correct shift, but it won't

zeroing derived struct using memset

倾然丶 夕夏残阳落幕 提交于 2019-12-19 11:26:51
问题 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 { //

zeroing derived struct using memset

强颜欢笑 提交于 2019-12-19 11:26:37
问题 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 { //

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

巧了我就是萌 提交于 2019-12-19 10:41:06
问题 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!!! 回答1: 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. 回答2: On some platforms there is

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

余生颓废 提交于 2019-12-19 10:15:58
问题 #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? 回答1: The number 2147483648 is too large to fit into an int , so it is promoted to long . Then, after the number has already been

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

一曲冷凌霜 提交于 2019-12-19 10:15:30
问题 #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? 回答1: The number 2147483648 is too large to fit into an int , so it is promoted to long . Then, after the number has already been

Socket网络编程--小小网盘程序(5)

核能气质少年 提交于 2019-12-19 08:59:46
  各位好呀!这一小节应该就是这个小小网盘程序的最后一小节了,这一节将实现最后的三个功能,即列出用户在服务器中的文件列表,还有删除用户在服务器中的文件,最后的可以共享文件给好友。   列出用户在服务器中的文件列表   增加一个结构体 1 struct FileList 2 { 3 int cnt; 4 char list[16][128]; 5 };   为了方便我就假设服务器最多可以存16个单个用户的文件。如果想要支持更多的文件,这里可以增加一个int pages;用于分页作用,我们在服务器中获取文件时,可以根据分页进行发送。这样既方便又能支持多文件。   client.cpp这个客户端文件增加一个函数 1 int file_list(struct Addr addr,struct User user) 2 { 3 struct sockaddr_in servAddr; 4 struct hostent *host; 5 struct Control control; 6 struct FileList filelist; 7 int sockfd; 8 9 host=gethostbyname(addr.host); 10 servAddr.sin_family=AF_INET; 11 servAddr.sin_addr=*((struct in_addr *)host->h

Why does 'sizeof' give wrong measurement? [duplicate]

Deadly 提交于 2019-12-19 07:39:09
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: struct sizeof result not expected I have this C++ struct: struct bmp_header { //bitmap file header (14 bytes) char Sign1,Sign2; //2 unsigned int File_Size; //4 unsigned int Reserved_Dword; //4 unsigned int Data_Offset; //4 //bitmap info header (16 bytes) unsigned int Dib_Info_Size; //4 unsigned int Image_Width; //4 unsigned int Image_Height; //4 unsigned short Planes; //2 unsigned short Bits; //2 }; It is

文件放过进剪贴板

一曲冷凌霜 提交于 2019-12-19 03:02:32
procedure CopyFilesToClipboard(FileList: string); var DropFiles: PDropFiles; hGlobal: Thandle; iLen: Integer; begin iLen := Length(FileList) * SizeOf(Char) + 2; FileList := FileList + #0#0; hGlobal := GlobalAlloc(GMEM_SHARE or GMEM_MOVEABLE or GMEM_ZEROINIT, SizeOf(TDropFiles) + iLen); if (hGlobal = 0) then raise Exception.Create('Could not allocate memory.'); begin DropFiles := GlobalLock(hGlobal); DropFiles^.pFiles := SizeOf(TDropFiles); DropFiles^.pt.X := 0; DropFiles^.pt.Y := 0; DropFiles^.fNC := False; DropFiles^.fWide := true; Move(FileList[1], (PAnsiChar(DropFiles) + SizeOf(TDropFiles))

Adding a default constructor to a base class changes sizeof() a derived type [duplicate]

此生再无相见时 提交于 2019-12-19 01:10:07
问题 This question already has answers here : When extending a padded struct, why can't extra fields be placed in the tail padding? (4 answers) Closed last year . I tend to think I have a pretty good grasp of C++ internals and memory layouts, but this one has me baffled. I have the following test code: #include <stdio.h> struct Foo { //Foo() {} int x; char y; }; struct Bar : public Foo { char z[3]; }; int main() { printf( "Foo: %u Bar: %u\n", (unsigned)sizeof( Foo ), (unsigned)sizeof( Bar ) ); }