Hi,
well i have been playing around with helper functions like you guys suggested. and for small examples it works but for my particular example i don't know how to make it work. so i have a queue(array based) and a want to place in ti numbers and the kick them out. But the numbers can be either 64 or 32 bit integers.
Example:
main.c
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define HELPER_TYPE uint32_t
#define HELPER_NAME(base) base ## _u32
#include "main-helper.c"
#undef HELPER_NAME
#undef HELPER_TYPE
#define HELPER_TYPE uint64_t
#define HELPER_NAME(base) base ## _u64
#include "main-helper.c"
#undef HELPER_NAME
#undef HELPER_TYPE
void main(){
long int64[10];
int int32[10];
int i f,k;
long j,l;
for(i=0;i<10;i++){
int32[i]=i;
}
for(j=0;j<10;j++){
int64[j]=j;
}
QueueCnt_u32 *queue = NULL;
queue = (QueueCnt_u32 *)emalloc(sizeof(QueueCnt_u32));
queue->front =0; queue->rear = 0; queue->count = 0; queue->last = 0;
for(i=0;i<10;i++){
printf("add:%d\n",int32[i]);
add_u32(int32[i],queue);
if(i==5){
f= checkFront_u32(queue);
printf("checkF:%d\n",f);
f =checkRear_u32(queue);
printf("checkR:%d\n",f);
for(k=0;k<=5;k++){
f= del_u32(queue);
printf("del:%d\n",f);
}
}
}
f= checkFront_u32(queue);
printf("checkF:%d\n",f);
f =checkRear_u32(queue);
printf("checkR:%d\n",f);
for(k=0;k<5;k++){
f= del_u32(queue);
printf("del:%d\n",f);
}
QueueCnt_u64 *queueX = NULL;
queueX = (QueueCnt_u64 *)emalloc(sizeof(QueueCnt_u64));
queueX->front =0; queueX->rear = 0; queueX->count = 0; queueX->last = 0;
for(i=0;i<10;i++){
printf("add:%ld\n",int64[i]);
add_u64(int64[i],queueX);
if(i==5){
l= checkFront_u64(queueX);
printf("checkF:%ld\n",l);
l =checkRear_u64(queueX);
printf("checkR:%ld\n",f);
for(k=0;k<=5;k++){
l= del_u64(queueX);
printf("del:%ld\n",l);
}
}
}
l= checkFront_u64(queueX);
printf("checkF:%ld\n",l);
l =checkRear_u64(queueX);
printf("checkR:%ld\n",l);
for(k=0;k<5;k++){
l= del_u64(queueX);
printf("del:%ld\n",l);
}
}
main-helper.c
Code:
#include <stdlib.h>
#define MAXWSIZE 15
typedef struct HELPER_NAME(QueueCnt) { /* queue */
HELPER_TYPE q[MAXWSIZE]; /* wheel */
HELPER_TYPE front, rear, count, last; /* counters */
}HELPER_NAME(QueueCnt);
HELPER_TYPE HELPER_NAME(addone) (const HELPER_TYPE i) {
return ((i+1) % MAXWSIZE);
}
void HELPER_NAME(add)(const HELPER_TYPE a,HELPER_NAME(QueueCnt) *cnt){
if(cnt->rear>MAXWSIZE){
printf("QUEUE FULL\n");
return;
}
else{
cnt->count++;
cnt->q[cnt->rear]=a;
cnt->last = cnt->rear;
cnt->rear = addone(cnt->rear);
}
}
HELPER_TYPE HELPER_NAME(del)(HELPER_NAME(QueueCnt) *cnt){
HELPER_TYPE a;
if(cnt->front == cnt->rear){
printf("QUEUE EMPTY\n");
return(0);
}
else{
cnt->count--;
a=cnt->q[cnt->front];
cnt->front= addone(cnt->front);
}
return(a);
}
HELPER_TYPE HELPER_NAME(checkFront)(HELPER_NAME(QueueCnt) *cnt){
HELPER_TYPE a;
a=cnt->q[cnt->front];
return (a);
}
HELPER_TYPE HELPER_NAME(checkRear)(HELPER_NAME(QueueCnt) *cnt){
HELPER_TYPE a;
a=cnt->q[cnt->last];
return (a);
}
Oh yes and the error is :
/tmp/ccPdRHLm.o: In function `add_u32':
deal.c
.text+0x91): undefined reference to `addone'
/tmp/ccPdRHLm.o: In function `del_u32':
deal.c
.text+0x101): undefined reference to `addone'
/tmp/ccPdRHLm.o: In function `add_u64':
deal.c
.text+0x214): undefined reference to `addone'
/tmp/ccPdRHLm.o: In function `del_u64':
deal.c
.text+0x29c): undefined reference to `addone'
/tmp/ccPdRHLm.o: In function `main':
deal.c
.text+0x35b): undefined reference to `emalloc'
deal.c
.text+0x506): undefined reference to `emalloc'
collect2: ld returned 1 exit status