String var memory allocation with API

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Denis.

Hi,

I don't know if what's following is a bug.
I crash many time my computer using API GetMenuString_
This API needs a buffer for string result.

Code: Select all

buffer.s
    GetMenuString_(MenuID,#Menu2,@buffer,81,#MF_BYCOMMAND)
buffer is only declared before. No memory allocation for it.

If you assign a value to buffer, then the API run well.

Code: Select all

buffer.s=""
    GetMenuString_(MenuID,#Menu2,@buffer,81,#MF_BYCOMMAND)
This code run well; You can assign any value to buffer, since you assign one.

In my opinion, it's a bug because 'buffer' is declared before the API calling and API has to use 'buffer' to assign a value. I think that PB syntax is respected.
What it is important is to know this.


Denis
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

that is what the msdn says:

int GetMenuString(

HMENU hMenu, // handle to the menu
UINT uIDItem, // menu item identifier
LPTSTR lpString, // pointer to the buffer for the string
int nMaxCount, // maximum length of the string
UINT uFlag // menu flags
);

the stringbuffer will note be alocated automatically by the api. you have to do it yourselfe. (like the most api commands)

buffer.s=space(255) for eg. will reserver 255 bytes for this string. (0 terminated, so its 256 bytes long)

Mike

Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User
System: Windows 2000 Server, 512 MB Ram, GeForce4200 TI 128 MB DDR, Hercules Theater 6.1 DTS Sound
System 2: Mobile Pentium 4 2.4GHz 512 MB DDR GeForce4 420-32, Windows XP Home
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Denis.

'the stringbuffer will note be alocated automatically by the api. you have to do it yourselfe. (like the most api commands)'

Yes, the API don't alocated memory for the buffer, that's why i mean that PB has to do it because the buffer (string var) is declared before API calling.
In this case, the value of buffer has no importance before API calling.

Denis

The road is long...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tranquil.

But PB doent know how many bytes/kBytes/MBs you require. Or didnt I see the point? Its on you to allocate enough memory. PB cant know how much you need.

Mike

Tranquilizer/ Secretly!
http://www.secretly.de
Registred PureBasic User
System: Windows 2000 Server, 512 MB Ram, GeForce4200 TI 128 MB DDR, Hercules Theater 6.1 DTS Sound
System 2: Mobile Pentium 4 2.4GHz 512 MB DDR GeForce4 420-32, Windows XP Home
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Denis.

Yes, it's right.
I don't remember if len of string in PB are limited ?

If yes, then PB has to put it with the max lenght.
If no, i have to put it myself.

Denis

The road is always long ...
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

It would be silly to allocate an empty string for you automatically. You have to do it, as Tranquil explained.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
Originally posted by Denis

Yes, it's right.
I don't remember if len of string in PB are limited ?

If yes, then PB has to put it with the max lenght.
If no, i have to put it myself.
Why does PB have to allocate the maximum length of string (the maximum is currently around 64k)? How does it know you want to use a string for the buffer of an API command? Parameters for API commands are only tagged as being addresses, not strings or buffers so the compiler cannot decide what needs to have strings allocated before they are used.

I hope you are not suggesting that PB should allocate 64k for all strings by default just in case you want to use one with a certain API command?

A far more sensible approach is that because you the programmer know what you want to do, then you should do it. This includes all allocations and freeing of resources.

This kind of thing has been shown before on these forums too. If you want to allocate some space in a string that the API will write into then you should do this:

Code: Select all

buffer.s = Space(256) ; Create a 256 byte buffer filled with spaces for API command to write into
blahblah = SomeAPICommand(..., @buffer, ...)

--
It's not minimalist - I'm increasing efficiency by reducing input effort.
(Win98first ed. + SP1, PB3.40)
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Denis.

OK,

and tks for the answers.


Denis
Post Reply