简体   繁体   English

C malloc指针多次

[英]C malloc a pointer multiple times

If I have a pointer: char ** tmp = (char **)malloc(sizeof(char *) * MAX_SIZE) , after assigning values to each block, I have a new pointer char ** ptr = tmp . 如果我有一个指针: char ** tmp = (char **)malloc(sizeof(char *) * MAX_SIZE) ,在为每个块赋值后,我有一个新指针char ** ptr = tmp

1). 1)。 Can I tmp = (char **)malloc(sizeof(char *) * MAX_SIZE) malloc it again without free it? 我可以tmp = (char **)malloc(sizeof(char *) * MAX_SIZE) malloc再次没有free吗?

2). 2)。 Does the ptr still has the values and also tmp points to a new block of memory? ptr是否仍然具有值,并且tmp指向新的内存块?

I have a function to free all used memory at the end, so don't worry about free . 我有一个功能来释放所有用过的内存,所以不要担心free

Assigning tmp to ptr keeps a reference to the malloced memory area. tmp分配给ptr会保留对malloced内存区域的引用。 So re-assigning tmp using a new call to malloc is not a problem. 因此,使用对malloc的新调用重新分配tmp不是问题。 This will not loose reference to the malloced memory as ptr is an existing alias. 这不会松散对malloced内存的引用,因为ptr是一个现有的别名。

So 所以

  1. yes, you can do another malloc. 是的,你可以做另一个malloc。 (You could do anyway, but would loose reference to malloced memory) (无论如何你可以做,但会松开对malloced内存的引用)

  2. Yes, ptr still references the malloced area 是的, ptr仍然引用了malloced区域

BTW, doing a free at the end could be rather pointless if this would refer to at the end of the program . 顺便说一句,如果在程序结束时提到这一点,那么在最后做一个自由可能是毫无意义 So, I assume you mean, at the end of the current algorithm . 所以,我认为你的意思是, 在当前算法的最后

Anyway, you need to keep references to the allocated memory. 无论如何,您需要保持对已分配内存的引用。 Usually it is advisable to release such memory as soon as it is no longer used. 通常建议在不再使用时立即释放此类内存。

1). 1)。 Can I tmp = (char **)malloc(sizeof(char *) * MAX_SIZE) malloc it again without free it? 我可以tmp = (char **)malloc(sizeof(char *) * MAX_SIZE) malloc再次没有免费吗?

Yes , you can again allocate memory again . 是的,您可以再次分配内存。 But tmp will now point to a new allocated memory and to previously allocated memory . 但是tmp现在将指向新分配的内存和先前分配的内存。

2). 2)。 Does the ptr still has the values and also tmp points to a new block of memory? ptr是否仍然具有值,并且tmp指向新的内存块?

Now tmp will point to newly allocated memory but ptr refers to the previous memory location which was allocated . 现在tmp将指向新分配的内存,但ptr指的是已分配的先前内存位置。

So in this way you don't loose reference to any of the memory block and can be freed . 因此,通过这种方式,您不会松开对任何内存块的引用,并且可以释放。

malloc is used to allocate a memory block. malloc用于分配内存块。 It allocates a block of memory of provided size and return a pointer to the beginning of the block. 它分配一个提供大小的内存块,并返回指向块开头的指针。

So the first time you write 所以你第一次写

char ** tmp = (char **)malloc(sizeof(char *) * MAX_SIZE)

it allocates memory and return a pointer pointing to beginning of memory location to temp. 它分配内存并将指向内存位置开头的指针返回到temp。 Now when you assign tmp to ptr, ptr now points to the allocated memory along with tmp. 现在,当您将tmp分配给ptr时,ptr现在指向已分配的内存以及tmp。 Now again if you write tmp = (char **)malloc(sizeof(char *) * MAX_SIZE) it will allocate a new memory and return a pointer to which tmp will be pointing to. 现在再次如果你编写tmp = (char **)malloc(sizeof(char *) * MAX_SIZE) ,它将分配一个新的内存并返回一个指向tmp的指针。 But ptr still continues to point to the previously allocated memory. 但是ptr仍然继续指向先前分配的内存。 So the answer to both of your question is YES. 所以你的两个问题的答案都是肯定的。

I hope I was able to explain the things correctly. 我希望我能够正确地解释这些事情。

int main(int argc, char **argv) {

    tmp = (char**)malloc(sizeof(char*) * argc);
    while (argv[i])
    {
        tmp[i] = strdup(argv[i]);
        i++;
    }
    if (argc > 3)
       printf("%s", argv[2])
    return(0)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM