gpt4 book ai didi

c - 重置 C 中的字符缓冲区

转载 作者:行者123 更新时间:2023-11-30 17:57:58 24 4
gpt4 key购买 nike

我正在做一项家庭作业,我基本上需要创建一个字符缓冲区。我需要创建的函数之一称为“b_reset”。它的目的是重新初始化给定的缓冲区,以便它将指向字符缓冲区中的第一个位置。这是必需的,因为稍后,当将新字符添加到缓冲区时,需要将其添加到缓冲区中的第一个位置。

这是我迄今为止的代码:

结构:

typedef struct BufferDescriptor {
char * ca_head ;
int capacity ;
char inc_factor;
int addc_offset ;
int mark_offset ;
char r_flag;
char mode;
} Buffer ;

代码:

int b_reset ( Buffer *pB )
{
Buffer *temp = NULL;
int i = 0;
int j = 1;

if (pB == NULL)
{
return R_FAIL_1;
}
else
{
temp = (Buffer*)malloc(sizeof(Buffer*));
if (temp == NULL)
{
return R_FAIL_1;
}
temp->ca_head = (char*)malloc(pB->capacity);
if (!temp->ca_head)
{
temp = NULL;
return R_FAIL_1;
}

for(i = 0;i < ca_getsize(pB);++i)
{
temp->ca_head[j] = pB->ca_head[i];
j++;
}

pB->ca_head = temp->ca_head;

//free(temp->ca_head);
//free(temp);

return 0;
}
}

我在这段代码中的目标是创建一个临时缓冲区,它基本上会根据实际给定的缓冲区将所有内容移动 1 次。这将使第一个位置为空,以便可以添加另一个字符。

我遇到的问题是原始缓冲区在重置后似乎没有返回正确的值。

例如,当我这样做时:

temp->ca_head[0] = 'a';
temp->ca_head[1] = 'b';
temp->ca_head[2] = 'c';
temp->ca_head[3] = 'd';
temp->ca_head[4] = 'e';

b_reset(temp); //this will return the size as 0, when it's actually 5

//temp->ca_head[0] = 'i'; //if this is executed, it returns the size as 6
//and prints out the right values, but if it's not,
//it will not print out anything

printf("%d", ca_getsize(temp));
for(i = 0;i < ca_getsize(temp);++i)
{
printf("%c", temp->ca_head[i]);
}

我知道这里出了问题,但我不太确定出什么问题。任何建议将不胜感激。

最佳答案

此代码基于您的后续评论:

well I'm not trying to resize the buffer, I just want to create an empty space in the first position, so basically shifting everything to the right 1 time. The assumption is that there is a enough space in the buffer to handle this process.

我认为除了最初的操作之外,您不需要执行任何 malloc() 操作。您可以在循环中将所有内容向上移动:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define R_FAIL_1 1

#define BUFFER_SIZE 10

typedef struct BufferDescriptor {
char * ca_head ;
int capacity ;
char inc_factor;
int addc_offset ;
int mark_offset ;
char r_flag;
char mode;
} Buffer ;

void allocate_buffer(Buffer *pB, int size)
{
pB->ca_head = malloc(size);
assert(pB->ca_head);
pB->capacity = size;
}

int ca_getsize( Buffer *pB)
{
return pB->capacity;
}


int b_reset ( Buffer *pB )
{
int i = 0;

if (pB == NULL)
{
return R_FAIL_1;
}
else
{
if ( ca_getsize(pB) <= 0 || pB->ca_head == NULL )
return R_FAIL_1;
}
// shift data up by 1 byte
for( i = ca_getsize(pB) - 1 ; i > 0;i-- )
{
pB->ca_head[i] = pB->ca_head[i-1];
}
pB->ca_head[0] = '\0';
return 0;
}

void print_buffer(Buffer *pB)
{
printf("capacity: %d \n", ca_getsize(pB));
for (int i = 0;i < ca_getsize(pB);++i)
{
printf("buffer(%d): [%d] ",i, pB->ca_head[i]);
}
printf("\n");
}


int main(void)
{
Buffer a_buffer;
allocate_buffer(&a_buffer,BUFFER_SIZE);
strcpy(a_buffer.ca_head,"abcdefgh");
print_buffer(&a_buffer);
int ret = b_reset(&a_buffer);
assert(ret == 0);
print_buffer(&a_buffer);
}

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

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