将指针延迟到不完整的C类型结构CUNIT
对不起,我将指针延迟到不完整的类型,我在buffer.c文件中定义了buffer\t,我在CUNIT中,在每次测试之前初始化缓冲区将指针延迟到不完整的C类型结构CUNIT,c,pointers,cunit,C,Pointers,Cunit,对不起,我将指针延迟到不完整的类型,我在buffer.c文件中定义了buffer\t,我在CUNIT中,在每次测试之前初始化缓冲区 #include "msg.h" #include <stdio.h> #include <string.h> #include "CUnit/Basic.h" #include <pthread.h> #include "buffer.h" /* Pointer to the file used by the tests. */
#include "msg.h"
#include <stdio.h>
#include <string.h>
#include "CUnit/Basic.h"
#include <pthread.h>
#include "buffer.h"
/* Pointer to the file used by the tests. */
buffer_t * buffer = NULL;
/* The suite initialization function.
* Opens the temporary file used by the tests.
* Returns zero on success, non-zero otherwise.
*/
int init_suite1(void)
{
buffer=buffer_init(5);
if(buffer!=NULL){
printf("buffer creato");
}
}
/* The suite cleanup function.
* Closes the temporary file used by the tests.
* Returns zero on success, non-zero otherwise.
*/
int clean_suite1(void)
{
buffer_destroy(buffer);
if(buffer==NULL){
printf("buffer distrutto");
}
}
//(P=1; C=0; N=1) Produzione di un solo messaggio in un buffer vuoto
void test1(void)
{
msg_t* msg = msg_init_string("ciao");
put_nb(buffer, msg);
CU_ASSERT(1 == buffer->msg_presenti);
CU_ASSERT( "ciao"== (*(buffer->array_msg[0]))->content);
这里是buffer.c:
/*
* Buffer.c
*
* Created on: 19/nov/2013
* Author: lele */
#include "buffer.h"
#include <stdio.h>
#include "msg.h"
#include <stdlib.h>
#include <pthread.h>
#define BUFFER_ERROR (msg_t *) NULL
typedef struct buffer {
int size;
int T;
int D;
int msg_presenti;
pthread_cond_t not_full;
pthread_cond_t not_empty;
pthread_mutex_t mutex;
msg_t ** array_msg;
} buffer_t;
/* allocazione / deallocazione buffer */
// creazione di un buffer vuoto di dim. max nota
buffer_t * buffer_init(unsigned int maxsize){
buffer_t * new_buffer = (buffer_t*)malloc( sizeof(buffer_t) );
new_buffer->T=0;
new_buffer->D=0;
new_buffer->msg_presenti=0;
new_buffer->array_msg =(msg_t **)malloc(sizeof(msg_t*) * maxsize);
new_buffer->size=maxsize;
pthread_cond_init(&(new_buffer->not_empty),NULL);
pthread_cond_init(&(new_buffer->not_full),NULL);
pthread_mutex_init(&(new_buffer->mutex),NULL);
return new_buffer;
}
// deallocazione di un buffer
void buffer_destroy(buffer_t* buffer){
int i;
for(i=0;i<buffer->size;i++){
msg_destroy_string(buffer->array_msg[i]);
}
free(buffer->array_msg);
free(buffer);
}
msg_t* put_non_bloccante(buffer_t* buffer, msg_t* msg){
// inserimento non bloccante: restituisce BUFFER_ERROR se pieno,
// altrimenti effettua l'inserimento e restituisce il messaggio
// inserito; N.B.: msg!=null
if( ( pthread_mutex_trylock(&(buffer->mutex))) !=0){
return BUFFER_ERROR; }
while(buffer->msg_presenti==buffer->size){
pthread_cond_wait(&(buffer->not_full),&(buffer->mutex));
}
buffer->array_msg[buffer->D]=msg;
buffer->D = (buffer->D+1)%buffer->size;
buffer->msg_presenti= buffer->msg_presenti+1;
pthread_cond_signal(&(buffer->not_empty));
pthread_mutex_unlock(&(buffer->mutex));
return msg;
}
// estrazione bloccante: sospende se vuoto, quindi
// restituisce il valore estratto non appena disponibile
msg_t* get_bloccante(buffer_t* buffer){
// estrazione non bloccante: restituisce BUFFER_ERROR se vuoto
// ed il valore estratto in caso contrario
pthread_mutex_lock(&(buffer->mutex));
while(buffer->msg_presenti==0){
pthread_cond_wait(&(buffer->not_empty),&(buffer->mutex));
}
msg_t * msg =buffer->array_msg[buffer->T];
buffer->T=(buffer->T+1)%buffer->size;
buffer->msg_presenti--;
pthread_cond_signal(&(buffer->not_full));
pthread_mutex_unlock(&(buffer->mutex));
return msg;
}
msg_t* get_non_bloccante(buffer_t* buffer){
if( (pthread_mutex_trylock(&(buffer->mutex))) !=0){
return BUFFER_ERROR;
}
while(buffer->msg_presenti==0){
pthread_cond_wait(&(buffer->not_empty),&(buffer->mutex));
}
msg_t* msg =buffer->array_msg[buffer->T];
buffer->T=(buffer->T+1)%buffer->size;
buffer->msg_presenti--;
pthread_cond_signal(&(buffer->not_full));
pthread_mutex_unlock(&(buffer->mutex));
return msg;
}
msg_t* put_bloccante(buffer_t *buffer, msg_t *msg){
// inserimento non bloccante: restituisce BUFFER_ERROR se pieno,
// altrimenti effettua l'inserimento e restituisce il messaggio
// inserito; N.B.: msg!=null
pthread_mutex_lock(&(buffer->mutex));
while(buffer->msg_presenti==buffer->size){
pthread_cond_wait(&(buffer->not_full),&(buffer->mutex));
}
buffer->array_msg[buffer->D]=msg;
buffer->D = (buffer->D+1)%buffer->size;
buffer->msg_presenti= buffer->msg_presenti+1;
pthread_cond_signal(&(buffer->not_empty));
pthread_mutex_unlock(&(buffer->mutex));
return msg;
}
/*
*缓冲区c
*
*创建日期:2013年11月19日
*作者:乐乐*/
#包括“buffer.h”
#包括
#包括“msg.h”
#包括
#包括
#定义缓冲区错误(msg\u t*)NULL
类型定义结构缓冲区{
整数大小;
int T;
int D;
int msg_presenti;
p线程状态未满;
pthread_cond_t不为空;
pthread_mutex_t mutex;
消息**数组消息;
}缓冲区;
/*allocazione/deallocazione缓冲液*/
//克雷嗪酮在缓冲液中的含量很低。马克斯诺塔酒店
buffer_t*buffer_init(无符号整数maxsize){
buffer_t*new_buffer=(buffer_t*)malloc(sizeof(buffer_t));
新建缓冲区->T=0;
新建缓冲区->D=0;
新建缓冲区->msg\U presenti=0;
新建缓冲区->数组\u msg=(msg\u t**)malloc(sizeof(msg\u t*)*maxsize);
新建缓冲区->大小=最大大小;
pthread_cond_init(&(new_buffer->not_empty),NULL);
pthread_cond_init(&(新建缓冲区->未满),NULL);
pthread_mutex_init(&(new_buffer->mutex),NULL);
返回新的缓冲区;
}
//去甲氧基噻吩缓冲液
无效缓冲区销毁(缓冲区*buffer){
int i;
对于(i=0;isize;i++){
msg_destroy_字符串(缓冲区->数组_msg[i]);
}
空闲(缓冲区->数组\u msg);
自由(缓冲);
}
msg\u t*put\u non\u bloccante(buffer\u t*buffer,msg\u t*msg){
//插入非bloccante:restituisce缓冲区错误,
//在信息系统中,有效的休息和休息
//插入;注意:msg!=null
if((pthread\u mutex\u trylock(&(buffer->mutex))!=0){
返回缓冲区\u错误;}
while(buffer->msg_presenti==buffer->size){
pthread_cond_wait(&(缓冲区->未满),&(缓冲区->互斥体));
}
buffer->array_msg[buffer->D]=msg;
缓冲区->D=(缓冲区->D+1)%buffer->大小;
buffer->msg\u presenti=buffer->msg\u presenti+1;
pthread_cond_信号(&(buffer->not_empty));
pthread_mutex_unlock(&(buffer->mutex));
返回味精;
}
//埃斯特拉齐昂·布卢坎特:昆迪,索斯潘德·塞沃托
//无争议的休息日
msg\u t*get\u bloccante(buffer\u t*buffer){
//estrazione non bloccante:Restituise BUFFER_ERROR se vuoto
//艾德·瓦洛雷·埃斯特拉托在卡索·康特拉里奥
pthread_mutex_lock(&(buffer->mutex));
而(缓冲区->消息\u presenti==0){
pthread_cond_wait(&(buffer->not_empty),&(buffer->mutex));
}
msg_t*msg=buffer->array_msg[buffer->t];
缓冲区->T=(缓冲区->T+1)%buffer->大小;
缓冲区->消息保存--;
pthread_cond_信号(&(缓冲区->未满));
pthread_mutex_unlock(&(buffer->mutex));
返回味精;
}
msg\u t*get\u non\u bloccante(buffer\u t*buffer){
if((pthread\u mutex\u trylock(&(buffer->mutex))!=0){
返回缓冲区错误;
}
而(缓冲区->消息\u presenti==0){
pthread_cond_wait(&(buffer->not_empty),&(buffer->mutex));
}
msg_t*msg=buffer->array_msg[buffer->t];
缓冲区->T=(缓冲区->T+1)%buffer->大小;
缓冲区->消息保存--;
pthread_cond_信号(&(缓冲区->未满));
pthread_mutex_unlock(&(buffer->mutex));
返回味精;
}
msg_t*put_bloccante(buffer_t*buffer,msg_t*msg){
//插入非bloccante:restituisce缓冲区错误,
//在信息系统中,有效的休息和休息
//插入;注意:msg!=null
pthread_mutex_lock(&(buffer->mutex));
while(buffer->msg_presenti==buffer->size){
pthread_cond_wait(&(缓冲区->未满),&(缓冲区->互斥体));
}
buffer->array_msg[buffer->D]=msg;
缓冲区->D=(缓冲区->D+1)%buffer->大小;
buffer->msg\u presenti=buffer->msg\u presenti+1;
pthread_cond_信号(&(buffer->not_empty));
pthread_mutex_unlock(&(buffer->mutex));
返回味精;
}
和缓冲区。H:
#ifndef BUFFER_H_
#define BUFFER_H_
#endif BUFFER_H_
#include <pthread.h>
#include "msg.h"
#define BUFFER_ERROR (msg_t *) NULL
typedef struct buffer buffer_t;
buffer_t * buffer_init(unsigned int maxsize);
void buffer_destroy(buffer_t* buffer);
msg_t* put_non_bloccante(buffer_t* buffer, msg_t* msg);
msg_t* get_bloccante(buffer_t* buffer);
msg_t* get_non_bloccante(buffer_t* buffer);
msg_t* put_bloccante(buffer_t *buffer, msg_t *msg);
#ifndef缓冲区_
#定义缓冲区_
#endif缓冲区_
#包括
#包括“msg.h”
#定义缓冲区错误(msg\u t*)NULL
类型定义结构缓冲区;
buffer_t*buffer_init(无符号整数maxsize);
无效缓冲区销毁(缓冲区*buffer);
msg_t*put_non_bloccante(buffer_t*buffer,msg_t*msg);
msg_t*get_bloccante(buffer_t*buffer);
msg_t*get_non_bloccante(buffer_t*buffer);
msg_t*put_bloccante(buffer_t*buffer,msg_t*msg);
buffer\t是否仅在buffer.c文件中定义,或者buffer.h中是否有声明?我们确实需要查看您的buffer.c和buffer.h文件。您无法访问不完整类型的成员。如果您需要这样做,您需要通过将结构定义移动到其他代码可以看到的地方(如头文件中),使其成为一个完整的类型。我添加了buffer.c和.h。是的,buffer.h中有一个声明,问题在哪里?buffer.h中有一个typedef,但不是struct buffer
(显示其成员的代码)…因此其他代码看不到完整定义,无法访问其成员。
#ifndef BUFFER_H_
#define BUFFER_H_
#endif BUFFER_H_
#include <pthread.h>
#include "msg.h"
#define BUFFER_ERROR (msg_t *) NULL
typedef struct buffer buffer_t;
buffer_t * buffer_init(unsigned int maxsize);
void buffer_destroy(buffer_t* buffer);
msg_t* put_non_bloccante(buffer_t* buffer, msg_t* msg);
msg_t* get_bloccante(buffer_t* buffer);
msg_t* get_non_bloccante(buffer_t* buffer);
msg_t* put_bloccante(buffer_t *buffer, msg_t *msg);