Author Topic: simple array question (I forgot)  (Read 5862 times)

0 Members and 1 Guest are viewing this topic.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
simple array question (I forgot)
« on: December 22, 2021, 08:44:46 pm »
when initializing an array and type vars - do they start with empty strings or integers ?



I'm tinkering with a program but have tons of arrays and type vars, and just realized that I may have to do the famous loop to zero stuff out.
Do I still need to do that?

example
Code: QB64: [Select]
  1. type tRec
  2. var1 as integer
  3. var2 as string
  4.  
  5. type t2ndRec
  6. var1 as integer
  7. var2 as tRec

do these start off as zero for the var1 and empty string for the var2?



I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

FellippeHeitor

  • Guest
Re: simple array question (I forgot)
« Reply #1 on: December 22, 2021, 08:50:51 pm »
Strings start empty (= "") and numbers start at 0.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: simple array question (I forgot)
« Reply #2 on: December 22, 2021, 08:54:46 pm »
I forgot, did we fix having to initialize UDT arrays at 0 or "", to be sure they are that?

FellippeHeitor

  • Guest
Re: simple array question (I forgot)
« Reply #3 on: December 22, 2021, 08:59:10 pm »
Please run some tests when you can, just to be 100% sure for us, but I do believe it's all good.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: simple array question (I forgot)
« Reply #4 on: December 22, 2021, 09:01:36 pm »
will do, when I finish the init section I'll run it and see if it gives me the required values
I'll post the results here.
but ya, its the UDT I am concerned about.
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: simple array question (I forgot)
« Reply #5 on: December 22, 2021, 09:35:48 pm »
If it was vitally important in my code, I wouldn't take chances and would clear them on initialization.

From my memory, we *usually* clear arrays at start, but I seem to remember some edge cases where we don't.

Is it the difference in DIM vs REDIM vs STATIC?

Is it the difference between arrays declared in the main module vs inside SUB/FUNCTION?  (They really are quite different, with temporary arrays being allocated on the fly in SUB/FUNCTIONs.)

Is it the difference in fixed length vs variable-length strings?  Or UDTs?

Honestly, I don't remember, but my mind is telling me there's some odd combo of those various options that end up with not all arrays being initialized to 0 and "".

If my code relied on null values at start-up, I wouldn't take any chances and would spend the .2 seconds to manually blank my array as necessary.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: simple array question (I forgot)
« Reply #6 on: December 22, 2021, 09:42:08 pm »
Another reason to initialize arrays (or any variables) to zero on your own is
the case of declaring an array in a SUB and later making the SUB static. 
« Last Edit: December 22, 2021, 09:50:18 pm by Richard Frost »
It works better if you plug it in.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: simple array question (I forgot)
« Reply #7 on: December 22, 2021, 09:50:58 pm »
People make SUBs static??  I've needed a few static variables but never a whole sub.  What's the use case for something like that?
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Richard Frost

  • Seasoned Forum Regular
  • Posts: 316
  • Needle nardle noo. - Peter Sellers
    • View Profile
Re: simple array question (I forgot)
« Reply #8 on: December 22, 2021, 09:56:00 pm »
I make a SUB static in an effort to cut down on global variables.

If I've got one-time initializations for a SUB, it's either have a global flag
for that sub or a local one that's preserved by making the SUB static.  There's
probably a better way that escapes me.
It works better if you plug it in.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: simple array question (I forgot)
« Reply #9 on: December 22, 2021, 11:48:07 pm »
Wow never knew you could make a whole sub static, for recursion it looks like.

Before this I thought the only reason to make a sub static was to keep the giant squid away:
https://fox2now.com/news/giant-squid-captured-on-video-attacking-submarine/

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: simple array question (I forgot)
« Reply #10 on: December 23, 2021, 04:56:49 am »
Unfortunately, there is a long-standing bug where an array of user-defined types which contain a variable length string may not have all its fields initialised to ""/0. This only occurs under rare circumstances, and only with variable length strings in user-defined types (fixed length strings, or simple arrays of strings are not affected). I have been unable to reproduce this with any kind of regularity which is why is escapes fixing.

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: simple array question (I forgot)
« Reply #11 on: December 23, 2021, 10:54:49 am »
I know just normal fixed length string arrays do NOT initialize to "". They tend to hold junk.

Pretty sure however that Fixed Length strings in UDTs do initialize to "". I would go with Luke's experience on the variable length ones though.
Granted after becoming radioactive I only have a half-life!

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: simple array question (I forgot)
« Reply #12 on: December 25, 2021, 04:22:41 am »
thanks for the replies

You are correct, it is good practice to init all vars, arrays, and udt, however, is a special case for me.
I was so fascinated by the "ON TIMER(x)" feature.
I am in the process of converting a clicker JS game to QB64, and have almost 500 lines of UDT. 

Here is what I was able to convert so far - goes pretty smooth. May not be the correct way. but it works
Code: QB64: [Select]
  1.  
  2. '-- THESE 2 LINES WILL RUN EVERY SECOND
  3. '-- AND UPDATE THE GAME WITH THE UPKEEP SUB
  4. On Timer(1) UP_KEEP
  5.  
  6. Const TRUE = 1
  7. Const FALSE = 0
  8.  
  9.  
  10. '-- GAME RECORDS
  11. '-- INITIATE EVERYTHING HERE.
  12. '--- TODO MAKE AN INI FILE FOR CUSTOM SETTINGS
  13.  
  14. '-- FOOD
  15. Type tCORE
  16.     nname As String * 20
  17.     total As Integer
  18.     increment As Integer
  19.     specialChance As Double
  20. Dim Shared CORE As tCORE
  21. Dim Shared FOOD As tCORE
  22. FOOD.nname = "food"
  23. FOOD.total = 0
  24. FOOD.increment = 1
  25. FOOD.specialChance = 0.1
  26.  
  27. '-- WOOD
  28. Dim Shared WOOD As tCORE
  29. WOOD.total = 0
  30. WOOD.increment = 1
  31. WOOD.specialChance = 0.1
  32.  
  33. '-- STONE
  34. Dim Shared STONE As tCORE
  35. STONE.total = 0
  36. STONE.increment = 1
  37. STONE.specialChance = 0.1
  38.  
  39.  
  40. '-- SKINS
  41. Dim Shared SKINS As tCORE
  42. SKINS.nname = "skins"
  43. SKINS.total = 0
  44.  
  45. Dim Shared HERBS As tCORE
  46. HERBS.nname = "herbs"
  47. HERBS.total = 0
  48.  
  49. Dim Shared ORE As tCORE
  50. ORE.nname = "ore"
  51. ORE.total = 0
  52.  
  53. Dim Shared LEATHER As tCORE
  54. LEATHER.nname = "leather"
  55. LEATHER.total = 0
  56.  
  57. Dim Shared METAL As tCORE
  58. METAL.nname = "metal"
  59. METAL.total = 0
  60.  
  61. Dim Shared PIETY As tCORE
  62. PIETY.nname = "piety"
  63. PIETY.total = 0
  64.  
  65. Dim Shared GOLD As tCORE
  66. GOLD.nname = "piety"
  67. GOLD.total = 0
  68.  
  69. LAND = 1000
  70. Dim Shared TOTAL_BUILDINGS As Long
  71. TOTAL_BUILDINGS = 0
  72.  
  73. Type TREQUIRES
  74.     FOOD As Integer
  75.     WOOD As Integer
  76.     STONE As Integer
  77.     SKINS As Integer
  78.     HERBS As Integer
  79.     ORE As Integer
  80.     LEATHER As Integer
  81.     METAL As Integer
  82.     PIETY As Integer
  83.     CORPSES As Integer
  84.  
  85. Type treqtemplate
  86.     total As Integer
  87.     devotion As Integer
  88.     requires As TREQUIRES
  89.  
  90.  
  91. Dim Shared TENT As treqtemplate
  92. TENT.requires.WOOD = 2
  93. TENT.requires.SKINS = 2
  94.  
  95. Dim Shared WHUT As treqtemplate
  96. WHUT.requires.WOOD = 20
  97. WHUT.requires.SKINS = 2
  98.  
  99.  
  100. Dim Shared COTTAGE As treqtemplate
  101. COTTAGE.requires.WOOD = 10
  102. COTTAGE.requires.STONE = 30
  103.  
  104. Dim Shared HOUSE As treqtemplate
  105. HOUSE.requires.WOOD = 30
  106. HOUSE.requires.STONE = 70
  107.  
  108. Dim Shared MANSION As treqtemplate
  109. MANSION.requires.WOOD = 200
  110. MANSION.requires.STONE = 200
  111.  
  112. Dim Shared BARN As treqtemplate
  113. BARN.requires.WOOD = 100
  114.  
  115. Dim Shared WOODSTOCK As treqtemplate
  116. WOODSTOCK.requires.WOOD = 100
  117.  
  118. Dim Shared STONESTOCK As treqtemplate
  119. STONESTOCK.requires.WOOD = 100
  120.  
  121. Dim Shared TANNERY As treqtemplate
  122. TANNERY.requires.WOOD = 30
  123. TANNERY.requires.STONE = 70
  124. TANNERY.requires.SKINS = 2
  125.  
  126. Dim Shared SMITHY As treqtemplate
  127. SMITHY.requires.WOOD = 30
  128. SMITHY.requires.STONE = 70
  129. SMITHY.requires.ORE = 2
  130.  
  131. Dim Shared APOTHECARY As treqtemplate
  132. APOTHECARY.requires.WOOD = 30
  133. APOTHECARY.requires.STONE = 70
  134. APOTHECARY.requires.HERBS = 2
  135.  
  136. Dim Shared TEMPLE As treqtemplate
  137. TEMPLE.requires.WOOD = 30
  138. TEMPLE.requires.STONE = 120
  139. TEMPLE.requires.HERBS = 10
  140.  
  141. Dim Shared BARRACKS As treqtemplate
  142. BARRACKS.requires.WOOD = 60
  143. BARRACKS.requires.FOOD = 20
  144. BARRACKS.requires.STONE = 120
  145.  
  146. Dim Shared STABLE As treqtemplate
  147. STABLE.requires.FOOD = 60
  148. STABLE.requires.WOOD = 60
  149. STABLE.requires.STONE = 120
  150. STABLE.requires.LEATHER = 10
  151.  
  152. Dim Shared MILL As treqtemplate
  153. MILL.requires.WOOD = 100
  154. MILL.requires.STONE = 100
  155.  
  156. Dim Shared GRAVEYARD As treqtemplate
  157. GRAVEYARD.requires.WOOD = 50
  158. GRAVEYARD.requires.STONE = 200
  159. GRAVEYARD.requires.HERBS = 50
  160.  
  161. Dim Shared FORTIFICATION As treqtemplate
  162. FORTIFICATION.requires.STONE = 100
  163.  
  164. Dim Shared BATTLEALTAR As treqtemplate
  165. BATTLEALTAR.devotion = 1
  166. BATTLEALTAR.requires.METAL = 50
  167. BATTLEALTAR.requires.PIETY = 200
  168. BATTLEALTAR.requires.STONE = 200
  169.  
  170. Dim Shared FIELDSALTAR As treqtemplate
  171. FIELDSALTAR.devotion = 1
  172. FIELDSALTAR.requires.FOOD = 500
  173. FIELDSALTAR.requires.WOOD = 500
  174. FIELDSALTAR.requires.STONE = 200
  175. FIELDSALTAR.requires.PIETY = 200
  176.  
  177. Dim Shared UNDERWORLDALTER As treqtemplate
  178. UNDERWORLDALTER.devotion = 1
  179. UNDERWORLDALTER.requires.PIETY = 200
  180. UNDERWORLDALTER.requires.STONE = 200
  181. UNDERWORLDALTER.requires.CORPSES = 1
  182.  
  183. Dim Shared CATALTER As treqtemplate
  184. CATALTER.devotion = 1
  185. CATALTER.requires.PIETY = 200
  186. CATALTER.requires.STONE = 200
  187. CATALTER.requires.HERBS = 100
  188.  
  189. Type TWONDER
  190.     TOTAL As Integer
  191.     WOOD As Integer
  192.     FOOD As Integer
  193.     STONE As Integer
  194.     SKINS As Integer
  195.     HERBS As Integer
  196.     ORE As Integer
  197.     LEATHER As Integer
  198.     METAL As Integer
  199.     PIETY As Integer
  200.     NNAME As String * 20
  201.     BUILDING As Integer
  202.     COMPLETED As Integer
  203.     PROGRESS As Integer
  204. Dim Shared WONDER As TWONDER
  205.  
  206. Type TPOPULATION
  207.     CURRENT As Integer
  208.     CAP As Integer
  209.     CATS As Integer
  210.     CORPSES As Integer
  211.     ZOMBIES As Integer
  212.     GRAVES As Integer
  213.     UNEMPLOYED As Integer
  214.     FARMERS As Integer
  215.     WOODCUTTERS As Integer
  216.     MINERS As Integer
  217.     TANNERS As Integer
  218.     BLACKSMITHS As Integer
  219.     APOTHECARIES As Integer
  220.     CLERICS As Integer
  221.     LABOURERS As Integer
  222.     SOLDIERS As Integer
  223.     SOLDIERSCAS As Integer
  224.     CAVALRY As Integer
  225.     CAVALRYCAS As Integer
  226.     CAVALRYPARTY As Integer
  227.     CAVALRYPARTYCAS As Integer
  228.     SIEGE As Integer
  229.     ESOLDIERS As Integer
  230.     ESOLDIERSCAS As Integer
  231.     EFORTS As Integer
  232.     healthy As Integer
  233.     totalsick As Integer
  234.     healthyas As Integer
  235.     totalSickas As Integer
  236.     unemployedIll As Integer
  237.     farmersIll As Integer
  238.     woodcuttersIll As Integer
  239.     minersIll As Integer
  240.     tannersIll As Integer
  241.     blacksmithsIll As Integer
  242.     apothecariesIll As Integer
  243.     clericsIll As Integer
  244.     labourersIll As Integer
  245.     soldiersIll As Integer
  246.     soldiersCasIll As Integer
  247.     cavalryIll As Integer
  248.     cavalryCasIll As Integer
  249.     wolves As Integer
  250.     wolvesCas As Integer
  251.     bandits As Integer
  252.     banditsCas As Integer
  253.     barbarians As Integer
  254.     barbariansCas As Integer
  255.     esiege As Integer
  256.     enemiesSlain As Integer
  257.     shades As Integer
  258. Dim Shared POPULATION As TPOPULATION
  259.  
  260. Type Tefficiency
  261.     happiness As Integer
  262.     farmers As Double
  263.     pestBonus As Double
  264.     woodcutters As Double
  265.     miners As Double
  266.     tanners As Double
  267.     blacksmiths As Double
  268.     apothecaries As Double
  269.     clerics As Double
  270.     soldiers As Double
  271.     cavalry As Double
  272. Dim Shared EFFICIENCY As Tefficiency
  273. EFFICIENCY.happiness = 1
  274. EFFICIENCY.farmers = 0.2
  275. EFFICIENCY.pestBonus = 0
  276. EFFICIENCY.woodcutters = 0.5
  277. EFFICIENCY.miners = 0.2
  278. EFFICIENCY.tanners = 0.5
  279. EFFICIENCY.blacksmiths = 0.5
  280. EFFICIENCY.apothecaries = 0.1
  281. EFFICIENCY.clerics = 0.05
  282. EFFICIENCY.soldiers = 0.05
  283. EFFICIENCY.cavalry = 0.08
  284.  
  285. Type TUPGRADES
  286.     domestication As Integer
  287.     ploughshares As Integer
  288.     irrigation As Integer
  289.     skinning As Integer
  290.     harvesting As Integer
  291.     prospecting As Integer
  292.     butchering As Integer
  293.     gardening As Integer
  294.     extraction As Integer
  295.     croprotation As Integer
  296.     selectivebreeding As Integer
  297.     fertilisers As Integer
  298.     masonry As Integer
  299.     construction As Integer
  300.     architecture As Integer
  301.     wheel As Integer
  302.     horseback As Integer
  303.     tenements As Integer
  304.     slums As Integer
  305.     granaries As Integer
  306.     palisade As Integer
  307.     weaponry As Integer
  308.     shields As Integer
  309.     writing As Integer
  310.     administration As Integer
  311.     codeoflaws As Integer
  312.     mathematics As Integer
  313.     aesthetics As Integer
  314.     civilservice As Integer
  315.     feudalism As Integer
  316.     guilds As Integer
  317.     serfs As Integer
  318.     nationalism As Integer
  319.     flensing As Integer
  320.     macerating As Integer
  321.     standard As Integer
  322.     deity As Integer
  323.     deityType As Integer
  324.     lure As Integer
  325.     companion As Integer
  326.     comfort As Integer
  327.     blessing As Integer
  328.     waste As Integer
  329.     stay As Integer
  330.     riddle As Integer
  331.     throne As Integer
  332.     lament As Integer
  333.     book As Integer
  334.     feast As Integer
  335.     secrets As Integer
  336.     trade As Integer
  337.     currency As Integer
  338.     commerce As Integer
  339. Dim Shared UPGRADES As TUPGRADES
  340.  
  341. Type TDEITY
  342.     nname As String * 20
  343.     ttype As String * 20
  344.     seniority As Integer
  345.     devotion As Integer
  346.     battle As Integer
  347.     fields As Integer
  348.     underworld As Integer
  349.     cats As Integer
  350. Dim Shared DEITY As TDEITY
  351. DEITY.seniority = 1
  352.  
  353. Type TACHIEVEMENTS
  354.     hamlet As Integer
  355.     village As Integer
  356.     smallTown As Integer
  357.     largeTown As Integer
  358.     smallCity As Integer
  359.     largeCity As Integer
  360.     metropolis As Integer
  361.     smallNation As Integer
  362.     nation As Integer
  363.     largeNation As Integer
  364.     empire As Integer
  365.     raider As Integer
  366.     engineer As Integer
  367.     domination As Integer
  368.     hated As Integer
  369.     loved As Integer
  370.     cat As Integer
  371.     glaring As Integer
  372.     clowder As Integer
  373.     battle As Integer
  374.     cats As Integer
  375.     fields As Integer
  376.     underworld As Integer
  377.     fullHouse As Integer
  378.     plague As Integer
  379.     ghostTown As Integer
  380.     wonder As Integer
  381.     seven As Integer
  382.     merchant As Integer
  383.     rushed As Integer
  384.     neverclick As Integer
  385. Dim Shared ACHIEVEMENTS As TACHIEVEMENTS
  386.  
  387. Type TTRADER
  388.     MATERIAL As Integer
  389.     REQUESTED As Integer
  390.     TTIMER As Integer
  391.  
  392. Dim Shared TRADER As TTRADER
  393.  
  394. Type TRAIDING
  395.     ISRAIDING As Integer
  396.     VICTORY As Integer
  397.     ITERATIONS As Integer
  398.     LAST As String * 20
  399. Dim Shared RAIDING As TRAIDING
  400. RAIDING.ISRAIDING = FALSE
  401. RAIDING.VICTORY = FALSE
  402.  
  403. Dim Shared TARGETMAX As String
  404. CIVTYPE = "THORP"
  405. TARGETMAX = "THORP"
  406.  
  407. Dim Shared resourceClicks As Integer
  408. Dim Shared logRepeat As Integer
  409. Dim Shared attackCounter As Integer
  410. Dim Shared tradeCounter As Integer
  411. Dim Shared throneCount As Integer
  412. Dim Shared pestTimer As Integer
  413. Dim Shared gloryTimer As Integer
  414. Dim Shared cureCounter As Integer
  415. Dim Shared graceCost As Integer
  416. Dim Shared walkTotal As Integer
  417. Dim Shared autosave As String * 4
  418. Dim Shared autosaveCounter As Integer
  419. Dim Shared customIncrements As Integer
  420. Dim Shared delimiters As Integer
  421. Dim Shared usingWords As Integer
  422. Dim Shared worksafe As Integer
  423. Dim Shared thissize As Integer
  424.  
  425. resourceClicks = 0
  426. logRepeat = 1
  427. attackCounter = 0
  428. tradeCounter = 0
  429. throneCount = 0
  430. pestTimer = 0
  431. gloryTimer = 0
  432. cureCounter = 0
  433. graceCost = 1000
  434. walkTotal = 0
  435. autosave = "on"
  436. autosaveCounter = 1
  437. customIncrements = FALSE
  438. delimiters = TRUE
  439. usingWords = FALSE
  440. worksafe = FALSE
  441. thissize = 1
  442.  



And I believe @Cobalt is correct - UDT fixed strings usually hold junk
Also, anyone have an idea how to use "name" in a UDT that doesnt produce an error?
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: simple array question (I forgot)
« Reply #13 on: December 25, 2021, 09:30:53 am »
Name is a keyword in QB64 like Print. Add or subtract a letter or more for variable name.

Offline xra7en

  • Seasoned Forum Regular
  • Posts: 284
    • View Profile
Re: simple array question (I forgot)
« Reply #14 on: December 25, 2021, 11:23:23 am »
Name is a keyword in QB64 like Print. Add or subtract a letter or more for variable name.

O ya,, knew that. I tried _name but that didnt work either. so using nname, but I saw in @Cobalt had a good idea, and used "nam" not bad. will prob switch to that

thanks @Cobalt
I just like re-writing old DOS book games into modern QB64 code - weird hobby, I know!