sizeof

Weird behavior of sizeof for arrays passed as parameters [duplicate]

人盡茶涼 提交于 2019-12-08 07:12:32
问题 This question already has answers here : Using sizeof on arrays passed as parameters [duplicate] (4 answers) Closed 7 months ago . Can you explain this: void foo(const char data[10]) { char copy[10]; const char copy1[10] = {}; printf("%i", sizeof(copy)); //prints 10, as expected printf("%i", sizeof(copy1)); //prints 10, as expected printf("%i", sizeof(data)); //prints 4, WTF? } Looks like function parameters are treated as simple pointers for sizeof. But WHY does this happen? Is it documented

sizeof typedef pointer

瘦欲@ 提交于 2019-12-08 07:02:57
问题 I have a struct that defined like that: typedef struct my_struct { int numbers[10]; } *my_struct; Is there a way to find out its size? sizeof(my_struct);// return size of a pointer 回答1: The struct type itself is spelled with struct , so you can say: sizeof (struct my_struct) This would not work if you hadn't also given your struct a name, which would have been possible: typedef struct { int numbers[10]; } * foo; /* struct type has no name */ foo p = malloc(1000); p->numbers[3] = 81; I'd say

how and why sizeof(a)/sizeof(a[0]) in c is used to calculate the number of elements in an array

怎甘沉沦 提交于 2019-12-08 06:13:32
问题 I am a beginner to programming and i don't know the exact meaning of sizeof(a) and sizeof(a[0]) to calculate the no of elements in an array. Why and where is this function used ? And what is the purpose of dividing them. 回答1: According to the C Standard (6.5.3.4 The sizeof and alignof operators) 2 The sizeof operator yields the size (in bytes) of its operand , which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an

sizeof- function or macro? [duplicate]

我的未来我决定 提交于 2019-12-08 05:29:56
问题 This question already has answers here : Why is sizeof considered an operator? (10 answers) Closed 4 years ago . In c, we are using the sizeof() for getting the size of the datatypes. So how it is defined. It is a macro or a function. Because we can use that as two ways, sizeof int and sizeof(int) so how this is defined in header file. 回答1: It's neither. It's a built-in operator, whose value is computed at compile-time unless the argument is the name of a variable-length array (added in C99).

What is sizeof(2.5) equal to? [closed]

ぐ巨炮叔叔 提交于 2019-12-08 04:00:50
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . An examination question was: Q2: sizeof(2.5) is equal to ____. A.1 B.2 C.3 D.4 There are no such suffixes as f l F L , so floating-point constant (2.5) has a double type. Since nothing else is specified, I suppose its value is unspecified. For example, on some implementations

Why isn't sizeof for a struct equal to the sum of sizeof of each member?

我的未来我决定 提交于 2019-12-08 03:30:04
问题 Why does the sizeof operator return a size larger for a structure than the total sizes of the structure's members? 回答1: This is because of padding added to satisfy alignment constraints. Data structure alignment impacts both performance and correctness of programs: Mis-aligned access might be a hard error (often SIGBUS ). Mis-aligned access might be a soft error. Either corrected in hardware, for a modest performance-degradation. Or corrected by emulation in software, for a severe performance

sizeof typedef pointer

岁酱吖の 提交于 2019-12-08 03:13:42
I have a struct that defined like that: typedef struct my_struct { int numbers[10]; } *my_struct; Is there a way to find out its size? sizeof(my_struct);// return size of a pointer The struct type itself is spelled with struct , so you can say: sizeof (struct my_struct) This would not work if you hadn't also given your struct a name, which would have been possible: typedef struct { int numbers[10]; } * foo; /* struct type has no name */ foo p = malloc(1000); p->numbers[3] = 81; I'd say all of this is poor code that is needlessly terse for no reason. I would just keep all the names unique, and

libevent笔记4:Filter_bufferevent过滤器

大憨熊 提交于 2019-12-08 00:21:24
Filter_bufferevent是一种基于bufferevent的过滤器,其本身也是一个bufferevent。能够对底层bufferevent输入缓存区中的数据进行操作(加/解密等)后再读取,同样也能在一定的操作后再将数据写入底层bufferevent的输出缓存区。需要注意的是,在创建Filter_bufferevent后,底层bufferevent的读写回调函数就不会再生效了,而缓存区的回调函数依旧有效。 Filter_bufferevent相关函数 struct bufferevent bufferevent_filter_new (struct bufferevent underlying, bufferevent_filter_cb input_filter, bufferevent_filter_cb output_filter, int options, void( free_context)(void ), void *ctx):创建一个过滤器,参数列表如下: struct bufferevent *underlying:需要过滤的底层bufferevent; bufferevent_filter_cb input_filter/bufferevent_filter_cb output_filter:对底层bufferevent的输入

Assign result of sizeof() to ssize_t

守給你的承諾、 提交于 2019-12-07 16:39:26
问题 It happened to me that I needed to compare the result of sizeof(x) to a ssize_t . Of course GCC gave an error (lucky me (I used -Wall -Wextra -Werror )), and I decided to do a macro to have a signed version of sizeof() . #define ssizeof (ssize_t)sizeof And then I can use it like this: for (ssize_t i = 0; i < ssizeof(x); i++) The problem is, do I have any guarantees that SSIZE_MAX >= SIZE_MAX ? I imagine that sadly this is never going to be true. Or at least that sizeof(ssize_t) == sizeof(size

几个例子,看懂poll和epoll

筅森魡賤 提交于 2019-12-07 15:39:59
1. poll的例子 #include <stdio.h> #include <sys/eventfd.h> #include <poll.h> int main() { uint64_t value = 2; int event_fd = eventfd(0, EFD_NONBLOCK); struct pollfd pfd; int status = 0; uint64_t check_value = 0; printf("event_fd=%d\n", event_fd); write(event_fd, &value, sizeof(value)); write(event_fd, &value, sizeof(value)); write(event_fd, &value, sizeof(value)); read(event_fd, &check_value, sizeof(check_value)); printf("read, value=%ld\n", check_value); int ret = fork(); if (ret < 0) { printf("fork error\n"); } if (ret == 0) { pfd.fd = event_fd; pfd.events = POLLIN; printf("__FILE: %s, __LINE: