gpt4 book ai didi

c - C中缓冲区的分配

转载 作者:太空宇宙 更新时间:2023-11-04 03:28:57 24 4
gpt4 key购买 nike

我正在尝试创建缓冲区来存储无限大小的符号。

我有这样的结构:

typedef struct buffer {  
int bufferSize;
int literalSize;
int allocatedSize;
char *bufferPtr;
} bufferStruct;

在我的file.h中。

我还有缓冲区的功能:

bufferStruct *BufferInitialize(int size) {
bufferStruct *tempBuff;
tempBuff = (bufferStruct *)malloc(sizeof(bufferStruct));
if (tempBuff == NULL) {
exit(99); // MEMORY_ERROR
}
tempBuff->bufferSize = size;
tempBuff->literalSize = 0;
tempBuff->bufferPtr = NULL;
tempBuff->allocatedSize = 0;

return (tempBuff);
}

int addToBuffer(bufferStruct *buffer, char c) {
if (buffer == NULL) {
return 99; // MEMORY_ERROR
}
if (buffer->allocatedSize > buffer->literalSize) {
buffer->bufferPtr[buffer->literalSize++] = c;
} else {
buffer->bufferPtr = realloc(buffer->bufferPtr, (buffer->allocatedSize + buffer->bufferSize) * sizeof(char));
if (buffer->bufferPtr == NULL) {
return 99; // MEMORY_ERROR
}
buffer->allocatedSize += buffer->bufferSize;
buffer->bufferSize <<= 1; // bS = bS * 2
buffer->bufferPtr[buffer->literalSize++] = c;
}
return 0;
}

int bufferDestroy(bufferStruct *buffer) {
if (buffer == NULL) {
return 99; // MEMORY_ERROR
}
free(buffer->bufferPtr);
free(buffer);
return 0;
}

在我的 file.c 中,我正在尝试创建缓冲区:

 token *getNextToken(token *tokenT) {
token *actualToken = NULL;
char *bufferData = NULL;
int charFromFile;
eState state = stateInit;

bufferStruct *bufferT = NULL;

while ((charFromFile = fgetc(fp))) {
switch (state) {
case stateInit: {
if (isdigit(charFromFile)) {
bufferT = BufferInitialize(8);
addToBuffer(bufferT, charFromFile);
state = stateInt;
} else
if (isalpha(charFromFile) || (charFromFile == '_')) {
state = stateId;
bufferT = BufferInitialize(16);
addToBuffer(bufferT, charFromFile);
} else
if (isspace(charFromFile)) {
state = stateInit;

... some more conditions ... it's similar, a lot.

case stateInt: {
if (isdigit(charFromFile)) {
state = stateInt;
addToBuffer(bufferT, charFromFile);
} else
if ((charFromFile == 'e') || (charFromFile == 'E')) {
state = stateExp;
addToBuffer(bufferT, charFromFile);
} else
if (charFromFile = '.') {
state = stateDouble;
addToBuffer(bufferT, charFromFile);
} else {
bufferData = bufferT->bufferPtr;
//strcpy(actualToken->content, bufferData);
addToBuffer(bufferT, '\0');
bufferDestroy(bufferT);
actualToken->type = tokenInt;
return actualToken;
}
} break;

... other similar cases ...
}
}
}

问题是当我尝试这样做时,Visual Studio 给我错误:

One or more multiply defined symbols found Also gives me already defined in main.obj for every function I have.

我看不到出路。我究竟做错了什么 ?

最佳答案

您的代码中存在多个问题:

  • 您不应该将代码放在头文件中。函数 BufferInitialize 不应位于 file.h 中,除非它被定义为 inline

  • 测试 while (c = fgetc(fp)) 不正确:您使用赋值作为测试表达式,它很容易出错,您至少应该将赋值表达式括起来,并可能测试 EOF 而不是 '\0':while ((c = fgetc(fp)) != EOF)。此外,c 必须定义为 int。发布实际代码,而不是伪代码。

  • 您将 tempBuff->bufferSize 初始化为一个可能非零的值,而 allocatedSize0 且缓冲区未分配.这似乎不正确。

您的实际代码中可能存在更多问题,我们看不到代码的内容,如何告诉您这些问题?始终发布一个完整的、可编译的代码来演示问题。

关于c - C中缓冲区的分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38696653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com