简体   繁体   中英

pointer of a pointer and memory allocation

If I have int **array and want to place a series of numbers in it (I don't know its size), 5 3 4 0 or 9 1 5 8 3 0 as an example. As far as I know I should be using malloc

So I did something like this

int **array;
int n = 1, inp = 0;
while(n){ // scan till the input is 0
    scanf("%d", &n);
    array = (int**)malloc(sizeof(int*)*(inp+1)); //since inp start at 0
    array[inp] = &n; //is this even correct?
    inp++;
}

My first question is: Will this method (the loop) upgrade/expand the size of the array or is what I am doing a waste of memory?

The second question is how can I print/edit the values of this array ?

EDIT:

From your answers I have came up with the following.

int **array;
int n = 1, inp = 0;
array = (int**)malloc(sizeof(int*));
while(n){
    scanf("%d", &n);
    realloc( array, sizeof((int*)(inp+1)));
    array[inp] = n;
    inp++;
}

Is this the correct way to do it?

Note* I am aware that it does not have to be a pointer of a pointer, but I need it to be for something else later on.

You code is wrong for at least these reasons.

1) You keep doing malloc to array and thereby loose previously malloced blocks. The function to use is realloc when extending the size of dynamic memory.

2) You store the address of n instead of the value of n

Besides that it seems strange to use a double pointer. Why not do like:

int *array = NULL;
int n = 1, inp = 0;
while(n){ // scan till the input is 0
    scanf("%d", &n);
    array = realloc(array, sizeof(int)*(inp+1));
    array[inp] = n;
    inp++;
}

EDIT after OPs update

If you really want to use a double pointer (ie int **array; ), you need to allocate memory in two levels.

That could look like:

int **array = malloc(sizeof *array);
*array = NULL;
int n = 1, inp = 0;
while(n){ // scan till the input is 0
    scanf("%d", &n);
    *array = realloc(*array, sizeof(int)*(inp+1));
    (*array)[inp] = n;
    inp++;
}

What you're doing in your code is allocating progressively larger areas of memory and saving the input value in the last position of each new area, while losing the pointers to the previously allocated areas. A commom and efficient solution for what you want (which is used in C++'s vectors, I believe) is to allocate some minimum amount of space, then check at each iteration if you are on the verge of exceeding it. In case you are, reallocate the area doubling the space. Something like this:

int i = 0; //iterator
int s = 1; //array size
int n;     //input (use do-while so you don't have to worry about initial value)

//it doesn't have to be bidimensional for a single series
int * array = (int *) malloc(sizeof(int) * s);

do
{
    if(i == s)
    {
        s *= 2;
        array = (int *) realloc(array, sizeof(int) * s);
    }
    scanf("%d", &n);
    array[i++] = n; //assign with the value, not with the address
}
while(n)

UPDATE: if you really need to use **int, do it like this:

int n, i = 0, s = 1;

int ** array = (int **) malloc(sizeof(int*) * s);

do
{
    if(i == s)
    {
        s *= 2;
        array = (int **) realloc(array, sizeof(int *) * s);
    }
    scanf("%d", &n);
    array[i] = (int *) malloc(sizeof(int));
    array[i][0] = n;
    ++i;
}
while(n)

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