2

So I'm aware that this question is most likely asked before, but after near an hour of searching I decided to ask all the same. Pointing me to a dublicate question which has already been answered would really be appreciated.

Then, programming in basic C, I'm curious to what happens to the array-elements when changing its pointer to pointing something else? Is it safe, without first freeing it? For instance,

int main()
{
    const int size = 3;
    int *p_arr = malloc(size * sizeof(int));

    for( int i=0; i<size; i++)
        p_arr[i] = i;

    int arr[size] = {0,0,0};

    p_arr = arr; // safe!?

    // What happens to the data previously allocated
    // and stored in *p_arr? Should one first call,
    // free(p_arr)
    // and then reallocate ..?
}

Essentially, changing the pointer will leave the data {0,1,2} in memory. Is this okay?

Thanks alot for any help!

Wololo
  • 1,249
  • 1
  • 13
  • 25
  • Note : VLA cann't have the initialization list. Also `p_arr = &arr;` --> `p_arr = arr;` but this cause memory leak. – BLUEPIXY Sep 29 '16 at 08:10
  • It's a memory leak. See: [What happens if I use malloc twice on the same pointer (C)?](http://stackoverflow.com/questions/19435433/what-happens-if-i-use-malloc-twice-on-the-same-pointer-c) – P.P Sep 29 '16 at 08:10
  • 1
    @BLUEPIXY .. Yes, I agree, I was a little too quick when creating the MWE, but this was beside the point. Nevertheless, I've corrected the typo, along with some other spelling errors. – Wololo Sep 29 '16 at 08:24

2 Answers2

3

Nothing happens to the data, except that it becomes unreachable ("leaked") and thus the memory is forever wasted, it can't be used for anything else until your program terminates (typically).

Don't do this, it's very bad practice to leak memory.

You should free() the memory when you no longer need it.

Also, the allocation can be written:

p_arr = malloc(size * sizeof *p_arr);

to remove the duplication of the int type, and lock the size to the actual variable. This is at least somewhat safer.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • "_and thus the memory is forever wasted_" this sounds so incredibly sad, what have you done to that poor memory. – Hatted Rooster Sep 29 '16 at 08:10
  • Thanks, this is exactly what the what I suspected and needed to get answered. Thanks! I will accept the answer when the time limit is reached. – Wololo Sep 29 '16 at 08:12
  • 1
    @GillBates That's kind of the point, it helps if you feel for your resources. :) – unwind Sep 29 '16 at 08:19
  • It is indeed bad practice, but bears no consequences for a simple program such as the OP's, where *forever* means very little time. – chqrlie Sep 29 '16 at 08:19
  • @chqrlie Indeed. However, this was just an example to emphesize my question. In reality, I'm writing a quite larger parallelized MPI-program which will run for an extended period of time ~1h on a cluster of 64 CPUs shared among about hundred people. So I think I should do this properly. I have mainly experience with Fortran, so this pointer-thing in C is rather new to me.. – Wololo Sep 29 '16 at 08:40
0

The array{0,1,2}is already in the memory,but you can't get it unless you point a pointer to the head address of the array again.

Fly
  • 1
  • 3