static in C/C++

最近经常碰到static,之前也使用过,但都是一知半解,所以下决心做个整理总结,搞搞灵清它到底用哪些作用。

一.static in C

1.默认初始化为0:

  如果不显式地对静态变量进行初始化,它们将被初始化为0。

  static变量存放在Global/Static(全局区/静态区)。在静态数据区,内存中所有的字节默认值都是0x00,所以在程序一开始时static声明的变量会被默认初始化为0。

 

2.static声明的变量会一直保持其值(局部静态对象)

  static变量存放在Global/Static(全局区/静态区)。程序一开始,对其进行唯一一次初始化。被函数调用后,static变量不会消失,计算机会一直记录其值,其生命周期是贯穿函数调用及之后的时间。

 1 #include <stdio.h>
 2 void funcion();
 3 
 4 int main()
 5 {
 6     for(int count = 1; count <= 3; count++) {
 7         printf("第%d次调用:",count);
 8         funcion();
 9     }                    //for循环中的count内存被释放 
10     return 0;
11 } 
12 
13 void funcion() 
14 {
15     int temp = 10;
16     static int count = 10;//只在函数第一次调用时执行,后续函数调用此变量的初始值为上次调用后的值,每次调用后存储空间不释放
17     printf("temp = %d  static count = %d\n",temp--, count--);
18 }
Eg Code

static count每次调用,其值减1,而temp每次都重新开始都为10。而主函数for循环中的count是在栈区的,作用域只在主函数的for循环中,当循环结束是存储空间自动释放。

 

3.具有内部链接的静态变量(static variable with internal linkage)

  普通的外部变量可以被程序的任一文件所包含的函数使用,而具有内部链接的静态变量只可以被与它在同一个文件中的函数使用。

 1 //举个例子
 2 文件1:a.c
 3 int money = 10;
 4 static wife = 1;
 5 int main()
 6 {
 7     money++;
 8     printf("wife = %d\n",wife);
 9 }
10 void function()
11 {
12     money--;
13     printf("wife = %d\n",wife);
14 }
15 文件2:b.c
16 int main()
17 {
18     extern int money;
19     money--;
20     printf("money = %d\n",money);
21     return 0;
22 } 

a.c中main函数,function函数皆可调用money和wife。b.c用外部声明extern money,是可以输出money = 9的。但是不能extern int wife,因为在a.c 中wife 加了static,其只允许在a.c中使用。(就像好兄弟money是可以共享的,但是wife不行!)

  在多文件构成的程序中,如需共享一个外部变量,除了一个声明(定义声明)外,其他所有声明都要加extern;而如果不想被共享的变量,那就在声明时用static,表明该变量具有内部链接。

 

二.static in C++

1.静态数据成员

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 class Account
 6 {
 7 public:
 8     static int sumAccount;            //声明静态数据成员
 9     void calculate() {
10         amount += amount * interestRate;
11     }
12     static double rate() {
13         return interestRate;
14     }
15     static double rate2(double interestRate2 = interestRate) {//使用静态成员作为默认实参。
16         return interestRate2;
17     }
18 //    [Error] invalid use of non-static data member 'Account::amount'普通数据成员值不能作为默认实参
19 //    static double showAmount(double Amount = amount) {    
20 //        return Amount;
21 //    }
22     static void rate(double);
23 private:
24     string owner;
25     double amount;
26     static double interestRate;        //声明静态数据成员
27     static double initRate();
28 };
29 double Account::interestRate = 0.30;//定义并初始化静态数据成员
30 int Account::sumAccount = 10000;    //定义并初始化静态数据成员
31 
32 int main()
33 {
34     Account ac1;
35     Account *ac2 = &ac1;
36     Account &ac3 = ac1;
37     cout << ac1.sumAccount << endl;
38     cout << ac2->sumAccount << endl;
39     cout << ac3.sumAccount << endl;
40     cout << Account::sumAccount << endl;
41     return 0;
42 }
Eg Account Code

1.静态成员存在于任何对象之外,对象中不包含任何与静态数据成员有关的数据。静态数据成员只分配一次内存,即只存在一个static成员对象,被该类所有的对象共享。

  Eg:每个Account对象都包含数据成员owner和amount。只存在一个interestRate对象被所有Account对象共享。

2.静态数据成员存放在Global/Static(全局区/静态区)。不是由类的构造函数初始化的。必须在类的外部定义和初始化每个静态数据成员,且只能定义一次。一旦被定义,就将存在于程序的整个生命周期。在外部定义静态数据成员时,不能重复static关键字。

  Eg:静态数据成员的定义 double Account::interestRate = 0.30;

3.静态数据成员遵守public/protected/private访问规则。

4.静态数据成员的访问: (静态数据成员为public)

  ①作用域运算符直接访问静态数据成员          

    Eg: <类类型名>::<静态数据成员名>  

      Account::sumAccount;

  ②仍可以用类的对象,引用或者指针来访问静态数据成员。

    Eg: <类对象名>.<静态数据成员名> 

      ac1.sumAccount;

5. 静态数据成员与普通成员区别:

  ①静态数据成员可以是不完全类型。指针成员可以是不完全类型。数据成员必须是完全类型。

  ②可以使用静态成员作为默认实参。普通数据成员值本身属于对象的一部分。(Eg:代码中被注释段)

  ③由于被该类所有的对象共享,改变其值,对于所有对象都改变了,无需逐一改变。

6. 静态数据成员与全局变量的区别:

  ①静态数据成员在类内,作用域与全局变量不同。且实现了封装性。

  ②静态数据成员可以实现信息隐藏,可以是private。而全局变量不能。

 

2.静态成员函数

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 class Account
 6 {
 7 public:
 8     void calculate() {
 9         amount += amount * interestRate;//非静态成员函数可以访问静态数据成员
10     }
11     static double rate() {
12         return interestRate;    //静态成员之间可以相互访问
13     }
14     static void rate(double);
15 private:
16     string owner;
17     double amount;
18     static double interestRate;    
19     static double initRate();
20 };
21 double Account::interestRate = 0.30;
22 
23 void Account::rate(double newRate) {    //定义静态成员函数,不能重复static关键字
24     interestRate = newRate;
25 }
26 
27 int main()
28 {
29     Account ac1;
30     Account *ac2 = &ac1;
31     Account &ac3 = ac1;
32     cout << ac1.rate() << endl;
33     cout << ac2->rate() << endl;
34     cout << ac3.rate() << endl;
35     Account::rate(0.35);
36     cout << Account::rate() << endl;
37     return 0;
38 }
Eg2 Account Code
//class Account -->银行账户

1.静态成员函数不与任何对象绑定在一起,被所有Account对象共享。它们不包含this指针。静态成员函数不能声明称const的,且不能再static函数体内使用this指针。

2.静态成员函数,既可以定义在类内,也可以定义在类外部。(通常情况,类的静态成员应该在类的外部初始化。)在外部定义静态成员时,不能重复static关键字,只能出现在类的内部声明。

  Eg:Eg2 Account Code 行23-25

3.静态成员之间可以相互访问,包括静态成员函数访问静态数据成员和访问静态成员函数

4.成员函数不用通过作用域运算符就直接使用静态函数。

5.静态成员函数的访问: (静态成员函数为public)

  ①作用域运算符直接访问静态成员函数          

    Eg: <类类型名>::<静态成员函数名>  

      Account::rate();

      Account::rate(0.35);

  ②仍可以用类的对象,引用或者指针来访问静态成员函数。

    Eg: <类对象名>.<静态成员函数名> 

      ac1.rate();

转载于:https://www.cnblogs.com/kuotian/p/5299322.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是iperf3的C代码,可以在GitHub上找到完整的代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <signal.h> #include <errno.h> #include <netdb.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include "iperf.h" #include "iperf_api.h" #include "iperf_locale.h" #include "units.h" #include "net.h" #include "timer.h" #include "tcp_window_size.h" #include "cjson.h" #ifdef HAVE_SSL #include <openssl/ssl.h> #include <openssl/err.h> #endif /* Forwards. */ static int iperf_run(struct iperf_test *test); /**************************************************************************/ int main(int argc, char **argv) { int i; struct iperf_test *test; int result; struct sigaction sa; char client_version[10] = IPERF_VERSION; setlocale(LC_ALL, ""); bindtextdomain("iperf3", LOCALEDIR); textdomain("iperf3"); /* Initialize settings. */ test = iperf_new_test(); /* Register a few signal handlers to make clean exit more likely. */ memset(&sa, 0, sizeof(sa)); sa.sa_handler = &iperf_got_sigint; sigaction(SIGINT, &sa, NULL); sa.sa_handler = &iperf_got_sigpipe; sigaction(SIGPIPE, &sa, NULL); /* Parse command line options, handling some options immediately. */ i = iperf_parse_arguments(test, argc, argv); if (i < 0) { fprintf(stderr, "%s", iperf_strerror(i)); exit(1); } if (test->version) { printf("%s\n", client_version); exit(0); } else if (test->help) { iperf_printf(test, "\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n", "Usage: iperf3 [-options] [-s|-c host] [options]\n", "Client/Server:", " -V, --version show version information and quit.", " -h, --help show this message and quit.", " -i, --interval n seconds between periodic bandwidth reports.", " -f, --format [kmKM] format to report: Kbits, Mbits, KBytes, MBytes", " -P, --parallel n number of parallel client streams to run."); iperf_printf(test, "\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n", "Client specific:", " -c, --client <host> run in client mode, connecting to <host>.", " -u, --udp use UDP rather than TCP.", " -b, --bitrate #[KMG][/#] target bitrate in bits/sec (0 for unlimited)", " (default 1 Mbit/sec for UDP, unlimited for TCP)", " -t, --time n time in seconds to transmit for (default 10 secs)", " -n, --bytes n number of bytes to transmit (instead of -t)", " -k, --blockcount #[KMG] number of blocks (packets) to transmit (instead of -t or -n)", " -l, --length #[KMG] length of buffer to read or write", " (default 128 KB for TCP, dynamic or 8 KB for UDP)", " -R, --reverse reverse the direction of a test (client sends, server receives)."); iperf_printf(test, "\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n", "Server specific:", " -s, --server run in server mode.", " -D, --daemon run the server as a daemon", " -I, --pidfile file write PID file (default /var/run/iperf3.pid)", " -1, --one-off handle one client connection then exit.", " -B, --bind <host> bind to a specific interface, e.g. eth0"); iperf_printf(test, "\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n%s\n\n", "JSON options:", " -J, --json output in JSON format", " --logfile f send output to a log file", "", "For more information and tuning options, see iperf3's man page.", "", "Please report bugs to https://github.com/esnet/iperf", ""); exit(0); } /* Check for version number consistency. */ if (test->protocol->id == Ptcp && test->version == 3 && test->tcp.omit_version) { i = 2; } else { i = 3; } if (strncmp(client_version, test->version_string, i) != 0) { iperf_printf(test, "warning: version number mismatch (client %s, server %s)\n", client_version, test->version_string); } /* Initialize IP protocol */ if (test->cookiefile) { test->cookie = iperf_load_cookie(test->cookiefile); if (test->cookie == NULL) { iperf_errexit(test, "unable to load cookie from file '%s'", test->cookiefile); } } if (test->reverse) test->settings->reverse = 1; if (test->protocol->id == Pudp) { if (test->settings->mss || test->settings->socket_bufsize) { iperf_printf(test, "warning: MSS and socket buffer size settings are not used in UDP mode.\n"); } if (test->settings->no_delay) { iperf_printf(test, "warning: the TCP_NODELAY option is not used in UDP mode.\n"); } if (test->settings->pmtu != -1) { iperf_printf(test, "warning: the PMTU option is not used in UDP mode.\n"); } } #ifdef HAVE_SSL /* Initialize SSL library */ if (test->protocol->id == Ptcp && test->settings->ssl) { SSL_load_error_strings(); SSL_library_init(); test->sslctx = SSL_CTX_new(TLS_client_method()); if (test->sslctx == NULL) { iperf_errexit(test, "failed to create SSL context\n"); } if (test->settings->ssl_cafile) { if (SSL_CTX_load_verify_locations(test->sslctx, test->settings->ssl_cafile, NULL) != 1) { SSL_CTX_free(test->sslctx); iperf_errexit(test, "failed to load CA certificates from %s\n", test->settings->ssl_cafile); } } if (test->settings->ssl_cert) { if (SSL_CTX_use_certificate_chain_file(test->sslctx, test->settings->ssl_cert) != 1) { SSL_CTX_free(test->sslctx); iperf_errexit(test, "failed to load SSL certificate from %s\n", test->settings->ssl_cert); } } if (test->settings->ssl_key) { if (SSL_CTX_use_PrivateKey_file(test->sslctx, test->settings->ssl_key, SSL_FILETYPE_PEM) != 1) { SSL_CTX_free(test->sslctx); iperf_errexit(test, "failed to load SSL key from %s\n", test->settings->ssl_key); } } SSL_CTX_set_verify(test->sslctx, SSL_VERIFY_PEER, NULL); } #endif /* Daemon mode. */ if (test->daemon) { if (daemon(0, 0) != 0) { iperf_errexit(test, "error - failed to become a daemon: %s\n", strerror(errno)); } if (test->pidfile) { FILE *f; f = fopen(test->pidfile, "w"); if (f == NULL) { iperf_errexit(test, "error - unable to write PID file '%s': %s\n", test->pidfile, strerror(errno)); } fprintf(f, "%d\n", getpid()); fclose(f); } } /* Ignore SIGPIPE to simplify error handling */ sa.sa_handler = SIG_IGN; sigaction(SIGPIPE, &sa, NULL); /* If we are doing a single client (-1) and it is a daemon, don't do -D again */ if (test->daemon && test->num_ostreams == 1) test->daemon = 0; /* Defer daemon mode until after the above check for single client + daemon */ if (test->daemon) { if (daemon(0, 0) != 0) { iperf_errexit(test, "error - failed to become a daemon: %s\n", strerror(errno)); } if (test->pidfile) { FILE *f; f = fopen(test->pidfile, "w"); if (f == NULL) { iperf_errexit(test, "error - unable to write PID file '%s': %s\n", test->pidfile, strerror(errno)); } fprintf(f, "%d\n", getpid()); fclose(f); } } /* Ignore SIGPIPE to simplify error handling */ sa.sa_handler = SIG_IGN; sigaction(SIGPIPE, &sa, NULL); /* Set up the output stream */ if (test->json_output) { test->outfile = json_get_output_stream(test->json_output_file); } else if (test->logfile) { test->outfile = fopen(test->logfile, "w"); if (test->outfile == NULL) { iperf_errexit(test, "error - unable to write log to file '%s': %s\n", test->logfile, strerror(errno)); } } else { test->outfile = stdout; } /* Start the client or server */ if (test->server) { if (test->daemon) { iperf_printf(test, "Server listening on port %d\n", test->server_port); } else { iperf_printf(test, "-----------------------------------------------------------\n"); iperf_printf(test, "Server listening on %s port %d\n", test->settings->domain == AF_INET6 ? "[::]" : "0.0.0.0", test->server_port); iperf_printf(test, "-----------------------------------------------------------\n"); } result = iperf_run_server(test); } else { if (test->daemon) { iperf_printf(test, "Client connecting to %s, TCP port %d\n", test->server_hostname, test->server_port); } else { if (test->reverse) iperf_printf(test, "-----------------------------------------------------------\n"); iperf_printf(test, "Client connecting to %s, %s port %d\n", test->server_hostname, test->protocol->name, test->server_port); if (test->reverse) iperf_printf(test, "-----------------------------------------------------------\n"); } result = iperf_run_client(test); } if (test->cookie) iperf_delete_cookie(test->cookie); #ifdef HAVE_SSL if (test->protocol->id == Ptcp && test->settings->ssl) { SSL_CTX_free(test->sslctx); } ERR_free_strings(); #endif iperf_free_test(test); return result; } static int iperf_run(struct iperf_test *test) { test->start_time = milliseconds(); test->next_time = test->start_time; test->bytes_sent = 0; test->blocks_sent = 0; test->retransmits = 0; if (test->server_hostname) { test->server_hostname_len = strlen(test->server_hostname); } if (test->bind_address) { test->bind_address_len = strlen(test->bind_address); } if (test->json_output) { cJSON *json_output = cJSON_CreateObject(); if (json_output == NULL) { iperf_errexit(test, "error - cJSON_CreateObject failed: %s\n", strerror(errno)); } cJSON_AddItemToObject(json_output, "start", cJSON_CreateNumber((double) test->start_time / 1000)); if (test->verbose) { cJSON_AddItemToObject(json_output, "verbose", cJSON_CreateNumber(1)); } cJSON_AddItemToObject(json_output, "system_info", cJSON_CreateObject()); iperf_json_printf(json_output, "version", "%s", test->version); iperf_json_printf(json_output, "system_info", "%s", get_system_info()); if (test->title) { iperf_json_printf(json_output, "title", "%s", test->title); } if (test->extra_data) { cJSON_AddItemToObject(json_output, "extra_data", cJSON_Parse(test->extra_data)); } test->json_start_time = milliseconds(); test->json_output_string = cJSON_Print(json_output); cJSON_Delete(json_output); if (test->json_output_string == NULL) { iperf_errexit(test, "error - cJSON_Print failed: %s\n", strerror(errno)); } } if (test->protocol->id == Ptcp) { if (test->settings->socket_bufsize) { if (iperf_set_tcp_windowsize(test) != 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } } if (test->reverse) { if (test->protocol->id == Ptcp) { if (iperf_create_streams(test, test->reverse) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } if (iperf_connect(test, test->reverse) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } else { if (iperf_create_streams(test, test->reverse) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } if (iperf_connect(test, test->reverse) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } if (test->json_output) { cJSON *json_output = cJSON_CreateObject(); if (json_output == NULL) { iperf_errexit(test, "error - cJSON_CreateObject failed: %s\n", strerror(errno)); } cJSON_AddItemToObject(json_output, "start", cJSON_CreateNumber((double) test->start_time / 1000)); if (test->verbose) { cJSON_AddItemToObject(json_output, "verbose", cJSON_CreateNumber(1)); } iperf_json_printf(json_output, "interval", "%d", test->settings->stats_interval); if (test->title) { iperf_json_printf(json_output, "title", "%s", test->title); } if (test->extra_data) { cJSON_AddItemToObject(json_output, "extra_data", cJSON_Parse(test->extra_data)); } iperf_json_printf(json_output, "start_connected", "%d", test->connected); cJSON_AddItemToObject(json_output, "intervals", cJSON_CreateArray()); test->json_output_string = cJSON_Print(json_output); cJSON_Delete(json_output); if (test->json_output_string == NULL) { iperf_errexit(test, "error - cJSON_Print failed: %s\n", strerror(errno)); } } if (test->protocol->id == Pudp) { if (iperf_udp_connect(test) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } if (test->reverse) { if (test->protocol->id == Ptcp) { if (iperf_listen(test) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } if (test->protocol->id == Pudp || test->protocol->id == Psctp) { if (iperf_run_server_udp(test) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } else { if (iperf_run_server_tcp(test) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } } else { if (test->protocol->id == Ptcp) { if (iperf_run_client_tcp(test) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } else { if (iperf_run_client_udp(test) < 0) { iperf_errexit(test, "error - %s\n", strerror(errno)); } } } if (test->json_output) { cJSON *json_output = cJSON_CreateObject(); if (json_output == NULL) { iperf_errexit(test, "error - cJSON_CreateObject failed: %s\n", strerror(errno)); } cJSON_AddItemToObject(json_output, "start", cJSON_CreateNumber((double) test->start_time / 1000)); if (test->verbose) { cJSON_AddItemToObject(json_output, "verbose", cJSON_CreateNumber(1)); } iperf_json_printf(json_output, "interval", "%d", test->settings->stats_interval); if (test->title) { iperf_json_printf(json_output, "title", "%s", test->title); } if (test
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值