开发者

Passing a struct array into a function and performing sizeof operation upon it

I suppose much like standard arrays (i.e integer arrays), when you pass an array of structs you must pass the size of the array with it. However what I do not yet understand is that when you take sizeof a structure first element you will get 4 (meaning 4 bytes in the first element?).

Now I pass a array of structs which contains only strings. I inspect the size of a single array element (remember it will be one struct) I get something like 28, but after I've passed it into a function (and yes by passed into the function I mean passed by value the address of the first element in my array), I only get 4.

Now I'm guessing the sizeof is getting the first element of my struct in the array.开发者_如何学Go So I have my array myArray of type myStruct:

myStruct { String name String address String postcode }

I presume sizeof is looking at "name"? But I know for a fact that name isn't 4 bytes long - it's 10.

What exactly is the sizeof looking at? What does the memory structure look like of an array of structs?

Thanks Thomas


The 4 will be the size of the pointer you have passed rather than the size of the struct to which it refers.


sizeof() works with types, not objects. sizeof(some_object) is implicitly taken as — using pseudo notation — "sizeof(typeof(some_object))".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜