2018-12-28 12:59:57
结构与malloc
结构是C语言中重要的一环,malloc是一个重要的函数,它完成了动态内存分配,用malloc分配的内存块要通过free释放。通过结构可以将不同类型的数据组合成一个整体,关于结构指针,LINUX下编程经常会运用一个技巧,这个技巧用在申请缓冲区上,可以申请不同大小的缓冲区。
首先,来看一个概念消息队列 ,一个或多个进程可向消息队列写入消息,而一个或多个进程可从消息队列中读取消息,Linux中的消息被描述成在内核地址空间的一个内部链表,每一个消息队列由一个IPC的标识号唯一的标识,Linux 为系统中所有的消息队列维护一个 msgque 链表,每个消息队列都在系统范围内对应唯一的键值,要获得一个消息队列的描述字,只需提供该消息队列的键值即可。
传递给队列的消息的数据类型是一个如下形式的结构,在Linux 的系统库linux/msg.h 中,它是这样定义的:
/ message buffer for msgsnd and msgrcv calls /
struct msgbuf {
long mtype; / type of message /
char mtext[1]; / message text /
};
其中,mtype成员代表消息类型,从消息队列中读取消息的一个重要依据就是消息的类型;mtext是消息内容。这个结构的精妙之处在于,mtext虽然在结构中被声明为大小为1的字符,但实际消息内容的长度可以由程序员任意定制,定制的关键在malloc函数。下面是部分代码段:
msg=(struct msgbuf*)malloc(sizeof(struct msgbuf)+100);//100为消息的长度,msgbuf结构只有2个成员一个成员是mytpe,另一个成员是一个字节的mtext,在结构后分配更多的空间以存放消息字符串
完整代码(演示了公共消息队列的使用)为:
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
#define QUE_ID 2
//使用公共消息队列,读写进程可以不同时运行。
int main(void){
int queue_id;
struct msgbuf *msg;
int rc;
//建立消息队列
queue_id=msgget(QUE_ID,IPC_CREAT|0600);//QUE_ID为一个正整数,公共消息队列的ID
if (queue_id==-1){
perror("create queue error!\n");
exit(1);
}
printf("message %d queue created!\n",queue_id);
//创建发送消息结构
printf("message send....\n");
msg=(struct msgbuf*)malloc(sizeof(struct msgbuf)+100);//100为消息的长度,msgbuf结构只有2个成员一个成员是mytpe,另一个成员是一个字节的mtext,在结构后分配更多的空间以存放消息字符串
msg->mtype=1;//消息类型,正整数
strcpy(msg->mtext,"deepfuture.iteye.com");
//发送消息
rc=msgsnd(queue_id,msg,100,0);
//最后一个参数可以是是0与随后这些值(或者就是0):IPC_NOWAIT,如果消息类型没有则立即返回,函数调用失败
//MSG_EXCEPT,当消息类型大于0时,读与消息类型不同的第一条消息
//MSG_NOERROR,如果消息长度大于100字节则被截掉
if (rc==-1){
perror("msgsnd error\n");
exit(1);
}
free(msg);//发送完毕,释放内存
printf("message sended!\n");
return 0;
}
以上是发送消息,以下是接收消息
#define _GNU_SOURCE
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/types.h>
#define QUE_ID 2
//使用公共消息队列,读写进程可以不同时运行。
int main(void){
int queue_id;
struct msgbuf *msg;
int rc;
//取得消息队列
queue_id=msgget(QUE_ID,0);//QUE_ID为一个正整数,公共消息队列的ID,
if (queue_id==-1){
perror("get queue error!\n");
exit(1);
}
printf("message recv....\n");
msg=(struct msgbuf*)malloc(sizeof(struct msgbuf)+100);
rc=msgrcv(queue_id,msg,101,0,0);
if (rc==-1){
perror("recv error\n");
exit(1);
}
printf("recv:%s\n",msg->mtext);
return 0;
}
效果
deepfuture@deepfuture-laptop:~/private/mytest$ ./testmessnd
message 0 queue created!
message send…
message sended!
deepfuture@deepfuture-laptop:~/private/mytest$ ./testmesrecv
message recv…
recv:deepfuture.iteye.com
deepfuture@deepfuture-laptop:~/private/mytest$
7、字符串常量
#include <stdio.h>
int main(int argc,int **argv){
printf ("%s","abcdefgh"+2);
}
dp@dp:~/test1 % cc test3.c -o mytest
dp@dp:~/test1 % ./mytest
cdefgh
8、函数指针
通过如下格式来声明函数指针:
返回类型 (*函数指针变量名)(参数列表)
int add(int a,int b);
int main(void){
int (*myfunc)(int a,int b);
myfunc=add;
int x=myfunc(12,36);
printf("%d",x);
return 1;
}
int add(int a,int b){
return a+b;
}
~
dp@dp:~/test1 % cc test1.c -o mytest
dp@dp:~/test1 % ./mytest
48
8、命令行参数
打印参数个数,注意,命令本身也是一个参数,所以argc至少为1。
#include <stdio.h>
int main(int argc,char **argv){
printf("%d\n",argc);
return 1;
}
~
dp@dp:~/test1 % cc test2.c -o mytest
dp@dp:~/test1 % ./mytest 12
下面没有使用argc参数,直接使用了argv参数,通过判断是否null,来决定参数列表是否结束
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char **argv){