简体   繁体   中英

malloc in for loop

i have a question... lets say i have the following part of code :

int *a,*a1,*a2;
for (i=1; i<=2; i++) {
    a=malloc(sizeof(int));
    if (i==1) a1=a;
    else if (i==2) a2=a;
}
*a1=5;
*a2=4;

so my question is if i use printf to print a1 and a2 the variable a1 is gonna to have the value 5 and the a2 the value 4 ? so if i use malloc to allocate memory and a points in that memory space and use again malloc to allocate memory then a points to a different part of memory but the first one part of memory still exist ? or if i use malloc with a again it will erase the first part of memory and it will write a new part of memory

每次调用malloc返回一个指向不同内存的指针,直到您调用free释放该内存为止。

if i use printf to print a1 and a2 the variable a1 is gonna to have the value 5 and the a2 the value 4 ?

Erm... no.

a1 (being of type int * , ie pointer to integer) will contain the address resulting from the first call to malloc() .

a2 (and a ), being also of pointer type, will contain the address resulting from the second call to malloc() .

The address in a1 will point to an integer, to which you assigned the value 5.

The address in a2 (and a ) will point to an integer, to which you assigned the value 4.

Both of those integers will remain allocated until you release them by calling free() on their address. Be careful : After you called eg free( a2 ) , the integer is no longer allocated. Calling free() again on the same address (eg via free( a ) , or by calling free( a2 ) a second time) will result in undefined behaviour (ie, if you are lucky, your program will crash).

A pointer is an address value . It seems this is where your confusion comes from: It isn't identical to the thing it points to.

I believe the answer to your question is yes. Malloc'ing will give you a memory location with (at least) the specified size (if available). It does not free any memory, only gives you more (use free() to free malloc'ed memory).

The fact that you are using some temporary variable a has no significance. At the end of this loop a == a2 != a1

if i use printf to print a1 and a2 the variable a1 is gonna to have the value 5 and the a2 the value 4 ?

Yes, you are correct. It is a bit strange question, though, because it is easier to try and see than to ask.

so if i use malloc to allocate memory and a points in that memory space and use again malloc to allocate memory then a points to a different part of memory but the first one part of memory still exist ?

That is correct. malloc() allocates memory that "exists" until you free it using free() , or do a realloc() that could possibly move your data to a different memory and release the old memory location. I'd recommend you read some documentation about it. The manual page at least.

Also, your code has errors. You use assignment instead of comparison and variable i is not defined. Here is a corrected version with a printf() :

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

int main()
{
    int *a,*a1,*a2;
    int i;

    for (i=1; i<=2; i++) {
        a = malloc(sizeof(int));
        if (i==1)
            a1 = a;
        else if (i==2)
            a2 = a;
    }
    *a1=5;
    *a2=4;

    printf("a1=%d, a2=%d\n", *a1, *a2);

    free(a1);
    free(a2);

    return EXIT_SUCCESS;
}

Hope it helps. Good Luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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