sizeof

sizeof struct with union [duplicate]

你说的曾经没有我的故事 提交于 2019-12-11 05:48:57
问题 This question already has answers here : How is the size of a C++ class determined? (4 answers) Closed 5 years ago . I'm very confused by the whole 'data alignment' thing: #include <stdio.h> int main(){ struct st{ int i,*p; char c; union { char type[4]; unsigned char d;} un; }; struct st s1={25,&a,'X',"asdf"}; printf("sizeof s1 is %d",sizeof(s1)); return 0; } due to data alignment, i thought that since the sizes of int i : 4 bytes int *p : 8 bytes char c : 1 byte(+3) union : 4 bytes the

converting size_t into long, Is there any disadvantage?

依然范特西╮ 提交于 2019-12-11 04:59:36
问题 Is there any disadvantage of converting size_t to long? Because, I am writing an program that maintains linked_list in a file. So I traverse to another node based on size_t and I also keep track of total number of lists as size_t. Hence, obviously there is going to be some conversion or addition of long and size_t. Is there any disadvantage of this? If there is then I will make everything as long instead of size_t, even the sizes. Please advise. 回答1: It's not a problem now, but it may be in

Safe assumption regarding size and alignment of datatypes on different platforms?

扶醉桌前 提交于 2019-12-11 03:56:53
问题 Let's say I will only be using types with explicit width, e.g. no int but int32, uint16, etc... is it safe to assume that: A byte will always take 8 bits and will be 8 bit aligned A short will always take 16 bits and will be 16 bit aligned An int will always take 32 bits and will be 32 bit aligned A long int will always take 64 bits and will be 64 bit aligned A float will always take 32 bits and will be 32 bit aligned A double will always take 64 bits and will be 64 bit aligned --- and also

VBA Equivalent of Sizeof()?

半城伤御伤魂 提交于 2019-12-11 03:46:10
问题 Is there an equivalent of the C++ sizeof function in VBA? The only remotely similar functions seem to be the Ubound and LBound operators on arrays. Dim arr(1 to 4) as integer MsgBox Ubound(arr) But this is not really the same thing as the C++ code: int arr[10]; std::cout << sizeof(std::string) << "\t" << sizeof(arr); 回答1: The few pointer-related functions that can be used in VBA are at: http://support.microsoft.com/kb/199824 No obvious equivalent of sizeof though For an array, you could

How do I fix “The program issued a command but the command length is incorrect.” error when calling Process32First()?

扶醉桌前 提交于 2019-12-11 03:38:47
问题 GetLastError tells me I'm getting the "The program issued a command but the command length is incorrect." error when calling Process32First() (see code below). I found one post that looked helpful (http://social.msdn.microsoft.com/Forums/is/vcgeneral/thread/6f43716f-fdd3-4c92-bfba-6a23178c32bf), but I'm not sure if this is my problem. I've tried building a program that includes only "stdafx.h" , <iostream> , <Windows.h> and <TlHelp32.h> to test __alignof(PROCESSENTRY32) , but I still get a

Why short is stored as 4 bytes in a struct in C?

☆樱花仙子☆ 提交于 2019-12-11 00:25:09
问题 I have the following two structs: The problem is the sizeof(Content) returns 160. The struct consists of 11 shorts, 6 ints, 76 chars, 7 floats, 1 double, totally adding to 158 bytes. I have counted three times and there is still a 2 byte difference. typedef struct TIME_T { short year,mon,day; short hour,min,sec; } TIME; typedef struct { int no; char name[20]; char Code[10]; char DASType[10]; short wlen; float VLtd; int samp; int comp; int locationID; short TranMode; char TranIns[12]; short

C: Custom strlen() library function

假装没事ソ 提交于 2019-12-11 00:09:09
问题 I created my version of strlen() function. unsigned int my_strlen(char *p) { unsigned int i = 0; while(*p!='\0') { i++; p++; } return i; } It gives me correct output everytime i run.But my collegues are saying this code may cause problem on systems where length of character is greater than 1 byte . Is that so ?? So they modified the code as following: unsigned int my_strlen(char *p) { unsigned int i = 0; char *start = p; while(*p!='\0') { i++; p++; } return p - start; } I always thought in C

How can i find the size of a dynamically allocated array in C?

最后都变了- 提交于 2019-12-10 22:31:56
问题 I've made an array that is dynamically allocated by a cycle. And then a cycle that reads the numbers out of the array but i need to know the size of the array. The array is correct and fully working and has correct values in it. I defined the array like this: int *array; Now when i want to use this it wont work because im using a pointer: int size = sizeof(array)/sizeof(array[0]); How can i fix it so it works with my pointer? 回答1: I assume you are allocating the array using one of new or

A question about sizeof and class member function

核能气质少年 提交于 2019-12-10 21:39:07
问题 class B { public: int a; void fn(); } If I create an object of B, using B* pb = new B; Where is the memory of fn() locate? Is there a pointer in object that pointing at the memory loaction of fn()? If yes, why sizeof(B) returns the value as if there is no pointer in object at all? 回答1: Where is the memory of fn() locate? Since it's a normal function, somewhere in the code section of your program. This location is the same for all instances of the class. In fact, it has got nothing to do with

how compiler works to evaluate sizeof operator in c

岁酱吖の 提交于 2019-12-10 20:28:29
问题 though there are already several questions asked on this forum & others related to sizeof operator, i could not get any answer on how compiler evaluates the sizeof operator to find the size of any datatype, variable, pointer,array etc. if possible also point me to some links which can help me understand this in detail. any help will be greatly appreciated. thanks. 回答1: The compiler just knows the size of primitive datatypes; this knowledge is fundamentally built in to the compiler. For