0

I am seeing some really weird behavior in my C project - https://github.com/ryu577/base/blob/master/numerical/c/NumericalRecipiesCode/src/ch05/tst5.c

On line 77, I run one of the routines on "testFn" which is on line 13 and I have also included below. I do this by passing a pointer to the function.

float testFn(float x)
{
    return x*x;
}

Next, on line 87, I simply attempt to initialize a 1d array of floats based on a boilerplate routine from Numerical recipes in C:

float *c1; c1 = vector(0, n1);

The "vector" function is quite simple and included here:

float *vector(long nl, long nh)
/* allocate a float vector with subscript range v[nl..nh] */
{
    float *v;
    v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
    if (!v) nrerror("allocation failure in vector()");
    return v-nl+NR_END;
}

Now, when I print the entries in my "vector" (float array - c1), this is what I get -

12.00   12.00   12.00   0.00    0.00

Somehow, it decides to put 12s in the first 3 positions. But what is really interesting is that these numbers depend on how I define testFn. For example, if I define it as:

float testFn(float x)
{
    return x*x*x;
}

I then get the entries of c1:

112.00  110.04  109.04  108.53  0.00

But the thing is that the creation and initialization of the vector had nothing to do with testFn at all. So, how is it able to influence its values in this way? Is it related to some block of memory that isn't being freed up and the values are spilling over into the new array?

3
  • 1
    It doesn't look like you initialised a vector - you just allocated it. Its contents are undefined until you store something there. Trying to read these values prior to initialising them is UB. Commented May 4, 2016 at 7:58
  • Also, please take note of comments and suggestions on your previous questions - especially the advice that you typically need to provide a minimal reproducible example if you want people to help you. Commented May 4, 2016 at 8:00
  • @PaulR - thanks. I found it very hard to provide a minimal example here since any part of the code could have been causing this behavior. I understand that I haven't initialized the vector now, but still curious as to why testFn is able to influence the default values. Commented May 4, 2016 at 8:03

1 Answer 1

3

malloc() will not initialize the contents of the array. This means in this block could be anything - floats, ints or complete garbage, in your case it's some numerical value thats left from a previous operation. This will also happen when declaring an array inside of a function.

You usually don't want to waste compute time on clearing every single allocated block, because you'll be overwriting them anyway when using them. To get the desired behavior use a loop to initialize the array after allocating it.

Finally, note that your program may invoke undefined behaviour by reading uninitialized memory as float. See question read before write is undefined with malloced memory? for a detailed explanation.

Sign up to request clarification or add additional context in comments.

9 Comments

Okay, good to know this is expected behavior. I'm used to C# and Java where these kinds of things never happen.
You could also use calloc()instead, i recommend reading the man pages for malloc and calloc first though.
calloc() just zeros out the memory which might work for integers or but not necessarily for floating point variables.
@alk: It may work for floating point, but it is not guaranteed by the C standard. I think it will (happen to) work on most common platforms, though.
@MaximilianSchlosser: nitpick mode on According to the C standard, all bits zero is indeed 0 for int and unsigned int. There is no such guarantee for float. IEEE 754 does give that guarantee; however, the C standard does not say a float must be in IEEE 754 format (though many compilers use it).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.