1

所以我写了这段代码,但它每次都给我相同的答案。我以 4 步增加分配给指针的内存,然后打印该值。

#include <stdio.h>

int main(void) {
    int n=0;
    char *name = "hello";
    scanf("%d",&n);

    for(int i =0; i<n;i++){
        name += sizeof(int);
        printf("%d \n", (sizeof(&name)));
    }
    return 0;
}

有人能帮我吗?我不知道这里有什么问题。我不需要不同的代码,我只想了解这有什么问题。

4

4 回答 4

3

请尝试以下操作,为清楚起见,省略了错误检查:

#include <stdio.h>
int main(void) 
{
    int n=0;
    char *name = null;
    scanf("%d",&n);

    for(int i=0; i<n;i++)
    {
        char *buffer = null;
        //allocate/reallocate the buffer. increases by 4 bytes every iteration
        buffer = (char*) realloc(name, (i+1)*4);
        name = buffer;
        printf("%d \n", (sizeof(&name)));
    }
    //release the memory used by the buffer
    free(name);
    return 0;
}
于 2013-06-25T15:30:37.407 回答
3

以下是对正在发生的事情的一些解释。

#include <stdio.h>

int main(void) {
    int n=0;

        // this does not actually allocate any memory. It sets the POINTER name to point (like an arrow) to a read-only block that contains "hello"
    char *name = "hello";
        // string literals generally come in fixed read-only memory

    scanf("%d",&n);

    for(int i =0; i<n;i++){
            // this causes the pointer memory address to be incremented by sizeof(int) (typically 4)
            // after the first increment if it will point to a string "o" (incremented by 4 characters)
            // after the second increment it will point to some undefined memory behind "hello" in your virtual address space and will have undefined behaviour when accessed
        name += sizeof(int);

            // sizeof(&name) will give you the size of a char **. Pointer to a character pointer. 
            // Wich is the same size as all pointers.
            // = sizeof(void *) = 8 for 64-bit systems, 4 for 32-bit systems
        printf("%d \n", (sizeof(&name)));
    }
    return 0;
}

这是这样做的方法:

#include <stdio.h>

int main(void) {
    int n=0;

    // allocate 10 bytes of memory and assign that memory address to name
    char *name = malloc(10);
    // the size of that memory needs to be kept in a separate variable
    size_t name_length = 10;

    // copy the desired contents into that memory
    memcpy(name, "hello", sizeof("hello"));

    scanf("%d",&n);

    for(int i =0; i<n;i++){

        // reallocate the memory into something with sizeof(int) more bytes
        void * tmp = realloc(name, name_length += sizeof(int));
        // this can fail
        if (tmp) {
            name = tmp;
        } else {
            perror("realloc");
            exit(-1);
        }

        printf("%d \n", name_length);
    }
    return 0;
}
于 2013-06-25T15:36:22.763 回答
0

在您提供的代码中,您根本没有为指针分配任何内存。如果要更改已分配块的大小,则必须处理动态内存。您必须首先使用malloc,然后使用realloc在每个步骤中分配更多内存。

于 2013-06-25T15:17:57.733 回答
0

让我们一一浏览您的代码:

char *name = "hello"; 

这将创建一个字符数组'h','e','l','l','o',0并将第一个字符的内存地址分配给 name

for(int i =0; i<n;i++){
    name += sizeof(int);
    printf("%d \n", (sizeof(&name)));
}

在这里,您将 int 的大小添加到 name 指针,这会使该指针每次传递增加 4。

因为这是一个 char 指针,所以指针增加了 4 个字节——因为 sizeof(int) == 4

您不能增加 hello char 数组的大小,因为它不是动态数组。

如果您希望能够调整字符串的大小,您应该 malloc 并将字符复制到更大的数组中。

于 2013-06-25T15:35:12.500 回答