-1

While reading some tutorials on the net about the C language, I found the following code that allocates memory for an array of length (n1-n0+1). I can't figure out how and why adding this instruction: v -= n0; works. Here is the code:

double *AllocVector(int n0, int n1)
{
    double *v;
    int i;

    v = malloc((n1-n0+1) * sizeof(double));
    if (!v)
        printf("Error allocating memory in AllocVector");

    /* Initiate */

    v -= n0;
    for (i = n0; i <= n1; i++)
        v[i] = 0.0;

    return v;
}
Sam Protsenko
  • 14,045
  • 4
  • 59
  • 75
  • 1
    If it works it's because you got lucky and didn't corrupt anything important by moving `v`. Were you trying to cause a crash? – Andy Brown Nov 24 '15 at 11:07
  • 1
    What do you mean by still can work. Sure there is no guarantees that you code will crash but it doesn't imply that it is sounds. You may be in real trouble later in your execution. – mathk Nov 24 '15 at 11:07
  • It's technically undefined behavior, but likely to work on many compilers. – interjay Nov 24 '15 at 11:09
  • can you point us to the url location of that tutorial? – milevyo Nov 24 '15 at 11:19
  • http://stackoverflow.com/questions/2235457/how-to-explain-undefined-behavior-to-know-it-all-newbies – Lundin Nov 24 '15 at 13:31
  • Thank you guys for the answers. The code has never crashed on my machine, but i Think i m just lucky so far. To be safe, I have deleted this instraction v -= n0; and changed the loop as follows for (i = 0; i <=( n1-n0); i++) v[i] = 0.0; – Mohamed Sellahi Nov 24 '15 at 17:50

3 Answers3

0

You're invoking undefined behaviour. At this point, all bets are off and anything can happen.

I just so happens that appearing to work correctly is one of the things that can happen.

tangrs
  • 9,709
  • 1
  • 38
  • 53
0

The code v -= n0; causes undefined behaviour, because it is not permitted to have a pointer point anywhere outside of allocated memory (except for a null pointer).

On some systems it might appear to work; the assembly instructions that the compiler issues for v - n0 + n0 might generate v, and so on.

Community
  • 1
  • 1
M.M
  • 138,810
  • 21
  • 208
  • 365
0

Here's why it's undefined behavior: the variable v is declared locally, which allocates space but does not initialize it. v has some random value in it. In other words, you can't know what's stored in v. Since it's a pointer to a double, you don't know where it points.

When you write v -= n0; you are saying move 'backward' from the address pointed to by v for n0 # of doubles and store that new address in v. Since you didn't know where v was pointing you can't know where v now points.

Then you dereference v by writing v[i] = 0.0; and since you didn't know where v points you don't know where you are writing the value 0.0.

You're writing 0.0 somewhere and if that somewhere is a memory location you have permission to access and you aren't using it for anything else, the program seems to work properly. Then you move it to a new machine or you modify the code and poof v is now pointing to a new memory location and the program crashes.

nicomp
  • 4,344
  • 4
  • 27
  • 60