deep-copy icon indicating copy to clipboard operation
deep-copy copied to clipboard

Tool doesn't handle fixed size arrays at all

Open jtrac3er opened this issue 2 years ago • 2 comments

When you try to deep-copy the follwing code

package main

type s3 struct {
	Y int
	Z int
}
type s2 struct {
	X      int
	String *byte
	Ps3    *s3
}
type s1 struct {
	ArrayOfS2        [10]s2
	ArrayOfPointerS2 [20]*s2
	PInt             *int
}

then the arrays are simply ommitted...

// generated by /home/ubuntu/go/bin/deep-copy --type s1 build/golang_converted.go; DO NOT EDIT.

package main

// DeepCopy generates a deep copy of s1
func (o s1) DeepCopy() s1 {
	var cp s1 = o
	if o.PInt != nil {
		cp.PInt = new(int)
		*cp.PInt = *o.PInt
	}
	return cp
}

exact command used:

~/go/bin/deep-copy --type s1 arrayStruct.h

jtrac3er avatar Jul 07 '23 21:07 jtrac3er

From my understanding fixed size arrays are always copied by value. See https://go.dev/play/p/yapKWLrehlPv So var cp s1 = o is enough.

Ok probably ArrayOfPointerS2 needs to be handled.

Nocccer avatar Sep 23 '25 18:09 Nocccer

Indeed, the arrays being omitted should be fine. What should not be is omitting arrays of pointers. The pointers themselves would need to be deep copied as well.

urandom avatar Nov 25 '25 13:11 urandom