'SizeOf' array in PureBasic?

Just starting out? Need help? Post your questions and find answers here.
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

blueznl wrote:So, all that is needed is a feature requst called: ArrayByteSize :-)
Or maybe ArrayMemorySize() ? 8)
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

netmaestro wrote:With the current commands it's as simple as x*y now, imho adding a command such as TotalArrayBytes or such is analagous to sending your remote control out to get hydraulic-assisted buttons installed. Just my 2 cents, no offense meant to anyone.
How would this be possible if there is no way to identify the size of the array's datatype at runtime?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

You created the array, you know its type or structure and how many dimensions it has.
BERESHEIT
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

netmaestro wrote:You created the array, you know its type or structure and how many dimensions it has.
To quote my first post:
Mistrel wrote:Is there a way to return the size of an array in bytes in PureBasic similar to how SizeOf works in C?
SizeOf in C returns the size of an array in bytes and does not need to know the size of the array's datatype. Your solution does not do this.

I'll just answer my own question: "No, you can't do that in PureBasic."

Thank you.
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

Mistrel wrote:
netmaestro wrote:You created the array, you know its type or structure and how many dimensions it has.
To quote my first post:
Mistrel wrote:Is there a way to return the size of an array in bytes in PureBasic similar to how SizeOf works in C?
SizeOf in C returns the size of an array in bytes and does not need to know the size of the array's datatype. Your solution does not do this.

I'll just answer my own question: "No, you can't do that in PureBasic."

IMHO your original question was based on a false premise.
regarding array declarations in C wrote:One important point about array declarations is that they don't permit the use of varying subscripts. The numbers given must be constant expressions which can be evaluated at compile time, not run time.
regarding C++ wrote: The sizeof operator cannot be used with the following operands:

* Functions. (However, sizeof can be applied to pointers to functions.)
* Bit fields.
* Undefined classes.
* The type void.
* Dynamically allocated arrays.
* External arrays.
* Incomplete types.
* Parenthesized names of incomplete types.

If you noticed, the answer in C is also "No, you can't do that in C.". It is likewise the same answer in C++.

You can only get the SizeOf an array structure or element, or a static array, just like in C. It just happens to be that all of C's arrays are static and declared by constants at compile time.

To create a dynamic array in C you have to allocate the memory yourself and assign it to a pointer, similar to what PureBasic does behind the scenes. Because this is done during runtime they can't return the SizeOf the array by a compile-time function. The several runtime methods above account for this in various ways. But the simple matter is, if it's determined at runtime then it's up to you because PureBasic doesn't provide a way to do it, and neither does C.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

I know that you can't use sizeof on a dynamic array in C. I specifically stated:
Mistrel wrote:Is there a way to return the size of an array in bytes in PureBasic similar to how SizeOf works in C?
Keywork "similar". I was referring specifically to the functionality of using sizeof on an array for returning its size in bytes without specifying a datatype, as I've tried to clarify.

Forget that I even mentioned C, considering it's lead to confusing instead of clarification of my request.

This thread is just getting worse with each resposne. I'm sorry for being unclear in my original post. There has already been too much over analyzing of a simple question.
User avatar
Demivec
Addict
Addict
Posts: 4282
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

Mistrel wrote:Forget that I even mentioned C, considering it's lead to confusing instead of clarification of my request.

This thread is just getting worse with each resposne. I'm sorry for being unclear in my original post. There has already been too much over analyzing of a simple question.
Noted. Good Day.
Post Reply