• Print

Author Topic: c style array initialization  (Read 208 times)

bobtheunplayer

  • Jr. Member
  • **
  • Posts: 70
  • I'd rather be coding.
    • Email
c style array initialization
« on: April 06, 2013, 07:41:18 AM »
Hey all,

I'm digging deep for this one, and haven't found anything.

TerryRitchie, I read your tutorial, and I think it's comprhensive and neat that real member names are used in the examples.
And, then I read the qb64 built in help on arrays.
Lastly, I read the array wiki.

After all that, I'm still on my quest for c style array initialization information in qb64.
Does it even exist?

I don't mind using read / data statments to load arrays with fixed information, but for really small items, somthing like this feels more appropriate:
Code: [Select]
DIM array(2,2) = { {0,1}, {-1,0} }

Thanks,
~bob

sjt1975

  • Newbie
  • *
  • Posts: 46
    • My Personal Web-Site
    • Email
Re: c style array initialization
« Reply #1 on: April 06, 2013, 09:52:16 AM »
Hi Bob,

Since the aim of QB64 is to be as QB45-compatible as possible, this would mean not implementing C-style functionality that is not a part of the 'allowed' QB45 syntax.  QB45 isn't C, so you might want to look in to C/C++/Visual C/Visual C++, if you want that kind of functionality.  A similar request for a C-style ability to assign a value to a variable at the point of declaration was also made earlier - your request is the same thing, but applied to arrays.

So, no, it doesn't exist; nor should it, in QB64 (in my opinion), otherwise that would break QB45 compatibility.

Stuart.

bobtheunplayer

  • Jr. Member
  • **
  • Posts: 70
  • I'd rather be coding.
    • Email
Re: c style array initialization
« Reply #2 on: April 06, 2013, 10:19:20 AM »
Stuart,

That makes sense.  I guess I was hoping for a _QB64_KEYWORD or something that looked more elegant than using a bunch of assignment statements or read / data with do loops to load fixed information into arrays.

It's not the end of the world, but thought it was at least worth asking about.

~bob

Galleon

  • Administrator
  • Hero Member
  • *****
  • Posts: 4664
  • QB Forever
    • Email
Re: c style array initialization
« Reply #3 on: April 06, 2013, 02:43:43 PM »
This is the most efficient method at present:
Code: [Select]
DIM a(10) AS LONG

'use the MEM system to fill the array
DIM m AS _MEM
m = _MEM(a())
_MEMFILL m, m.OFFSET, 11 * 4, 1000000 AS LONG
_MEMFREE m

'test it
FOR i = 0 TO 10
    PRINT a(i)
NEXT
I'm not saying use it, but it's there if you need it.

If you want to set the individual elements differently, you may need to do that manually.
Something old... Something new... Something borrowed... Something blue...

mcalkins

  • Hero Member
  • *****
  • Posts: 1269
    • qbasicmichael.com
    • Email
Re: c style array initialization
« Reply #4 on: April 06, 2013, 06:46:20 PM »
Someone, I don't remember exactly who, made a preprocessor to allow initialization syntax. I think that it was SMcNeill, but it might have been someone else.

The problem with assigning to the variable at runtime, even with _MEM, is that it involves a copy. Whereas, initialized static data would be loaded by the operating system at loadtime, and would not need a copy. However, non-static data, such as function local variables, would need a copy, even if an initializer is used.

If there were something like _MEMDATA, something that would give _MEM access to the DATA statements, that might be interesting. However, I prefer C++ style initializers.

Quote from: sjt1975 on April 06, 2013, 09:52:16 AM
Hi Bob,

Since the aim of QB64 is to be as QB45-compatible as possible, this would mean not implementing C-style functionality that is not a part of the 'allowed' QB45 syntax.  QB45 isn't C, so you might want to look in to C/C++/Visual C/Visual C++, if you want that kind of functionality.  A similar request for a C-style ability to assign a value to a variable at the point of declaration was also made earlier - your request is the same thing, but applied to arrays.

So, no, it doesn't exist; nor should it, in QB64 (in my opinion), otherwise that would break QB45 compatibility.

Stuart.

It would not break QBASIC backward compatibility. Adding new things doesn't break backward compatibility. Changing existing things is what breaks backward compatibility.

QB64 already adds many things that QBASIC/QB did not support.

Regards,
Michael
The QBASIC Forum Community: http://www.network54.com/index/10167 Includes off-topic subforums.
QB64 Off-topic subforum: http://qb64offtopic.freeforums.org/

DSMan195276

  • Hero Member
  • *****
  • Posts: 1978
  • Yes
    • Email
Re: c style array initialization
« Reply #5 on: April 06, 2013, 07:36:21 PM »
Quote
The problem with assigning to the variable at runtime, even with _MEM, is that it involves a copy. Whereas, initialized static data would be loaded by the operating system at loadtime, and would not need a copy. However, non-static data, such as function local variables, would need a copy, even if an initializer is used.

I realize what you're getting at, however I don't actually think that makes much of a difference for QB64 because all of the variables are allocated at runtime anyway. I can't say I see the reasons for that choice, but it is how it is (Compile a program and then take a lot inside of ./internal/temp/maindata.txt). All variables are still set to zero after being allocated though, so in that case it would be useful to be able to set their value at that point instead of having to set them again later. I think that in the end it wouldn't have much of an effect on runtime performance as far as I can see. In light of that, I still think it would be useful since it can allow for much more readable code in the end.

_MEMDATA would definitely be interesting, I'm not really sure how to go about that though, DATA is quirky as it is.

Matt
"Cast your cares on the Lord and he will sustain you; he will never let the righteous be shaken" -- Psalm 55:22
QB64 Linux Installer

mcalkins

  • Hero Member
  • *****
  • Posts: 1269
    • qbasicmichael.com
    • Email
Re: c style array initialization
« Reply #6 on: April 06, 2013, 08:05:20 PM »
Quote
however I don't actually think that makes much of a difference for QB64 because all of the variables are allocated at runtime anyway. I can't say I see the reasons for that choice, but it is how it is (Compile a program and then take a lot inside of ./internal/temp/maindata.txt).

Yes. I think that it is to support VARSEG/DEF SEG. In my opinion, this allocation mechanism is one of the primary things keeping QB64 from being as efficient as it could be. I understand that it is good to support VARSEG/DEF SEG, but most programs don't use it.

Quote
All variables are still set to zero after being allocated though, so in that case it would be useful to be able to set their value at that point instead of having to set them again later.

True. That way, you'll get 1 assignment instead of 2.

In that case, some sort of _UNINITIALIZED keyword might be useful. However, this would be dangerous, and would need to be used with care.

I haven't actually looked into how DATA works in QB64. I generally avoided it in QBASIC, and I continue to avoid it in QB64. All I know is that it involves linking an object file into the program.



As it is, you can use DECLARE LIBRARY:

Code: [Select]
int32 test_array[2][2] = { {0,1}, {-1,0} };

uptrszint getTestArray(void) {return (uptrszint) test_array;}

Code: [Select]
DECLARE LIBRARY "delme"
 FUNCTION getTestArray~%& ()
END DECLARE

DIM m AS _MEM: m = _MEM(getTestArray, 2 * 2 * 4)
DIM i AS LONG

FOR i = 0 TO 3
 PRINT _MEMGET(m, m.OFFSET + i * 4, LONG)
NEXT
_MEMFREE m
END



To bobtheunplayer:

Please keep in mind that, while in C/C++, you specify the number of elements, in BASIC, you specify the upperbound:

int array[2][2];     // 4 elements
DIM array(2, 2) AS LONG     ' 9 elements

This is a good reason for explicitly specifying "0 TO ", to remind yourself that it is the upperbound:

DIM array(0 TO 2, 0 TO 2) AS LONG

Also, C/C++ multidimensional arrays are "row major", whereas BASIC multidimensional arrays are "column major".

Regards,
Michael
The QBASIC Forum Community: http://www.network54.com/index/10167 Includes off-topic subforums.
QB64 Off-topic subforum: http://qb64offtopic.freeforums.org/

DSMan195276

  • Hero Member
  • *****
  • Posts: 1978
  • Yes
    • Email
Re: c style array initialization
« Reply #7 on: April 06, 2013, 09:05:49 PM »
I think variables are allocated on runtime to allow for a simpler variable system over all (Because of all the various things that QBasic supports in the way of variable scope, and etc.). I would agree that having to allocate memory for every variables does slow things down though, I imagine if things were to be improved the variable system would be one thing to work on. But it does work and is compatible with QBasic, so that is worth keeping in mind, that's not exactly a small feat.

As for _UNINITALIZED, I think the danger of that command may not be worth it honestly, mostly because if the variable system changes then the command will probably not be relevant anyway. And if assignments with DIM were added then it would also be useless.

Matt
"Cast your cares on the Lord and he will sustain you; he will never let the righteous be shaken" -- Psalm 55:22
QB64 Linux Installer

sjt1975

  • Newbie
  • *
  • Posts: 46
    • My Personal Web-Site
    • Email
Re: c style array initialization
« Reply #8 on: April 07, 2013, 01:05:27 AM »
Quote from: mcalkins on April 06, 2013, 06:46:20 PM
Quote from: sjt1975 on April 06, 2013, 09:52:16 AM
Hi Bob,

Since the aim of QB64 is to be as QB45-compatible as possible, this would mean not implementing C-style functionality that is not a part of the 'allowed' QB45 syntax.  QB45 isn't C, so you might want to look in to C/C++/Visual C/Visual C++, if you want that kind of functionality.  A similar request for a C-style ability to assign a value to a variable at the point of declaration was also made earlier - your request is the same thing, but applied to arrays.

So, no, it doesn't exist; nor should it, in QB64 (in my opinion), otherwise that would break QB45 compatibility.

Stuart.

It would not break QBASIC backward compatibility. Adding new things doesn't break backward compatibility. Changing existing things is what breaks backward compatibility.

It would break backwards-compatibility, in the strict sense, since if such code was compiled with QB45 itself, it would throw it out as an error, since it is an un-supported extension to that language.  True backwards-compatibility requires that the code should be able to be compiled and work as intended on both the original compiler and the 'newer' compiler which is emulating the original compiler.

However, this true backwards-compatibility would therefore not allow for any new functionality in the more modern 'emulating' compiler (i.e. that is beyond the scope of the original compiler), since such newer functionality would not work with the original compiler.

I am not against adding new functionality in QB64, but such new functionality would not work with the QB45 compiler, so developers may need to be careful (if they wish to backwards-compile).  That is why some people on this discussion-board are more 'strict' with respect to what functionality is implemented in QB64 and the manner in which it is implemented to work.
« Last Edit: April 07, 2013, 01:22:07 AM by sjt1975 »

mcalkins

  • Hero Member
  • *****
  • Posts: 1269
    • qbasicmichael.com
    • Email
Re: c style array initialization
« Reply #9 on: April 07, 2013, 01:51:23 AM »
Quote
It would break backwards-compatibility, in the strict sense, since if such code was compiled with QB45 itself, it would throw it out as an error, since it is an un-supported extension to that language.  True backwards-compatibility requires that the code should be able to be compiled and work as intended on both the original compiler and the 'newer' compiler which is emulating the original compiler.

No.
http://en.wikipedia.org/wiki/Backward_compatibility

QuickBASIC 4.5 had to be purchased, so it is much less relevant.  QBASIC came with DOS, Windows, and OS/2, so almost everyone has it.

Anyone who cares about running code in both QBASIC and QB64 can constrain himself to the common subset.

It wouldn't be out of the question to port a significant subset of QB64 to 32 bit DOS. However, I'm not sure why anyone would want to, as DOS is quite limited. To be practical, you'd probably have to port portions of the Windows API as well. It would probably be better just to write your own Windows-like operating system, and forget DOS.

Regards,
Michael
The QBASIC Forum Community: http://www.network54.com/index/10167 Includes off-topic subforums.
QB64 Off-topic subforum: http://qb64offtopic.freeforums.org/

sjt1975

  • Newbie
  • *
  • Posts: 46
    • My Personal Web-Site
    • Email
Re: c style array initialization
« Reply #10 on: April 07, 2013, 03:00:34 AM »
Quote from: mcalkins on April 07, 2013, 01:51:23 AM
Quote
It would break backwards-compatibility, in the strict sense, since if such code was compiled with QB45 itself, it would throw it out as an error, since it is an un-supported extension to that language.  True backwards-compatibility requires that the code should be able to be compiled and work as intended on both the original compiler and the 'newer' compiler which is emulating the original compiler.

No.
http://en.wikipedia.org/wiki/Backward_compatibility

That definition is flawed, because it only look forwards (in development of the compiler, in this case), not backwards, which true backwards-compatibility also requires.  People have to get out of thinking that Wiki is the only source of definition of terms.  How did we 'define' things before Wikipedia was launched (on 15/01/2001)?

Quote from: mcalkins on April 07, 2013, 01:51:23 AM
Anyone who cares about running code in both QBASIC and QB64 can constrain himself to the common subset.

Yes, I agree, that's what I was also saying (i.e. not being able to use the newer functionality which is not supported by the original compiler, if somebody wants to run their code on that original compiler).

bobtheunplayer

  • Jr. Member
  • **
  • Posts: 70
  • I'd rather be coding.
    • Email
Re: c style array initialization
« Reply #11 on: April 07, 2013, 04:31:28 AM »
Quote
It wouldn't be out of the question to port a significant subset of QB64 to 32 bit DOS. However, I'm not sure why anyone would want to, as DOS is quite limited. To be practical, you'd probably have to port portions of the Windows API as well. It would probably be better just to write your own Windows-like operating system, and forget DOS.
I don't worry about QB45 or QBASIC compatibility since I'm running Linux.  :P


SkyCharger001

  • Hero Member
  • *****
  • Posts: 1594
Re: c style array initialization
« Reply #12 on: April 07, 2013, 10:54:33 AM »
guys, are you sure you're not mixing backward and forward compatibility up?
Backward compatibility: can handle files and such of an older version. (which adding new features shouldn't break if done properly)
Forward compatibility: can handle files and such of a later version. (this one is easy to break)

sjt1975

  • Newbie
  • *
  • Posts: 46
    • My Personal Web-Site
    • Email
Re: c style array initialization
« Reply #13 on: April 07, 2013, 01:04:47 PM »
Quote from: bobtheunplayer on April 07, 2013, 04:31:28 AM
I don't worry about QB45 or QBASIC compatibility since I'm running Linux.  :P

LOL  Fair enough (!!!)...

  • Print