sizeof

排序方法的算法实现

倖福魔咒の 提交于 2019-12-28 14:29:21
排序方法的算法实现 实验目的: 用顺序表存储方式存储实验数据,并实现以下相关算法: 1 、希尔排序 2 、快速排序 3 、堆排序 实验代码如下: # include <stdio.h> # include <iostream> # include <stdlib.h> using namespace std ; # define MAXSIZE 20 # define LEN(a) sizeof(a)/sizeof(a[0]) typedef int InfoType ; typedef int KeyType ; typedef struct { KeyType key ; InfoType otherinfo ; } RedType ; typedef struct { RedType r [ MAXSIZE + 1 ] ; int length ; } SqList ; //希尔排序 void ShellInsert ( SqList & L , int dk ) { /*L.r[0]用作暂存单元*/ int i , j ; for ( i = dk + 1 ; i <= L . length ; i ++ ) if ( L . r [ i ] . key < L . r [ i - dk ] . key ) { L . r [ 0 ] = L . r [ i ] ; for

how to determine sizeof class with virtual functions?

泪湿孤枕 提交于 2019-12-28 12:47:26
问题 this is kind of homework question. For the following code, #include <iostream> using namespace std; class A { public: virtual void f(){} }; class B { public: virtual void f2(){} }; class C: public A, public B { public: virtual void f3(){} }; class D: public C { public: virtual void f4(){} }; int main() { cout<<sizeof(D)<<endl; } The output is: 8 Could anyone please explain how it is 8 bytes? If the vtable implementation is compiler dependent, what should I answer for this kind of question in

Size of pid_t, uid_t, gid_t on Linux

僤鯓⒐⒋嵵緔 提交于 2019-12-28 03:45:30
问题 On Linux systems (either 32- or 64-bit), what is the size of pid_t , uid_t , and gid_t ? 回答1: #include <stdio.h> #include <sys/types.h> int main() { printf("pid_t: %zu\n", sizeof(pid_t)); printf("uid_t: %zu\n", sizeof(uid_t)); printf("gid_t: %zu\n", sizeof(gid_t)); } EDIT: Per popular request (and because, realistically, 99% of the people coming to this question are going to be running x86 or x86_64)... On an i686 and x86_64 (so, 32-bit and 64-bit) processor running Linux >= 3.0.0, the answer

Reliably determine the number of elements in an array

a 夏天 提交于 2019-12-28 02:55:06
问题 Every C programmer can determine the number of elements in an array with this well-known macro: #define NUM_ELEMS(a) (sizeof(a)/sizeof 0[a]) Here is a typical use case: int numbers[] = {2, 3, 5, 7, 11, 13, 17, 19}; printf("%lu\n", NUM_ELEMS(numbers)); // 8, as expected However, nothing prevents the programmer from accidentally passing a pointer instead of an array: int * pointer = numbers; printf("%lu\n", NUM_ELEMS(pointer)); On my system, this prints 2, because apparently, a pointer is twice

Access struct members as if they are a single array?

风流意气都作罢 提交于 2019-12-28 02:11:07
问题 I have two structures, with values that should compute a pondered average, like this simplified version: typedef struct { int v_move, v_read, v_suck, v_flush, v_nop, v_call; } values; typedef struct { int qtt_move, qtt_read, qtt_suck, qtd_flush, qtd_nop, qtt_call; } quantities; And then I use them to calculate: average = v_move*qtt_move + v_read*qtt_read + v_suck*qtt_suck + v_flush*qtd_flush + v_nop*qtd_nop + v_call*qtt_call; Every now and them I need to include another variable. Now, for

How sizeof(array) works at runtime?

孤者浪人 提交于 2019-12-27 17:38:45
问题 I have read that sizeof operator in C is interpreted at compile time and since at compile time compiler knows the array size and its type,sizeof is abled to compute the number of bytes occupied by array.But how is sizeof working for the following code : #include<stdio.h> #include<string.h> int main() { int n; scanf("%d",&n); int a[n]; int s=sizeof(a); printf("%d",s); return 0; } Here array size is not known at compile time,then how is it working properly ? 回答1: sizeof is always computed at

How sizeof(array) works at runtime?

纵饮孤独 提交于 2019-12-27 17:38:15
问题 I have read that sizeof operator in C is interpreted at compile time and since at compile time compiler knows the array size and its type,sizeof is abled to compute the number of bytes occupied by array.But how is sizeof working for the following code : #include<stdio.h> #include<string.h> int main() { int n; scanf("%d",&n); int a[n]; int s=sizeof(a); printf("%d",s); return 0; } Here array size is not known at compile time,then how is it working properly ? 回答1: sizeof is always computed at

3、linux下Socket编程-TCP/UDP

萝らか妹 提交于 2019-12-27 09:13:25
1、 什么是Socket   网络的 Socket数据传输是一种特殊的I/O,Socket也是一种文件描述符。Socket也具有一个类似于打开文件的函数调用Socket(),该函数返 回一个整型的Socket描述符,随后的连接建立、数据传输等操作都是通过该Socket实现的。常用的Socket类型有两种:流式Socket (SOCK_STREAM)和数据报式Socket(SOCK_DGRAM)。流式是一种面向连接的Socket,针对于面向连接的TCP服务应用;数据 报式Socket是一种无连接的Socket,对应于无连接的UDP服务应用。 2、 Socket建立   为了建立Socket,程序可以调用Socket函数,该函数返回一个类似于文件描述符的句柄。socket函数原型为:    int socket(int domain, int type, int protocol);     domain指明所使用的协议族,通常为PF_INET,表示互联网协议族(TCP/IP协议族);type参数指定socket的类型: SOCK_STREAM 或SOCK_DGRAM,Socket接口还定义了原始Socket(SOCK_RAW),允许程序使用低层协议;protocol通常赋值"0"。 Socket()调用返回一个整型socket描述符,你可以在后面的调用使用它。   

线性规划与网络流24题

拈花ヽ惹草 提交于 2019-12-27 03:32:02
诈个尸。 1. 飞行员配对方案问题 二分图匹配。 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <queue> 6 using namespace std; 7 const int INF = 1e9; 8 const int maxn = 2e5 + 10; 9 int lv[maxn], it[maxn]; 10 int cnt, h[maxn]; 11 12 struct edge 13 { 14 int to, pre, cap; 15 } e[maxn<<1]; 16 17 void init() 18 { 19 memset(h, -1, sizeof(h)); 20 cnt = 0; 21 } 22 23 void add(int from, int to, int cap) 24 { 25 e[cnt].pre = h[from]; 26 e[cnt].to = to; 27 e[cnt].cap = cap; 28 h[from] = cnt; 29 cnt++; 30 } 31 32 void ad(int from, int to, int cap) 33 { 34 add(from, to, cap); 35

【线性规划和网络流24题】

六月ゝ 毕业季﹏ 提交于 2019-12-27 03:26:08
(1)飞行员配对方案问题:二分图最大匹配。 思路:略。 View Code 1 #include<cstdio> 2 #include<cstring> 3 #define MAXN 1010 4 int cx[MAXN], cy[MAXN]; 5 int first[MAXN], next[MAXN], v[MAXN], e; 6 bool vis[MAXN]; 7 inline void addEdge(int x, int y) { 8 v[e] = y; 9 next[e] = first[x]; 10 first[x] = e++; 11 } 12 int path(int x) { 13 int i; 14 int y; 15 for (i = first[x]; i != -1; i = next[i]) { 16 y = v[i]; 17 if (!vis[y]) { 18 vis[y] = true; 19 if (cy[y] == -1 || path(cy[y])) { 20 cx[x] = y; 21 cy[y] = x; 22 return 1; 23 } 24 } 25 } 26 return 0; 27 } 28 int main() { 29 int n, m; 30 int i; 31 int x, y; 32 int ans; 33 while