0

Code:

cpdef values(int n):
    cdef size_t i
    cdef double * v = <double *> malloc(sizeof(double) * n)
    if v is NULL:
        abort()
    for i in range(n):
        print v[i]

Output:

>>> values(5)
1.06816855917e-306
0.0
0.0
0.0
0.0

Question:
Why does this function print zeros and where does the leading number come from / what does it mean? I thought unlike calloc, malloc does not initialize to zero, as said on wikipedia and in this thread. What is happening here behind the scenes of Python / Cython / C?

Community
  • 1
  • 1
embert
  • 7,336
  • 10
  • 49
  • 78

2 Answers2

1

For all intents and purposes the contents of malloc'ed memory block is to be treated as random and undetermined. The values found in the allocated buffer may be contents of memory blocks previously freed.

Dariusz
  • 21,561
  • 9
  • 74
  • 114
0

The memory allocated by malloc() by default contains garbage values.malloc() does not initialize memory to zero. 0(zero) can be one of the garbage value.

Chinna
  • 3,930
  • 4
  • 25
  • 55