Page 1 of 1
Help the newbie please with the PB datatypes
Posted: Tue May 23, 2006 4:24 pm
by Peter_B
Hello.
I've got this brilliant software, PureBasic!

But I'm so dumb

that I can't figure the use of different PB specific datatypes out.
Can someone explain me very pedagogically how when and why do I need or want to use the different datatypes. I did only used DarkBasic Pro (also as a beginner) where you have integer/string/float. There're also others but I didn't use them.
The questions are:
What is the difference between Byte/Cahracter/Long/...
When do I for example need to use Byte instead of Long and so on...?
What are advantages of String when you compare it to Float and so on...?
What is memory allocation and when, why and how do I use it?
Isn't memory allocation something automamatic which PB does for me automaticlly?
Do I also have to clear memory (I mean delete variable values) manually when I finish the in PB created program (main loop)?
Can you please give me some illustrative and relevant code examples to play with?
Please take in account that I'm a stupid beginner who doesn't have a clue bout the complicated stuff. Please make easy explanations.
Thank you very much. I hope you didn't get bored reading this long text
P.S.: English is not my native language that's why there could be some misspellings.
Posted: Tue May 23, 2006 4:34 pm
by josku_x
I think you might want to look the help file for "Variables and Types"
Posted: Tue May 23, 2006 6:16 pm
by netmaestro
Welcome to the forums, Peter! You'll find the answers to most of your questions easily in the docs. AllocateMemory() is an extremely flexible tool that allows you to create any structure of any size in memory and manipulate it to suit your needs. It isn't something someone new to programming would use much, if at all, but to an experienced coder it's pure gold. It's used in conjunction with all the PEEK and POKE variants as well as a few advanced tools like CopyMemory() and MoveMemory() and is an invaluable complement to such tools as the Pack library, Network library and File library, to name a few. Think of it as a kind of custom canvas, or repository, that you can configure any way at all and then read and write from at will.
Posted: Tue May 23, 2006 6:21 pm
by Trond
What is the difference between Byte/Cahracter/Long/...
A byte occupies one byte of memory and is signed.
A character occupies one byte when you compile as ASCII and two bytes when Unicode is checked in the compiler options. A character is unsigned.
A word occupies two bytes and is signed
A long occupies four bytes and is signed.
When do I for example need to use Byte instead of Long and so on...?
Since bytes are slower only use them when you use them in large arrays (to save memory) and when they are required for API calls. Use words only when you need to save memory (they are slower than longs) and when they are required for API calls.
Be sure that you don't need to store a number larger than the type can handle. Look in the manual for a list of what number ranges each type can handle.
What are advantages of String when you compare it to Float and so on...?
Strings holds characters (letters, numbers, spaces and symbols) while floats holds decimal numbers (2, 2.0, 6.555692).
What is memory allocation and when, why and how do I use it?
You don't need to know how and why yet if you don't know. Sooner or later you'll need it an then you'll know you'll need it. In most cases you don't need to worry. Basically it is reserving a part of memory for use of your program.
Isn't memory allocation something automamatic which PB does for me automaticlly?
Yes, everything is automatic, but it can also be done manual with AllocateMemory() and FreeMemory(). But you don't need to use those for most if not all programs. Sometimes they can be useful, but forget about them until you fully understand bytes/longs/floats/strings.
Do I also have to clear memory (I mean delete variable values) manually when I finish the in PB created program (main loop)?
If you have not used AllocateMemory() you don't need to clear memory. When your program ends all automatically allocated memory is freed automatically. So no, you don't need to do anything special.
Can you please give me some illustrative and relevant code examples to play with?
There are many in the examples folder.
Posted: Tue May 23, 2006 7:19 pm
by Peter_B
Thanks a lot guys!

Posted: Tue May 23, 2006 7:47 pm
by thefool
Trond wrote:
Do I also have to clear memory (I mean delete variable values) manually when I finish the in PB created program (main loop)?
If you have not used AllocateMemory() you don't need to clear memory. When your program ends all automatically allocated memory is freed automatically. So no, you don't need to do anything special.
And why would you need to clear the memory IF you used AllocateMemory?
The only reason to free it would be if you are in the middle of the program not needing the data it's holding anymore.
edit: Forgot to say welcome to you, Peter

Posted: Tue May 23, 2006 7:56 pm
by Num3
thefool wrote:Trond wrote:
Do I also have to clear memory (I mean delete variable values) manually when I finish the in PB created program (main loop)?
If you have not used AllocateMemory() you don't need to clear memory. When your program ends all automatically allocated memory is freed automatically. So no, you don't need to do anything special.
And why would you need to clear the memory IF you used AllocateMemory?
The only reason to free it would be if you are in the middle of the program not needing the data it's holding anymore.
Yes you should clear all the resources you allocated, it's good computer programming practice...
PB can fail, even windows can fail!
Trust no one

Posted: Tue May 23, 2006 8:57 pm
by Trond
thefool wrote:Trond wrote:
Do I also have to clear memory (I mean delete variable values) manually when I finish the in PB created program (main loop)?
If you have not used AllocateMemory() you don't need to clear memory. When your program ends all automatically allocated memory is freed automatically. So no, you don't need to do anything special.
And why would you need to clear the memory IF you used AllocateMemory?
The only reason to free it would be if you are in the middle of the program not needing the data it's holding anymore.
edit: Forgot to say welcome to you, Peter

Well, you don't have to in XP. I just said it because I do it and because it's not good to be able to allocate memory without being able to free it. So if he ever starts to use AllocateMemory() he'll learn how to free it again instead of putting that of until later.
Posted: Tue May 23, 2006 9:07 pm
by Shannara
I always believed the reason that Longs are faster then the smaller data types are because longs are basically 32-bit integers, and when running on 32-bit systems, it's fast ...
Now that ULongs are out (??, my C# terminology is coming in here ..), aka, 64-bit integers, that would mean, if (or when??) PB is able to run and compile on 64-bit systems, the ULongs would process faster then regular longs .. ??
Posted: Tue May 23, 2006 9:13 pm
by thefool
Num3: The pb help file CLEARLY states that its NOT nesecary to free the memory.
Trond: Its nothing to do with XP. Its ALL os'es.
But, of course its a good thing. its just unnecesary.
I always believed the reason that Longs are faster then the smaller data types are because longs are basically 32-bit integers, and when running on 32-bit systems, it's fast ...
haha

well. They are 32 bit in memory. the smaller are less; so they are faster.
Posted: Tue May 23, 2006 9:14 pm
by Trond
If by ULong you mean a 64-bit integer, then the answer is yes it will most likely be faster on 64-bit cpus.
Posted: Wed May 24, 2006 4:23 am
by Konne
But actually you do not have to care about the speed differences between Bytes and Longs etc... On todays Computers the difference in speed is less than a very small number and the adavantage that u need less memory, on the other side, should make it no problem to use Bytes for stuff like Yes/No Variables.
Quads are 64 Bit Variables (8Byte =2*Long) and Doubles are Flaots with 8 Byte. This is mainly needed if u use Files bigger than 2 GB.
Posted: Wed May 24, 2006 4:52 pm
by Shannara
Thats the same with size of an program

So which one to use? Faster speed? Definately for math intensive applications ... what about smaller size? A definate must for small environments ..

I guess it depends on the needs.
Posted: Wed May 24, 2006 7:50 pm
by Psychophanta
Welcome to the easiest and powerful program language in the world, Peter