当前位置: 动力学知识库 > 问答 > 编程问答 >

Dynamic memory location C - argument int *

问题描述:

I've got a piece of code:

void Read(int T[], int len) {

int i;

T = (int*) malloc(len * sizeof *T);

for (i=0;i<len;i++) {

scanf("%d", &T[i]);

}

}

I use in this way:

int *T;

Read(T,len);

Next, I wanna write my table:

void Write(int T[], int len) {

int i;

for(i=0;i<len;i++) {

printf("%d, ", T[i]);

}

printf("\n");

return;

}

And use it:

Write(T,len);

It gives me wrong results. I almost sure that problem is connected with '&' but I cannot deal with it.

Thanks in advance

网友答案:

One problem may be that you're modifying a local variable T inside your Read function:

int *T;
Read(T, len);
// ...

void Read(int T[], int len) {
    int i;
    T = (int*) malloc(len * sizeof *T);
    // ...
}

The T inside Read is a copy of the real T variable. You assign a new value to the copy, but the original remains unchanged.

To actually modify the outer T, pass a pointer to it like so:

int *T;
Read(&T, len);
// ...

void Read(int **T, int len) {
    int i;
    *T = (int*) malloc(len * sizeof *T);
    for (i=0;i<len;i++) {
        scanf("%d", &(*T)[i]);
    }
}
网友答案:

Try the following (function Write is unchanged):

void Read(int T[], int len) {
    int i;
    for (i=0;i<len;i++) {
        scanf("%d", &T[i]);
    }
}

T = (int*) malloc(len * sizeof *T);
Read(T,len);

Write(T,len);   

UPDATE: But if you want Read to allocate memory, it will have to receive a pointer to the pointer:

void Read(int *T[], int len) {
    int i;
    T = (int**) malloc(len * sizeof **T)

    for (i=0;i<len;i++) {
        scanf("%d", &(*T)[i]); /* not the easiest expression in the language */
    }        
}

Read(&T, len);
Write(T, len);

Actually, it would be easier to modify Read to return the pointer directly, but maybe that is not what you want: int *Read(int len) { ... }.

网友答案:

Nate's solution is all good and dandy yet I find it slightly inelegant.

C itself is fairly obfuscated as it is and I don't see a reason why to make it so twice as much. I suggest you use the following:

Change the signature of your void Read(int T[], int len) to int* Read(int T[], int len).

Doing this comes with multiple advantages. Not only does it make your code more readable, but it also removes the "hidden" side effect (partially) that the nasty read comes with and also clearly states what your intent is, so you will be less likely to NOT forget to call free() simply because you see that a pointer is returned so it's a hint that there may have been a sneaky dynamic allocation in the function. Also you have one less level of indirection to worry about.

Then you can do the following:

int* Read(int T[], int len) {
    int i;
    T = (int*) malloc(len * sizeof(T));
    for (i=0; i<len; ++i) {
        scanf("%d", &T[i]);
    }
    return T; // Basically the only change!
}

Now, in the function that calls your Read():

int main() {

const int size = 5;
int *T = 0; // It's not needed to do a prior declaration and initialization
            // but it does silence a warning which is a good thing.
T = Read(T, size);
Write(T, size);

// Do more stuff and free() whenever needed

return 0;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: