sizeof

How can I get the size of an array from a pointer in C?

匿名 (未验证) 提交于 2019-12-03 02:10:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I've allocated an "array" of mystruct of size n like this: if ( NULL == ( p = calloc ( sizeof ( struct mystruct ) * n , 1 ))) { /* handle error */ } Later on, I only have access to p , and no longer have n . Is there a way to determine the length of the array given just the pointer p ? I figure it must be possible, since free(p) does just that. I know malloc() keeps track of how much memory it has allocated, and that's why it knows the length; perhaps there is a way to query for this information? Something like... int length =

void main() { if(sizeof(int) > -1) printf(“true”); else printf(“false”); ; [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This question already has an answer here: Why does this if condition fail for comparison of negative and positive integers 6 answers void main() { if(sizeof(int) > -1) printf("true"); else printf("false"); } I expected the output to be true but it is false. Can anybody please explain me the reason for the output. 回答1: sizeof(int) is of type size_t , which is an unsigned integer type. So in the expression if(sizeof(int) > -1) , -1 is converted to an unsigned integer, which is very big. BTW, use int main instead of the non-standard void main .

why does malloc(sizeof(pointer)) work?

匿名 (未验证) 提交于 2019-12-03 02:00:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This following code works fine: #include #include int main() { struct node{ int a, b, c, d, e; }; struct node *ptr = NULL; printf("Size of pointer ptr is %lu bytes\n",sizeof (ptr)); printf("Size of struct node is %lu bytes\n",sizeof (struct node)); ptr = (struct node*)malloc(sizeof (ptr)); //Line 1 // ptr = (struct node*)malloc(sizeof (struct node)); //Line 2 ptr->a = 1; ptr->b = 2; ptr->c = 3; ptr->d = 4; ptr->e = 5; printf("a: %d, b: %d, c: %d, d: %d, e: %d\n", ptr->a,ptr->b,ptr->c,ptr->d,ptr->e); return 0; } When complied as: gcc -Wall

Check Windows version

匿名 (未验证) 提交于 2019-12-03 02:00:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How I can check in C++ if Windows version installed on computer is Windows Vista and higher (Windows 7)? 回答1: Similar to other tests for checking the version of Windows NT: OSVERSIONINFO vi; memset (&vi, 0, sizeof vi); vi .dwOSVersionInfoSize = sizeof vi; GetVersionEx (&vi); if (vi.dwPlatformId == VER_PLATFORM_WIN32_NT && vi.dwMajorVersion >= 6) 回答2: All the answers in this thread point you to using GetVersion or GetVersionEx for this test, which is incorrect . It seems to work, but it is risky. The primary source of appcompat problems for

How to copy char *str to char c[] in C?

匿名 (未验证) 提交于 2019-12-03 01:58:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Trying to copy a char *str to char c[] but getting segmentation fault or invalid initializer error. Why is this code is giving me a seg fault ? char *token = "some random string"; char c[80]; strcpy( c, token); strncpy(c, token, sizeof c - 1); c[79] = '\0'; char *broken = strtok(c, "#"); 回答1: use strncpy() rather than strcpy() /* code not tested */ #include int main(void) { char *src = "gkjsdh fkdshfkjsdhfksdjghf ewi7tr weigrfdhf gsdjfsd jfgsdjf gsdjfgwe"; char dst[10]; /* not enough for all of src */ strcpy(dst, src); /* BANG!!! */ strncpy

Allocating memory for a Structure in C

匿名 (未验证) 提交于 2019-12-03 01:58:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm tasked to create a program which dynamically allocates memory for a structure. normally we would use x=malloc(sizeof(int)*y); However, what do I use for a structure variable? I don't think its possible to do struct st x = malloc(sizeof(struct)); Could someone help me out? Thanks! 回答1: My favorite: #include struct st *x = malloc(sizeof *x); Note that: x must be a pointer no cast is required include appropriate header 回答2: You're not quite doing that right. struct st x is a structure, not a pointer. It's fine if you want to allocate one on

Is there anything wrong with sizeof(array)/sizeof(array[0])?

匿名 (未验证) 提交于 2019-12-03 01:58:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: One of my colleagues has recently said that the above statement is not type safe and I should use something else as you need as much type safe structures as possible to reduce the amount of possible bugs. Whilst I agree on being type safe, I'm a little confused as this is the type of code in question (only the contents and length of data[] is modified) unsigned char data[] = {1,2,3,4,5}; int data_len = sizeof(data) / sizeof(data[0]); Where is the part that is not type safe? Needless to say, other than the comment, the colleague will not

When extending a padded struct, why can't extra fields be placed in the tail padding?

匿名 (未验证) 提交于 2019-12-03 01:57:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Let's consider the structs : struct S1 { int a ; char b ; }; struct S2 { struct S1 s ; /* struct needed to make this compile as C without typedef */ char c ; }; // For the C++ fans struct S3 : S1 { char c ; }; The size of S1 is 8, which is expected due to alignment. But the size of S2 and S3 is 12. Which means the compiler structure them as : | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | | a | b | padding | c | padding | The compiler could place c in the padding in 6 7 8 without breaking alignment constraints. What is the

Floating Point to Binary Value(C++)

匿名 (未验证) 提交于 2019-12-03 01:57:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I want to take a floating point number in C++, like 2.25125, and a int array filled with the binary value that is used to store the float in memory (IEEE 754). So I could take a number, and end up with a int num[16] array with the binary value of the float: num[0] would be 1 num[1] would be 1 num[2] would be 0 num[3] would be 1 and so on... Putting an int into an array isn't difficult, just the process of getting the binary value of a float is where I'm stuck. Can you just read the binary in the memory that the float variable? If not, how

Struct timeval to printable format

匿名 (未验证) 提交于 2019-12-03 01:57:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Could you please help me how to format a struct timeval instance to human readable format like "2010-01-01 15:35:10.0001"? 回答1: Convert the tv_sec using localtime , and strftime , then append tv_usec part. 回答2: You need to manually append the microseconds part, since it's not in the struct tm that strftime () deals with. Here's a snippet: struct timeval tv; time_t nowtime; struct tm *nowtm; char tmbuf[64], buf[64]; gettimeofday(&tv, NULL); nowtime = tv.tv_sec; nowtm = localtime(&nowtime); strftime(tmbuf, sizeof tmbuf, "%Y-%m-%d %H:%M:%S",