www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - arrays and .sizeof

reply Trass3r <un known.com> writes:
I've encountered this problem several times now and almost always it takes  
hours or days to reduce crashes down to this.
The sizeof property works as expected for static arrays, but as soon as  
you change the array to a dynamic one (e.g. cause it became too big to  
stay on the stack)
.sizeof doesn't return .length * elem.sizeof anymore but only the size of  
the struct consisting of .ptr and .length.

This is really error-prone and I need the actual size of an array while  
interfacing with C a lot.
Any way to avoid this? Maybe a dmd warning if possible?

Please don't "just use .length * elem.sizeof" me. It's cumbersome and a  
bit error-prone as well. If you hard-code the element type and the type  
changes later -> bang.
I know I could use a function but this doesn't change the problem that  
.sizeof works perfectly for static arrays and silently crashes when  
changed to a dynamic one.
Aug 09 2011
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
Trass3r:

 Any way to avoid this? Maybe a dmd warning if possible?
 
 Please don't "just use .length * elem.sizeof" me. It's cumbersome and a  
 bit error-prone as well. If you hard-code the element type and the type  
 changes later -> bang.
 I know I could use a function but this doesn't change the problem that  
 .sizeof works perfectly for static arrays and silently crashes when  
 changed to a dynamic one.
What if the array is an associative one? Or a user-defined struct that represents a 2D matrix, that contains a pointer to heap memory? sizeof is not what you think it is. It doesn't return the whole memory used by a data structure, just its size on the stack. If you want to know the whole memory used by a data structure that contains references you need a function written by yourself (one idea is to add a .memory attribute to every built-in type that returns the whole memory used by it, but it's hard to do because data structures can share part of their memory, they don't need to always own memory). Bye, bearophile
Aug 09 2011
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 2011-08-09 14:38, Trass3r wrote:
 I've encountered this problem several times now and almost always it
 takes hours or days to reduce crashes down to this.
 The sizeof property works as expected for static arrays, but as soon as
 you change the array to a dynamic one (e.g. cause it became too big to
 stay on the stack)
 .sizeof doesn't return .length * elem.sizeof anymore but only the size
 of the struct consisting of .ptr and .length.

 This is really error-prone and I need the actual size of an array while
 interfacing with C a lot.
 Any way to avoid this? Maybe a dmd warning if possible?

 Please don't "just use .length * elem.sizeof" me. It's cumbersome and a
 bit error-prone as well. If you hard-code the element type and the type
 changes later -> bang.
 I know I could use a function but this doesn't change the problem that
 .sizeof works perfectly for static arrays and silently crashes when
 changed to a dynamic one.
You can implement your own function that behaves differently for static and dynamic arrays, call it "sizeOf" or something like that and it will almost look built-in. -- /Jacob Carlborg
Aug 09 2011
prev sibling next sibling parent Pelle <pelle.mansson gmail.com> writes:
On Tue, 09 Aug 2011 14:38:21 +0200, Trass3r <un known.com> wrote:

 I've encountered this problem several times now and almost always it  
 takes hours or days to reduce crashes down to this.
 The sizeof property works as expected for static arrays, but as soon as  
 you change the array to a dynamic one (e.g. cause it became too big to  
 stay on the stack)
 .sizeof doesn't return .length * elem.sizeof anymore but only the size  
 of the struct consisting of .ptr and .length.

 This is really error-prone and I need the actual size of an array while  
 interfacing with C a lot.
 Any way to avoid this? Maybe a dmd warning if possible?

 Please don't "just use .length * elem.sizeof" me. It's cumbersome and a  
 bit error-prone as well. If you hard-code the element type and the type  
 changes later -> bang.
 I know I could use a function but this doesn't change the problem that  
 .sizeof works perfectly for static arrays and silently crashes when  
 changed to a dynamic one.
size_t memSize(T)(T[] ts) { return t.length * T.sizeof; } And then never use .sizeof for that purpose again.
Aug 09 2011
prev sibling parent "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
On Tue, 09 Aug 2011 14:38:21 +0200, Trass3r wrote:

 I've encountered this problem several times now and almost always it
 takes hours or days to reduce crashes down to this. The sizeof property
 works as expected for static arrays, but as soon as you change the array
 to a dynamic one (e.g. cause it became too big to stay on the stack)
 .sizeof doesn't return .length * elem.sizeof anymore but only the size
 of the struct consisting of .ptr and .length.
 
 This is really error-prone and I need the actual size of an array while
 interfacing with C a lot.
 Any way to avoid this? Maybe a dmd warning if possible?
 
 Please don't "just use .length * elem.sizeof" me. It's cumbersome and a
 bit error-prone as well. If you hard-code the element type and the type
 changes later -> bang.
 I know I could use a function but this doesn't change the problem that
 .sizeof works perfectly for static arrays and silently crashes when
 changed to a dynamic one.
I would say that you are using .sizeof wrongly. You should not use it to get the size of an array, be it static or not. You should *only* use x.sizeof when you want to know the size of the variable x, not of whatever x points to. -Lars
Aug 11 2011