Help the newbie please with the PB datatypes

Just starting out? Need help? Post your questions and find answers here.
Peter_B
New User
New User
Posts: 4
Joined: Fri Mar 10, 2006 11:01 am

Help the newbie please with the PB datatypes

Post by Peter_B »

Hello.
I've got this brilliant software, PureBasic! :P But I'm so dumb :cry: 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 :wink:

P.S.: English is not my native language that's why there could be some misspellings.
josku_x
Addict
Addict
Posts: 997
Joined: Sat Sep 24, 2005 2:08 pm

Post by josku_x »

I think you might want to look the help file for "Variables and Types"
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
Peter_B
New User
New User
Posts: 4
Joined: Fri Mar 10, 2006 11:01 am

Post by Peter_B »

Thanks a lot guys! :wink:
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post 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 :)
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post 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 ;)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post 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 .. ??
thefool
Always Here
Always Here
Posts: 5875
Joined: Sat Aug 30, 2003 5:58 pm
Location: Denmark

Post 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 :D
well. They are 32 bit in memory. the smaller are less; so they are faster.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
Konne
Enthusiast
Enthusiast
Posts: 434
Joined: Thu May 12, 2005 9:15 pm

Post 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.
Apart from that Mrs Lincoln, how was the show?
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post 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.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Welcome to the easiest and powerful program language in the world, Peter
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Post Reply