Gangmax Blog

自由之思想,独立之精神

C++ Code Snippets

| Comments

From here.

test.cpp
1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
    using namespace std;
    cout << "Hello, world!" << endl;
    return 0;
}
sys.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
     system("echo -n '\nHostname: '"); system("hostname");
     system("echo -n '\nIP Address: '"); system("ip a");
     system("echo -n '\nDate/Time: '"); system("date");
     system("echo '\nFilesystem Utilization:'"); system("df -h; echo");
     return 0;
}
fork.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
        pid_t PID,CPID;
        PID = getpid();
        CPID = fork();
        if(CPID<0)
        {       cout << "Error starting child process\n";
                return 1;
        }
        else
        {
                cout << "The PID of the current process: " << PID << endl;
                cout << "The PID of the new child process: " << CPID << endl;
        }
        return 0;
}
pid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
        pid_t PID,CPID;
        PID = getpid();
        CPID = fork();
        if(CPID<0)
        {       cout << "Error starting child process\n";
                return 1;
        }
        else
        {
                cout << "The PID of the current process: " << PID << endl;
                cout << "The PID of the new child process: " << CPID << endl;
        }
        return 0;
}
pointer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <unistd.h>
using namespace std;
int main()
{
        int myAge = 40;

        int* agePtr = &myAge;

        cout << "Address of pointer is pointing " << agePtr << endl;

        cout << "Address of pointer " << &agePtr << endl;
        cout << "Data at memory address " << *agePtr << endl;


        int** agePtrPtr = &agePtr;

        cout << "agePtr " << *agePtrPtr << endl;

        cout << "Data " << **agePtrPtr << endl;

  int* nullPtr = 0;

  cout << "Data " << *nullPtr << endl;

        return 0;
}

Comments