sizeof

What type should be used to loop through an array? [duplicate]

好久不见. 提交于 2019-12-13 10:14:47
问题 This question already has answers here : What is the correct type for array indexes in C? (9 answers) For iterating though an array should we be using size_t or ptrdiff_t? (3 answers) Closed 9 months ago . Let's have this array: char arr[SIZE_MAX]; And I want to loop through it (and one past its last element): char *x; for (i = 0; i <= sizeof(arr); i++) x = &arr[i]; (Edited to add some valid code with pointers). What is the most adequate type for i ? I accept non-standard types such as POSIX

why using sizeof in malloc?

大兔子大兔子 提交于 2019-12-13 09:54:44
问题 When executing this code on IDEONE: #include <stdio.h> #include <stdlib.h> struct A{ int x; char c; }; struct B{ int y; }; int main(void) { // your code goes here struct A* pa = malloc(sizeof(struct B)); printf("%d\n",sizeof(*pa)); pa = malloc(sizeof(int)); printf("%d\n",sizeof(*pa)); pa = malloc(sizeof(char)); printf("%d\n",sizeof(*pa)); pa = malloc(0); printf("%d\n",sizeof(*pa)); return 0; } I got: 8 8 8 8 I'm guessing that since pa is of type struct A * and struct A is 8 bytes long, then

Why Size is different for different pointers

落爺英雄遲暮 提交于 2019-12-13 09:23:57
问题 #include <stdio.h> #define R 10 #define C 20 int main() { int *p; int *p1[R]; int *p2[R][C]; printf("%d %d %d", sizeof(*p),sizeof(*p1),sizeof(*p2)); getchar(); return 0; } Why is the output: 4 8 160 ? Why does size of p1 becomes 8 and not 4? 回答1: Consider the types . sizeof(*p) ==> sizeof(int) sizeof(*p1) ==> sizeof(int *) sizeof(*p2) ==> sizeof((int [20])) Note: Depending on your platform and compiler, you'll get different results. Also, as we know, sizeof produces a result of type size_t ,

malloc of pointer to structure works why?

不羁岁月 提交于 2019-12-13 09:11:19
问题 In a code I accidentally used list* Head = malloc(sizeof(list*)); instead of the correct list* Head = malloc(sizeof(list)); to create a new list type node but it worked just fine later on. So my question is why did it work properly? 回答1: The idea here is, malloc() has no idea (type/size) or relation to the variable to which the return value is going to be assigned. It takes the input argument, allocates memory of the requested size and returns a pointer to the memory block, that's it. So, in

Strange case while using the sizeof() operator in C

安稳与你 提交于 2019-12-13 08:37:50
问题 I was testing the use of sizeof operator in C/C++ with this code: #include <ctype.h> /* Character types */ #include <stdio.h> /* Standard buffered input/output */ #include <stdlib.h> /* Standard library functions */ #include <string.h> /* String operations */ #include <sys/types.h> /* Data types */ #include <sys/wait.h> /* Declarations for waiting */ #include <unistd.h> void main() { char a[100]; char *des = malloc(100*sizeof(char)); strcpy(des,"abcded\0"); printf("%d\n",sizeof(des)); printf(

The real speed/benchmark of loop sizeof($x) vs loop ($x)?

荒凉一梦 提交于 2019-12-13 07:24:22
问题 I'm confused about the speed of the sizeof($x) vs $x when in a loop. This site: phpbench.com claims that the loop of sizeof($x) without pre calc -count() is THOUSANDS of percent slower than with pre calc count(). So I did a test as below, but I'm not sure if this is the right way to test it. The results show that the time is almost the same for each function. So I don't understand how it would be so much different from the phpbench website. In summary, I'd like to know a definite answer if

Does ARRAY_SIZE return undefined behaviour when the array is empty? [duplicate]

那年仲夏 提交于 2019-12-12 23:02:02
问题 This question already has answers here : What happens if I define a 0-size array in C/C++? (7 answers) Closed 2 years ago . Is ARRAY_SIZE return undefined behaviour when the array is empty? because we make a devide of unexisting sizeof((X)[0]) #include <stdio.h> #include <stdlib.h> #include <string.h> #ifndef ARRAY_SIZE #define ARRAY_SIZE(X) sizeof((X))/sizeof((X)[0]) #endif struct ka { int a; int b; }; int main(void) { struct ka k[] = {}; printf("%d\n", ARRAY_SIZE(k)); } 回答1: It is not

2019年12月12日

纵饮孤独 提交于 2019-12-12 22:59:17
一个由C/C++编译的程序占用的内存分为以下几个部分, 1、栈区(stack),由编译器自动分配释放 ,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。 2、堆区(heap),一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收 。注意它与数据结构中的堆是两回事,分配方式倒是类似于链表,呵呵。 3、全局区(静态区(static),全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域, 未初始化的全局变量和未初始化的静态变量在相邻的另 一块区域。 程序结束后由系统释放。 4、文字常量区 ,常量字符串就是放在这里的。 程序结束后由系统释放 5、程序代码区,存放函数体的二进制代码。 二、例子程序 这是一个前辈写的,非常详细 //main.cpp int a = 0; 全局初始化区 char *p1; 全局未初始化区 main() { int b; 栈 char s[] = “abc”; 栈 char *p2; 栈 char *p3 = “123456”; 123456\0在常量区,p3在栈上。 static int c =0; 全局(静态)初始化区 p1 = (char *)malloc(10); p2 = (char *)malloc(20); 分配得来得10和20字节的区域就在堆区。 strcpy(p1, “123456”);

C++ throwing compilation error on sizeof() comparison in preprocessor #if

筅森魡賤 提交于 2019-12-12 11:32:49
问题 I have this which does not compile with the error "fatal error C1017: invalid integer constant expression" from visual studio. How would I do this? template <class B> A *Create() { #if sizeof(B) > sizeof(A) #error sizeof(B) > sizeof(A)! #endif ... } 回答1: The preprocessor does not understand sizeof() (or data types, or identifiers, or templates, or class definitions, and it would need to understand all of those things to implement sizeof). What you're looking for is a static assertion

Can I use value_type on an instance of the vector, not on its type

北慕城南 提交于 2019-12-12 10:50:37
问题 while playing and trying to calculate total size of vector I tried something like vector<double> vd; auto area = vd.size()* sizeof (vd::value_type); //Ive seen Stepanov use area as name for this kind of size, idk if he adds the sizeof vd also to area :) Unfortunately this doesnt work... I need to use vector<double>::value_type but that makes code less readable. Can it be made to work? I dont like sizeof vd.front() because it just looks ugly to write front() for this. EDIT: decltype variants