Opened 5 years ago

#1316 new defect

Invalid code generated for array access in loop

Reported by: jlahd Owned by:
Priority: normal Milestone:
Component: Compiler Version: 1.10
Keywords: Cc:

Description

The following function reverses a 2D fixnum array on its first dimension:

(defun reverse-path (pts)
  (destructuring-bind (n d)
      (array-dimensions pts)
    (let* ((out (make-array (list n d) :element-type 'fixnum)))
      (dotimes (i n)
	(dotimes (j d)
	  (setf (aref out i j) (aref pts (- n 1 i) j))))
      out)))

However, the result is not quite as expected:

* (reverse-path #2A((1 1) (5 4) (8 2)))
Fault during read of memory address #xB
   [Condition of type CCL::INVALID-MEMORY-ACCESS]

Adding a let gets rid of the fault, but gives a wrong result:

(defun reverse-path (pts)
  (destructuring-bind (n d)
      (array-dimensions pts)
    (let* ((out (make-array (list n d) :element-type 'fixnum)))
      (dotimes (i n)
	(dotimes (j d)
	  (let ((k (aref pts (- n 1 i) j)))
	    (setf (aref out i j) k))))
      out)))

* (reverse-path #2A((1 1) (5 4) (8 2)))
#2A((0 1) (2 3) (4 5))

Removing the :element-type 'fixnum specification from make-array makes the code work correctly:

(defun reverse-path (pts)
  (destructuring-bind (n d)
      (array-dimensions pts)
    (let* ((out (make-array (list n d))))
      (dotimes (i n)
	(dotimes (j d)
	  (setf (aref out i j) (aref pts (- n 1 i) j))))
      out)))

* (reverse-path #2A((1 1) (5 4) (8 2)))
#2A((8 2) (5 4) (1 1))

Change History (0)

Note: See TracTickets for help on using tickets.