[英]Error with typedef and function declaration
I don't understand why I it tells I have an invalid function declaration, when I'm just declaring a variable. 我不明白为什么我在声明变量时会告诉我函数声明无效。 Also, I don't get why my typedef did not work. 另外,我不明白为什么我的typedef无法正常工作。
Username@Server 388> g++ -Wall -pthread testHashT.cpp -o testHashT memHashT.h:22: error: invalid function declaration memHashT.h: In function âvoid memAccessUpdate(void*, unsigned int, pthread_t, bool)â: memHashT.h:114: error: cannot convert âmList*â to âlinkedMlist*â in assignment memHashT.h:119: error: cannot convert âlinkedMlist*â to âmList*â in assignment memHashT.h:128: error: âcountWSâ was not declared in this scope memHashT.h:133: error: âcountRAâ was not declared in this scope
Username@Server 389> cat memHashT.h
//Basic hash table for memory addresses, recommended size for program running is table indexed by a prime ~81281
/************************************************************
* Structure of table:
* Hash table indexed by memory address
* at each entry a vector made of arrays with size 2 of void pointers
* each vector entry will have the unique memory address and a pointer to a vector
* the vector it points to will contain a list of all threads that accessed that location
*
* Overall:(currently being changed to HT=>LL=>LL
* Hash Table => Vector => Vector Containing threads that accessed a given memory location
*************************************************************/
#include <pthread.h>//same as bellow
#include <stdlib.h>//watch out in case actual function contains this
//using namespace std;
//Global var
unsigned int tableSize; //note this is best if it is a prime number
unsigned long long countWA;
unsigned long long countRA:
unsigned long long countWS;
unsigned long long countRS;
//Linked Lists (thread, then mem)
//added all information in T_list to the M list, can be deleted
/*struct linkedT_list {
int threadID;
struct linkedT_list * nextT;
};
typedef struct linkedT_list tList;
tList * currT, * headT;*/
//For memory addresses
struct linkedM_list {
void * memAddr;
//Details
bool shared;
pthread_t prevThread;
unsigned long long rCounter;
unsigned long long wCounter;
//End Details
struct linkedMlist * nextM;
};
typedef struct linkedM_list mList;
//mList * currM, * headM;
mList ** hashTable;
//computes the index of the hash table
//made its own function in case future implementation need to change how indexing is set up
unsigned int getHTIndex (void * arg){
return (unsigned int) ((unsigned long long)arg%tableSize);
}
//returns pointer to mList containing information
//NOT CORRECTLY IMPLEMENTED YET, NEED TO FIND RIGHT MEM ADDRESS, NOT JUST TOP MEM
mList * getHTElement(void * arg){
mList * tempM;
//tList * tempT = NULL;
unsigned int index = getHTIndex(arg);
tempM = hashTable[index];
//tempT = tempM->threadList;
return tempM;
}
//remove a element
void removeHTElement(void * memArg){
//note no garbage collection yet
;
}
//returns the number of threads to access a memery location
int tLength(void * arg){
return -1;
}
//Create the hash table
int createHashTable(unsigned int num) {
tableSize = num;
hashTable = (mList **) malloc(sizeof(mList) * tableSize);
if (hashTable == NULL)
{
return 0;
//printf("Error: Memory could not be allocated");
}
else {
unsigned int i;
for(i=0;i<tableSize;i++)
{
hashTable[i]=NULL;
}
}
return 1;
}
void destroyHashTable(){
free(hashTable);
}
//adds a element to the hash table
void memAccessUpdate(void * memArg, unsigned int thread, pthread_t thread_id, bool writeAccess){
mList * headM = getHTElement(memArg);
mList * currM;
if (headM == NULL)
{//then create and new mList
currM = (mList *)malloc(sizeof(mList));
//initialize values
currM->shared = false;
currM->prevThread = thread_id;
currM->rCounter = 0;
currM->wCounter = 0;
currM->nextM = hashTable[getHTIndex(memArg)];
hashTable[getHTIndex(memArg)] = currM;
}
else {//change details in linked list and global var
//headM->nextM = (mList *)malloc(sizeof(mList));
currM = headM->nextM;
if (thread_id != currM->prevThread){
currM->shared = true;
currM->prevThread = thread_id;
}
if(writeAccess)
{
countWA++;
if(currM->shared){
countWS++;
}
currM->wCounter++;
}
else{//mem read
countRA++;
if(currM->shared){
countRS++;
}
currM->rCounter++;
}
}
//if (stuff) //possibly don't need
//else
// head = hashTable[index]; //note may be null
// curr = (mList *)malloc(sizeof(mList));
// curr->
}
I'm very confused as to why I'm getting those errors. 我很困惑为什么会收到这些错误。 Any help is appreciated. 任何帮助表示赞赏。 (I know its not good practice to declare functions in a header, but for now I'm just looking to correct my syntax). (我知道在标头中声明函数不是一个好习惯,但是现在我只是想纠正我的语法)。
Got it! 得到它了! It took me a while, what with my eyesight...Look carefully at the last character of line 22 :-) 我花了一段时间,用我的视力……仔细看第22行的最后一个字符:-)
Also, decide whether you want to call it linkedMlist
or linkedM_list
. 另外,决定是否要将其命名为linkedMlist
或linkedM_list
。 (That one wasn't so hard on the eyes.) (那只眼睛并不那么硬。)
struct linkedMlist
is not defined or declared, as far as I can see 据我所知,未定义或声明struct linkedMlist
struct linkedM_list {
void * memAddr;
//Details
bool shared;
pthread_t prevThread;
unsigned long long rCounter;
unsigned long long wCounter;
//End Details
struct linkedMlist * nextM; // <===== HERE
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.