cpp中delete数组

例1:

#include <iostream>
using namespace std;
class A {
public:
   A() { cout << "constructor" << endl; }
   ~A() { cout << "desconstructor" << endl; }
};

int main() {
   A* p = new A[3] ;
   delete[] p;
}

输出:

constructor constructor constructor desconstructor desconstructor desconstructor

结论:正确释放内存。

例2:

    #include <iostream>

    using namespace std;

    class A { 
    public:
        A() { cout << "constructor" << endl; }
        ~A() { cout << "desconstructor" << endl; }
    };

    int main() {
        A** p = new A*[3] ;
        for(int i = 0; i < 3; i++)
          p[i] = new A;

        delete[] p;
    }

输出:

constructor constructor constructor

结论:没有如想象正确调用虚构函数

例3:

    #include <iostream>

    using namespace std;

    class A { 
    public:
        A() { cout << "constructor" << endl; }
        ~A() { cout << "desconstructor" << endl; }
    };

    int main() {
        A** p = new A*[3] ;
        for(int i = 0; i < 3; i++)
          p[i] = new A;
        for(int i = 0; i < 3; i++)
          delete p[i];

        delete[] p;
    }

输出:

constructor constructor constructor desconstructor desconstructor desconstructor

结论:正确调用虚构函数

MacOS使用ZThread错误 lazy symbol binding failed

OS: macos

使用编译命令:

g++ -Wall -g -o a a.cpp  -lzthread

编译顺利通过,运行时出现如下报错:

dyld: lazy symbol binding failed: Symbol not found: _MPCreateCriticalRegion Referenced from: /usr/local/lib/libZThread-2.3.2.dylib Expected in: flat namespace

dyld: Symbol not found: _MPCreateCriticalRegion Referenced from: /usr/local/lib/libZThread-2.3.2.dylib Expected in: flat namespace

Trace/BPT trap: 5

解决方法: 需要在编译时加入carbon framework:

g++ -Wall -g -o a a.cpp -framework Carbon -lzthread

Linux C单例执行程序

保证系统种某个进程只有一个实例在运行,举例下面2种方法:

  1. 使用popen执行ps -ef 后分析结果
  2. 使用文件锁 代码如下:
第一种:
int exist(void){
    FILE* fp;
    char buf[10] = {0};
    char command[100] = {0};
    sprintf(command, "ps -ef | grep %s | grep -v grep | wc -l", argv[0]);
    printf("command : %s\n", command);
    fp = popen(command, "r");
    if(fp == NULL)
    {
        perror("popen error!");
        return -1;
    }
    if((fgets(buf, 10, fp))!= NULL)
    {
        if(atoi(buf) != 0)
        {
            printf("%s is running!", argv[0]);
        }
        else
        {
            printf("%s is not running!", argv[0]);
        }
    }
    pclose(fp);
    return 0;
}
第二种
#define LOCKFILE  "/var/run/lte8718wdt.pid"
#define LOCKMODE  (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)

int lockfile(int fd)
{
    struct flock fl;
    int ret;

    fl.l_type = F_WRLCK;
    fl.l_start = 0;
    fl.l_whence = SEEK_SET;
    fl.l_len = 0; // lock the whole file

    ret = fcntl(fd, F_SETLK, &fl);
    if(-1 == ret)
        perror("set file lock error!");
    return ret;
}
int is_running(const char* filename)
{
    int fd;
    char pid[15] = {0};

    fd = open(filename, O_RDWR | O_CREAT, LOCKMODE);
    if(fd < 0)
    {
        perror("open file error!");
        return -1;
    }
    if(lockfile(fd) == -1)
    {
        if(errno == EACCES || errno == EAGAIN)
        {
            printf("%s already locked\n", filename);
            close(fd);
            return -1;
        }
    }
    ftruncate(fd, 0);
    sprintf(pid, "%ld", (long)getpid());
    write(fd, pid, sizeof(pid) - 1);
    return 0;
}

Linux Kernel Driver自动创建设备

static int watchdog_init(void)
{
	wdt_major = register_chrdev(0, "lte8718wdt", <e8718wdt_fopts);
	wdt_class = class_create(THIS_MODULE, wdt_name);
	device_create(wdt_class, NULL, MKDEV(wdt_major, 0), NULL, wdt_name);
}

static int watchdog_exit(void)
{
	device_destroy(wdt_class, MKDEV(wdt_major, 0));
	class_destroy(wdt_class);
	unregister_chrdev(lte8718wdt_major, "lte8718wdt");
}

Linux清理系统文件缓存

OS : opensuse11.4

echo 1>/proc/sys/vm/drop_caches
echo 2>/proc/sys/vm/drop_caches
echo 3>/proc/sys/vm/drop_caches