关于一个内存分配小程序的问题(41行)

Questions about a small memory allocation program (41 lines)

问题

  1. "memory storage array" allocbuf[10] 是否背靠背保存所有内容。例如 allocbuf[10] 已满 {F, i, r, s, t, \0, S, e, c, \0] 我将说明 allocbuf 是如何变满的。我想象我保存到 allocbuf 中的所有信息都是背靠背保存的。因此,作为示例,在使用 alloc(6) 并将返回的字符指针分配给变量 char *first 之后,字符指针 "first"指向allocbuf[0]。现在我赋值,first = "First";如果我想打印"First",我是否必须使用for循环,for (i = 0; i < 6; i++) { print allocbuf[i]; }打印出"First"?

  2. 如果我先assign = alloc(1) 然后再assign first = "First",就可以了,覆盖allocbuf的内容,对吗?

  3. 为什么我的代码中 printf("allocbuf: %s\n", allocbuf); 行没有打印出 allocbuf 的内容?

我相信我会对这个程序的更多功能感兴趣,我很高兴能得到你的帮助。

我很高兴阅读有关此程序、功能和内存分配的任何评论,尽管他们可能无法回答我提出的具体问题。所以请与我分享你的知识和经验。谢谢:)

P.S。我还没有在我的书 K&R C Programming 2nd ed 中遇到过 malloc(),所以,请不要评论说使用 malloc()。

代码

char *alloc(int); //return pointer to free storage in allocbuf[10]
void afree(int *); //free storage in allocbuf[10]
void strcpy2(char *, char *); //copy to, from
static char allocbuf[10];
static char* allocp = allocbuf;

main()
{
    char array4[5] = "4444";
    char* cp = "overwritten";
    char* copy = array4;
    char* occupyalloc;

    printf("cp: %s\n", cp); //"overwritten"
    printf("copy: %s\n", copy); //"4444"

    cp = "2"; //overwrite *cp = "overwritten" with *cp = "2"
    printf("cp: %s\n", cp); //"2"

    occupyalloc = alloc(4); //returns allocp 0, intended storage space is allocbuf[0] through allocbuf[3]

    cp = alloc(3); //returns allocp 4, intended use is allocbuf[4] through allocbuf[6]

    strcpy2(cp, copy); //copies "4444" into *cp, specifically allocbuf[4] through allocbuf[7]

    printf("cp: %s\n", cp); //"4444" , stored in allocbuf[4] through allocbuf[7] improperly
    printf("allocbuf: %s\n", allocbuf); //prints allocbuf -- not working
}

char *alloc(int n)
{
    if (allocbuf + ALLOCSIZE - allocp >= n) 
    {
        allocp += n;
        return allocp - n;
    }
    else
        return 0;
}

void afree(int *initial_storage_element_location)
{
    if (initial_storage_element_location >= allocbuf && initial_storage_element_location < allocbuf + ALLOCSIZE) 
        allocp = initial_storage_element_location;
}

void strcpy2(char *s, char *t)
{
    while (*s++ = *t++)
            ;
}

你应该明白,如果你有一行 cp = "2" 那么

  • 编译器创建一个 字符串文字 作为 "2"
  • 这个字符串文字一般存放在内存的只读位置。
  • 字符串文字的地址存储在指针中cp

现在,如果您将动态内存分配用作

 char *first = alloc(6);
 first = "First";

第一条语句中发生的事情是从静态缓冲区allocbuf分配内存,分配内存的地址存储在first中。使用第二条语句,您将用只读存储器中的一个位置覆盖指针 first。所以你丢失了之前分配的指针,你有 内存泄漏

如果您正在使用 alloc,您应该使用 strcpy

将数据 First 复制到内存中
strcpy(first, "First")

If I assign first = alloc(1) and then assign first = "First", it will work, and overwrite the contents of allocbuf, correct?

不,您没有覆盖 allocbuf 的内容,但您正在使用指向只读内存的新指针。

Why does the line printf("allocbuf: %s\n", allocbuf); not print out the contents of allocbuf in my code?

我将在发布的代码的上下文中回答这个问题。

occupyalloc = alloc(4); //returns allocp 0, intended storage space is allocbuf[0] through allocbuf[3]
cp = alloc(3); //returns allocp 4, intended use is allocbuf[4] through allocbuf[6]
strcpy2(cp, copy); //copies "4444" into *cp, specifically allocbuf[4] through allocbuf[7] 

printf("cp: %s\n", cp); //"4444" ,通过allocbuf[7]不正确地存储在allocbuf[4]中 printf("allocbuf: %s\n", allocbuf); // 打印 allocbuf -- 不工作

在这种情况下,allocbuf[0] 将是 [=24=],因为您没有修改 occupyalloc 变量。

另请注意,"4444" 需要 5 个字节,而您只分配了 3 个字节。