+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2016
    Posts
    1

    Default Is it possible to have determine array size in runtime

    This is what the manual says about defining arrays:

    4.5. Arrays


    Variable arrays can be created in a similar manner to variables.


    Label VAR Size[Number of elements]


    Label is any identifier, excluding keywords, as described above. Size is BIT, BYTE or WORD. Number of elements is how many array locations is desired.
    It is not clear if the Number of elements has to be a literal number or a variable can be used.

    Can I do something like this?

    Code:
    READ 1, num_books ' how many nooks
    books VAR BYTE[num_books] ' Define array for each book price

  2. #2
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    421

    Default Re: Is it possible to have determine array size in runtime

    I do not believe that you could demension an array in the middle of your code and have it be dynamic at runtime.
    I believe that variables and arrays are defined and created by the compiler.

    But I could be wrong?

    Can you not just make your array sufficiently large to be ready for the largest number of books you expect to have?
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  3. #3
    Join Date
    Sep 2009
    Posts
    637

    Default Re: Is it possible to have determine array size in runtime

    @Heckler
    You are right, all variables are defined by compiler.

    @MajidF
    Number of elements must be a literal number or constant. Constants are just names that represents number.
    So it must be a number like 15.
    A var byte[15]
    Or
    NumberFifteen CON 15
    A var byte[NumberFifteen]

  4. #4
    Join Date
    Aug 2003
    Location
    Australia
    Posts
    973

    Default Re: Is it possible to have determine array size in runtime

    Hi, What you’re talking about is what malloc (memory allocate) in C does.
    That’s intended for programs running under operating systems where other programs may be running,
    and already consuming memory. So your program asks the system for a quantity of memory,
    and a flag is returned if it was available.

    I doubt it’s implemented in C for microcontrollers because the function would consume considerable
    RAM and code space for many microcontrollers.

    When you declare an array in PBP or RISC asm, all the compiler remembers is to reserve the next
    memory spaces and mark the next variables in the locations following the array.
    Code:
    var byte array[20]
    var byte value
    In this case the variable “Value” will be permanently associated with RAM location 21,
    and any time the array is used, the index is added to the array start location.
    Code:
    array[10] = 1
    
    same as
    
    (*array + 10) = 1
    These numbers aren’t strictly true because PBP will put some of it’s own system variables before your array.

    If there’s only one possible big array to deal with I don’t see a reason not to simply make it as large as you can afford.
    If you don’t always use the maximum size of it, you can always mark the end with a normally unused character or sequence,
    and search for that to find the current array size you’re using.
    Last edited by Art; - 21st March 2016 at 16:54.

Similar Threads

  1. Changing CONFIG @ runtime possible ???
    By Acetronics2 in forum PBP3
    Replies: 2
    Last Post: - 8th April 2014, 19:47
  2. PIC16 Max Array Size
    By MikeWinston in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 25th February 2012, 17:30
  3. Configure Debug @ runtime?
    By dhouston in forum Serial
    Replies: 5
    Last Post: - 11th November 2011, 21:04
  4. Redefinition of CON bmode during runtime
    By Pedro Pinto in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 16th February 2008, 22:57
  5. PIC18F4515 Serin2 Maximum Array size
    By millersamsr in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 6th April 2007, 00:29

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts