KeyPgString

Forum for discussion about the documentation project.
Post Reply
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

KeyPgString

Post by speedfixer »

There is no example of fixed size string.

To declare a fixed size string, the argument must be a constant.

Code: Select all

test2.bas(14) error 11: Expected constant in 'dim result as string * (length + 1)'
badidea
Posts: 2586
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: KeyPgString

Post by badidea »

On https://freebasic.net/wiki/KeyPgstring you mean? I think you are right, fixed-size apparently means that the size is determined at compile time and only a constant for the size can be used, but that is not very obvious form the manual page. I'll wait for other responses...
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: KeyPgString

Post by caseih »

I guess I've never ever thought about it. To me "fixed-size string" is self-evident that it must be defined at compile time and would therefore require a constant expression. Dunno. I also think the error message is clear and points you in the right direction.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: KeyPgString

Post by fxm »

KeyPgString → fxm [added precision on fixed-size string declaration syntax]
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: KeyPgString

Post by dodicat »

I note line:
A simpler and safer method for dynamic allocation /deallocation is to use the advanced New / Delete keyword (which itself also calls the constructor / destructor of the string object).


Could you provide an example of usage of this perhaps, I have not seen strings created this way.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: KeyPgString

Post by fxm »

The NEW operator can only be used for dynamic strings allocation:

Code: Select all

Dim As String Ptr ps = New String

*ps = "FreeBASIC"
Print *ps

Delete ps
dodicat wrote:IA simpler and safer method for dynamic allocation /deallocation is to use the advanced New / Delete keyword (which itself also calls the constructor / destructor of the string object).
If using [C]Allocate, the code is less straightforward:

Code: Select all

Dim As String Ptr ps = Allocate(Sizeof(String))

Clear *ps, 0, Sizeof(String)  '' not needed if using Callocate instead of Allocate
*ps = "FreeBASIC"
Print *ps
*ps = ""  '' always mandatory to avoid memory leaks

Deallocate ps
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: KeyPgString

Post by counting_pine »

I wonder if the info about allocate/new/delete etc. could go in its own section somehow.
Most people reading the article won't be interested in how to store string descriptors in allocated memory, but nor do we want to remove that information.

I can't remember off the top of my head, does the wiki support custom subheadings?
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: KeyPgString

Post by coderJeff »

counting_pine wrote:I can't remember off the top of my head, does the wiki support custom subheadings?
For the styles that are recognized well by the wiki and doc generator:
FBWikiFbdocAction

For a custom heading can use: {{fbdoc item="section" value="Section"}}

To add a new fbdoc item section, for example {{fbdoc item="advanced"}} with some custom title, then need to update the wiki PHP and fbdoc generator - which is minor code changes.
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: KeyPgString

Post by fxm »

counting_pine wrote:I wonder if the info about allocate/new/delete etc. could go in its own section somehow.
Most people reading the article won't be interested in how to store string descriptors in allocated memory, but nor do we want to remove that information.
- For the special case of var-len string dynamic allocation with Allocate/Callocate/Reallocate/Deallocate, the mandatory precautions to be taken are then described both on the 'String' page and on the 'Allocate'/'Callocate'/'Reallocate'/'Deallocate' page.
- For the case of var-len string dynamic allocation with New/Delete, as no precaution is to be taken (the implicit call to the constructor and the destructor doing all the work), nothing is then described on the 'New'/'Delete' page.
Post Reply