CreateThread Procedure corrupting strings

Just starting out? Need help? Post your questions and find answers here.
sc4pb
User
User
Posts: 26
Joined: Tue Mar 07, 2023 5:33 pm

Re: CreateThread Procedure corrupting strings

Post by sc4pb »

According to the PureBasic.pdf docs (page 358)

A long is ALWAYS 4 bytes

An integer is 4 bytes on 32-bit CPUs and 8 bytes on 64-bit machines

...and the default type is integer, meaning you don't know if it is 4 or 8 bytes (technically) So for all of my handles, I probably should have been using 8 bytes because I'm on a 64-bit machine, but long is locked to 4, unless docs are wrong
jassing
Addict
Addict
Posts: 1745
Joined: Wed Feb 17, 2010 12:00 am

Re: CreateThread Procedure corrupting strings

Post by jassing »

sc4pb wrote: Thu Jun 08, 2023 2:53 am A long is ALWAYS 4 bytes

Yup; I was wrong. (I had it the other way around; working with a 32bit app right now)
Sorry for the mis-information.

Code: Select all

Debug SizeOf(long)
Debug SizeOf(integer)
User avatar
Demivec
Addict
Addict
Posts: 4085
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: CreateThread Procedure corrupting strings

Post by Demivec »

sc4pb wrote: Thu Jun 08, 2023 2:42 am Awww nuts, stupid question time.

For PureBasic built in library procedures that return "handles" to things, you know databases, images, memory blocks whatever, what data type should I be using? I just realized as I was looking over the docs that I was mistaken about something.
I have just a few comments. These things should reasonably be known after reading only a small portion of the documentation for PureBasic.
Excerpt from General Synrax Rules wrote:- Return values of commands are always Integer if no other type is specified in the Syntax line of the command description.
https://www.purebasic.com/documentation/reference/general_rules.html
Many examples in docs utilize variables that are not declared, just generated on the fly. And if no type is specified, for some reason I thought the default type was "long" (4 bytes). Now I see the default type is "integer", which can be either 4 or 8 bytes depending on 32 or 64 bit processor.

Well ok, for all of my calls with handles, like AllocateMemory and OpenDatabase, I've been using longs. So...

Code: Select all

Define handle.l
handle=AllocateMemory(1000)
...but this is wrong, right? I should have been using integers?
Yes you should be using integers. Historically, the integer type was created as a separate type about the time when 64-bit compilations first became available. Previous to that all handles were longs. Older code and possibly some examples in the documentation may have been written with longs used to hold object handles.
Excerpt from documentation for Define wrote:Without this keyword, variables are created with the default type of PureBasic which is the type INTEGER. As a reminder, the type INTEGER is:
4 bytes (with a 32-bit compiler) ranging from -2147483648 to + 2147483647
8 bytes (with a 64-bit compiler) ranging from -9223372036854775808 to +9223372036854775807
https://www.purebasic.com/documentation/reference/define.html

I bring this up because of another big gripe of mine:

I won't write a line of code without EnableExplicit. But in my mind EnableExplicit should require both a variable declaration AND a type declaration. So when explicit:

Code: Select all

Define foo ; this should be REJECTED by EnableExplicit, there is no type
Define foo.s ;this is fine, it's a string.
I've had a number of bugs that have been hard to track down because I failed to add a type to a variable declaration.

Is there way to require a type? Or in other words, to disable the default behavior of PureBasic assuming variables are integers?
I have no opinion on your feature request. EnableExplicit was added in v4.00 and may benefit from some additional options.
Post Reply