sizeof

How does sizeof work for different data types when added and calculated? [duplicate]

北慕城南 提交于 2019-12-03 20:42:27
This question already has answers here : Closed 5 years ago . What happens here? sizeof(short_int_variable + char_variable) (5 answers) #include <stdio.h> int main() { short int i = 20; char c = 97; printf("%d, %d, %d\n", sizeof(i), sizeof(c), sizeof(c + i)); return 0; } The output of this code is 2, 1, 4 According to me it should be 2, 1, 2 because char + short int is short int and size of short int is 2 . According to C standard integral promotion rules , the type of the expression c + i will be int , that's why you're getting the equivalent of sizeof (int) , i.e. 4 . When different

c++数据结构随笔(1)

邮差的信 提交于 2019-12-03 20:38:55
创建数据结构单链表的时候,突然碰到基础不牢固的问题,指针问题。就当复习指针了。 问题是如果三个指针a和b和r, b=a ; b->next = r; 那么a->next会不会变? 然后写了一个小的cpp验证了一下。 struct node{ int data; node *next; }; int main() { node *p = (node*)malloc(sizeof(node)); node *q = p; node *r = (node*)malloc(sizeof(node)); p->next = NULL; cout<<q->next<<endl; p->next = r; cout<<q->next<<endl; return 0; } 结果是不一样的,说明q->next会随着p->next的指向变化而变化。 印证了我最开始的想法。 然后又接着想, b=a ; a->next = r; 那么b->next会不会变? 答案也是会变的,所以如果把b和a指向同一个区域(即b=a),那么可以通过改变a->next来改变b->next。 根据最初的假设接着继续想, b=a; a=r; b的指向会不会变? int main() { node *p = (node*)malloc(sizeof(node)); node *q = p; node *r = (node*

指针专题2-数组指针

白昼怎懂夜的黑 提交于 2019-12-03 20:34:56
1 以指针的方式遍历数组元素 int arr[]={11, 22, 33, 44, 55 }; int len = sizeof(arr)/sizeof(int); //求数组长度 char i; for(i=0; i<len; i++) { printf("%d ", *(arr+i)); //*(arr+i)等价于arr[i] }   arr是int*类型,每次加1时,arr自身的值(地址)会增加sizeof(int)。 2 以数组指针的方式遍历数组元素 int arr[]={11. 22, 33, 44, 55 }; int len = sizeof(arr)/sizeof(int), *p=&arr; //等价于*p=&arr[0] char i; for(i=0; i<len; i++) { printf("%d ", *(p+i)); //*(p+i)等价于*(arr+i) }   p、arr、&arr[0] 等价   *(p+i)、*(arr+i)、arr[i]、p[i ]等价 3 借助自增运算符遍历数组元素   不管是数组名还是数组指针,都可以用上面的两种方式来访问数组元素,不同的是:数组名是常量,他的值不能被改变;数组指针是变量(除非特别指明它是常量),它的值可以任意改变。也就是说,数组名只能指向数组的开头,而数组指针可以先指向数组开头,再指向其他元素。 int

About sizeof of a class member function pointer [duplicate]

柔情痞子 提交于 2019-12-03 17:15:28
问题 This question already has answers here : Pointers to members representations (2 answers) Closed 6 years ago . Let's say we have a class A class A; and these typedefs typedef void (A::*a_func_ptr)(void); typedef void (*func_ptr)(void); My question is why sizeof(a_func_ptr) returns 16, while sizeof(func_ptr) returns 4 (as for any pointer on x86 system)? For instance int main(int argc, char *argv[]) { int a = sizeof(a_func_ptr); int b = sizeof(func_ptr); } 回答1: My question is why sizeof(a_func

Objective-C Runtime: What to put for size & alignment for class_addIvar?

余生长醉 提交于 2019-12-03 16:44:35
The Objective-C Runtime provides the class_addIvar C function : BOOL class_addIvar(Class cls, const char *name, size_t size, uint8_t alignment, const char *types) What do I put for size and alignment ? I'm adding an instance variable of type UITextPosition * , but no UITextPosition object is in scope. For size , can I just do sizeof(self) , where self is a subclass of UITextField ? I.e., can I assume that a UITextPosition object is the same size as a UITextField object? How do I get alignment ? The documentation on this stuff is not very informative, which does reflect that generally you

find sizeof char array C++

纵饮孤独 提交于 2019-12-03 15:36:17
im trying to get the sizeof char array variable in a different function where it was initialize however cant get the right sizeof. please see code below int foo(uint8 *buffer){ cout <<"sizeof: "<< sizeof(buffer) <<endl; } int main() { uint8 txbuffer[13]={0}; uint8 uibuffer[4] = "abc"; uint8 rxbuffer[4] = "def"; uint8 l[2]="g"; int index = 1; foo(txbuffer); cout <<"sizeof after foo(): " <<sizeof(txbuffer) <<endl; return 0; } the output is: sizeof: 4 sizeof after foo(): 13 desired output is: sizeof: 13 sizeof after foo(): 13 This can't be done with pointers alone. Pointers contain no information

difference between sizeof('a') and sizeof(“a”)

家住魔仙堡 提交于 2019-12-03 15:16:49
My question is about the sizeof operator in C. sizeof('a'); equals 4 , as it will take 'a' as an integer: 97. sizeof("a"); equals 2 : why? Also (int)("a") will give some garbage value. Why? 'a' is a character constant - of type int in standard C - and represents a single character . "a" is a different sort of thing: it's a string literal , and is actually made up of two characters: a and a terminating null character. A string literal is an array of char , with enough space to hold each character in the string and the terminating null character. Because sizeof(char) is 1 , and because a string

IPv4,IPv6套接字地址结构

喜欢而已 提交于 2019-12-03 15:00:22
1.IPv4套接字地址结构 struct in_addr{   in_addr_t s_addr;//unsigned int }; struct sockaddr_in{   //uint8_t sin_len 这个字段可能在其他系统上有,我的系统是ubuntu 19.04 下面介绍的地址结构类似   sa_familiy_t sin_family;//unsigned short   in_port_t sin_port;//unsigned short   struct in_addr sin_addr;   unsigned char sin_zero[sizeof(struct sockaddr) - \ sizeof(sa_familiy_t) - sizeof(in_port_t) - sizeof(struct in_addr)]; }; sin_family指明了属于哪个协议族。sin_port指明了端口号(0-65535),sin_addr.s_addr指明了网络二进制字节序值。sin_zero可以从上面看出就是用了填充字节的,使得该结构和sockaddr通用套接字地址结构的大小相同。 2.通用套接字地址结构是什么? 通用套接字地址结构主要是为了方便处理不同协议族的套接字地址结构,即在需要传递套接字地址结构时通常采用struct sockaddr *sa作为形参

c++ function template specialization for known size typedefed array

和自甴很熟 提交于 2019-12-03 13:58:38
问题 Please consider the following code: #include <iostream> #include <typeinfo> template< typename Type > void func( Type var ) { std::cout << __FUNCTION__ << ": var = " << var << " [" << typeid( var ).name( ) << "]." << std::endl; std::cout << "-> var is SCALAR. Size = " << sizeof( Type ) << std::endl; } #if 1 template< typename Type > void func( Type * var ) { std::cout << __FUNCTION__ << ": var = " << var << " [" << typeid( var ).name( ) << "]." << std::endl; std::cout << "-> var is ARRAY.

使用单链表来实现集合的交并差运算 数据结构

被刻印的时光 ゝ 提交于 2019-12-03 13:16:54
使用单链表来实现集合的交并差运算 数据结构 问题描述 该算法的设计,要求运行结果如下所示: 集合的运算如下: 原 集 合A: c a e h 原 集 合B: f h b g d a 有序集合A: a c e h 有序集合B: a b d f g h 集合的并C: a b c d e f g h 集合的交C: a h 集合的差C: c e 代码实现 首先这里交并差的实现是基于链表中的元素已经从小到大排完序的链表。这里排序使用的是冒泡排序。 //链表的冒泡排序算法 bool BubbleSort(LinkList &L) //链表的冒泡排序 { LinkList p, q, tail; tail=NULL; while((L->next->next)!=tail) { p=L; q=L->next; while(q->next!=tail) { if((q->data) > (q->next->data)) { p->next=q->next; q->next=q->next->next; p->next->next=q; q=p->next; } q=q->next; p=p->next; } tail=q; } return true; } //集合的并 bool merge(LinkList &L1, LinkList &L2, LinkList &L3)//集合的并