sizeof

Sizes of arrays declared with pointers

依然范特西╮ 提交于 2019-12-02 03:28:10
char c[] = "Hello"; char *p = "Hello"; printf("%i", sizeof(c)); \\Prints 6 printf("%i", sizeof(p)); \\Prints 4 My question is: Why do these print different results? Doesn't c[] also declare a pointer that points to the first character of the array (and therefore should have size 4, since it's a pointer)? It sounds like you're confused between pointers and arrays. Pointers and arrays (in this case char * and char [] ) are not the same thing. An array char a[SIZE] says that the value at the location of a is an array of length SIZE A pointer char *a; says that the value at the location of a is a

Why function does not know the array size?

。_饼干妹妹 提交于 2019-12-02 03:03:56
If I write int main() { int a[100] = {1,2,3,4,}; cout<<sizeof(a)/sizeof(a[0])<<endl; //a is a pointer to the first elem of array, //isn't it return 0; } I get 400! If I write void func(int *a); int main() { int a[100] = {1,2,3,4,}; func(a); return 0; } void func(int *a) { cout<<sizeof(a)/sizeof(a[0])<<endl; //a is a pointer to the first elem of array } Then I get 1! So why function does not know the array size? Arrays decay to pointers when passed to functions, so all you will get is the size of the pointer. sizeof returns the size of the type. In the second example, func( int *a ) , a is a

[HEOI2016] 字符串 - 后缀数组,主席树,ST表,二分

时光总嘲笑我的痴心妄想 提交于 2019-12-02 02:22:48
[HEOI2016] 字符串 Description 给定一个字符串 \(S\) , 有 \(m\) 个询问,每个询问给定参数 \((a,b,c,d)\) ,求 \(s[a..b]\) 的子串与 \(s[c..d]\) 的最长公共前缀长度的最大值。 Solution 读懂题意以后就很简单。把后缀数组和高度数组都搞出来,并对高度数组建立 ST 表,然后对于每个询问找到 \(s[c..d]\) 在后缀排序中的位置,二分一个 \(LCP\) 长度,检验只需要查询某一段下标区间内有没有 \(rank\) 在某个区间内的值,对 \(rank\) 数组建一个主席树即可。 我太菜了,写了一个多小时,瞎优化主席树才卡过了常。正思索着也没哪里常数太大,后来发现原来是 \(log\) 的锅…… Code #include <bits/stdc++.h> using namespace std; const int N = 100005; int fastlog[N]; namespace st { int a[N][21]; void build(int *src,int n) { for(int i=1; i<=n; i++) a[i][0]=src[i]; for(int i=1; i<=20; i++) for(int j=1; j<=n-(1<<i)+1; j++) a[j][i]=min(a

Extra bytes when declaring a member of a struct as uint32_t

半城伤御伤魂 提交于 2019-12-02 01:58:08
I have a problem when using the uint32_t type from the stdint.h library. If I run the following code (on Ubuntu linux 11.10 x86_64, g++ version 4.6.1): #include "stdint.h" #include <iostream> using std::cout; typedef struct{ // api identifier uint8_t api_id; uint8_t frame_id; uint32_t dest_addr_64_h; uint32_t dest_addr_64_l; uint16_t dest_addr_16; uint8_t broadcast_radius; uint8_t options; // packet fragmentation uint16_t order_index; uint16_t total_packets; uint8_t rf_data[]; } xbee_tx_a; typedef struct{ // api identifier uint8_t api_id; uint8_t frame_id; uint16_t dest_addr_64_h; uint16_t

基于Windows的服务器和客户端

强颜欢笑 提交于 2019-12-02 00:26:05
server.c 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <winsock2.h> 4 void ErrorHandling(char* message); 5 6 int main(int argc, char* argv[]) 7 { 8 WSADATA wsaData; 9 SOCKET hServSock, hClntSock; 10 SOCKADDR_IN servAddr, clntAddr; 11 12 int szClntAddr; 13 char message[] = "Hello World!"; 14 if (argc != 2) 15 { 16 printf("Usage : %s ,port>\n", argv[0]); 17 exit(1); 18 } 19 20 if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) 21 ErrorHandling("WSAStartup() error!"); 22 23 hServSock = socket(PF_INET, SOCK_STREAM, 0); 24 if (hServSock == INVALID_SOCKET) 25 ErrorHandling("socket() error"); 26

王道数据结构——线性表中链表的实现与应用

♀尐吖头ヾ 提交于 2019-12-02 00:25:13
1、单链表 1 #include <cstdio> 2 #include <cstdlib> 3 typedef struct LNode{ 4 int data; 5 struct LNode *next; 6 }LNode, *LinkList;//定义结构体和结构体指针 7 8 /*按值查找结点*/ 9 int LocateElem(LinkList &L, int x){ 10 LNode *p = L -> next; 11 int cnt = 1; 12 while(p){ 13 if(p -> data == x) 14 break; 15 cnt++; 16 p = p -> next; 17 } 18 return cnt; 19 } 20 /*按序号查找结点值,序号从0开始*/ 21 LNode *GetElem(LinkList &L, int i){ 22 LNode *p = L -> next; 23 if(i == 0) return L; 24 if(i < 1) return NULL; 25 26 int cnt = 1; 27 while(p != NULL){ 28 if(cnt == i) 29 break; 30 cnt++;//千万要记得不等时,计数要增加1啊 31 p = p->next; 32 } 33 return p; 34 }

Vtable内存布局分析

浪子不回头ぞ 提交于 2019-12-01 23:06:59
vtale 内存布局分析 虚函数表指针与虚函数表布局 考虑如下的 class: class A { public: int a; virtual void f1() {} virtual void f2() {} }; int main() { A *a1 = new A(); return 0; } 首先明确,sizeof(A)的输出是 16,因为:class A 中含有一个 int 是 4 字节,然后含有虚函数,所以必须含有一个指向 vtable 的 vptr,而 vptr 是 8 字节,8 + 4 = 12,对齐到 8 的边界,也就是 16 上述 class 的 AST record layout 如下: *** Dumping AST Record Layout 0 | class A 0 | (A vtable pointer) 8 | int a | [sizeof=16, dsize=12, align=8, | nvsize=12, nvalign=8] 可以证明对齐边界为 8 字节 需要注意的是:由于含有指针,而 64 位系统,指针为 8 字节,所以对齐边界是 8 虚函数表指针 vptr 为了完成多态的功能,现代的 C++编译器都采用了表格驱动的对象模型,具体来说,所有虚函数的地址都存放在一个表格之中,而这个表格就被称为 虚函数表vtable

Array length problem

北慕城南 提交于 2019-12-01 22:34:47
I was reading a csc placement paper where I read a question related to c language array's sizeof() operator. Answer was something else then i expected it to be. int DIMension(int array[]) { return sizeof(array )/ sizeof(int); } main() { int arr[10]; printf(“Array dimension is %d”, DIMension(arr)); } This program in c language prints 1 as the answer. Why is that happening? Because int array[] is just a pointer and it's size is same as of int . I think you expected that size of arr will somehow be passed to function, but it doesn't work that way. Size of arr can be determined only in same scope

php数组长度怎么获取

旧街凉风 提交于 2019-12-01 22:21:46
我们可以将元素添加到数组或从数组中删除元素,那么如果我们想要知道数组中存在的元素的总长度或总数,我们就可以使用count() 或sizeof函数。 下面我们就通过简单的示例,给大家介绍php获取数组长度的方法。 方法一:count()函数 首先我们创建了一个数组,如下: 1 $array = array ( "ABC" , "DEF" , "GHI" , "KLM" ); 然后我们使用count函数来打印出此数组中的元素数量。 1 print count ( $array ); 输出结果: 如图输出4,因为上述数组中有四个元素。 注 :count() 函数计算数组中的单元数目或对象中的属性个数。 大理石平台 方法二:sizeof()函数 我们还可以使用sizeof函数来确定值的数量。 代码示例: 1 2 $value = array (2,5,6,8,9); echo "数组值的数量是" .sizeof( $value ). "<br>" ; 来源: https://www.cnblogs.com/furuihua/p/11719004.html

[c++11]结构体字节对齐

核能气质少年 提交于 2019-12-01 22:16:06
//测试c++中结构体的字节对齐 #include <iostream> #include <cmath> using namespace std; void vsVersion(); //example 1 struct Node1 { char a; int b; double c; }; //example 2 struct Node2 { char a; int b; char c; }; //example 3 struct Node3 { }; //example 4 struct Node4 { char a; int b; static double c; }; //example 5 struct Inside { int a; double b; float c; }; struct Node5 { char e[2]; int f; short h; struct Inside inside; }; //example 6 struct Node6 { char a; int b; float c; double d; int* p; char* pc; short e; }; int main() { Node1 n1; Node2 n2; Node3 n3; Node4 n4; Node5 n5; Node6 n6; vsVersion(); //宏定义