A pointer to a constant

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

A pointer to a constant

Post by User_Russian »

It would be great if one could get a pointer to a constant what is required for some API functions.
Fred
Administrator
Administrator
Posts: 18153
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: A pointer to a constant

Post by Fred »

A pointer to constant makes no sens, which API needs that ?
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: A pointer to a constant

Post by User_Russian »

Code: Select all

#ClassName = "MyWin"

wc.WNDCLASSEX 
wc\cbsize        = SizeOf(WNDCLASSEX) 
wc\hInstance     = hInstance
wc\lpfnWndProc   = 0
wc\hCursor       = LoadCursor_(0, #IDC_ARROW) 
wc\hbrBackground = #COLOR_WINDOW
wc\lpszClassName = #ClassName ; It requires a pointer!
RegisterClassEx_(@wc)

hWnd  = CreateWindowEx_(0, #ClassName, #ClassName, #WS_VISIBLE, #CW_USEDEFAULT, #CW_USEDEFAULT, #CW_USEDEFAULT, #CW_USEDEFAULT, 0, 0, 0, 0)
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: A pointer to a constant

Post by ts-soft »

The structure require a pointer to a string, but not to a constant!
http://msdn.microsoft.com/en-us/library ... 85%29.aspx
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
skywalk
Addict
Addict
Posts: 4210
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: A pointer to a constant

Post by skywalk »

I remember stumbling on this once or twice:
I had problems passing pointers to literal constants: @3.14 or @"mystring".
Instead, I created a variable and assigned it to my constant and then passed a pointer to my variable.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: A pointer to a constant

Post by User_Russian »

In the constant string is stored. Right?
When the executable file is loaded into memory, all the constants be kept in memory, and why not get a pointer to this memory?
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: A pointer to a constant

Post by ts-soft »

User_Russian wrote:When the executable file is loaded into memory, all the constants be kept in memory, and why not get a pointer to this memory?
No constant in memory, all use of constants replaced with value. Constants not available in executable!
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
Fred
Administrator
Administrator
Posts: 18153
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: A pointer to a constant

Post by Fred »

What you are refering is a C macro, not a constant (there is no real constant in C).
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: A pointer to a constant

Post by User_Russian »

ts-soft, constant is loaded into memory, otherwise DataSection not exist.
A pointer to a constant can be obtained.
Otherwise, as you explained to this code works?

Code: Select all

#Const = "Test"

*Point=0
!MOV dword [p_Point],_S1

Debug *Point
Debug PeekS(*Point) 

x.s=#Const
And it is really a pointer to a constant and its value can be changed.

Code: Select all

#Const = "Test"

*Point=0
!MOV dword [p_Point],_S2

Debug *Point
Debug PeekS(*Point) 

PokeS(*Point, "1234")

x.s=#Const

Debug x
Fred
Administrator
Administrator
Posts: 18153
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: A pointer to a constant

Post by Fred »

It's not true for numeric constants for example
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: A pointer to a constant

Post by User_Russian »

I mean the only string constants.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: A pointer to a constant

Post by ts-soft »

A literal string or constant string is in datasection. The constant in executable is replaced by pointer to the string, but the constant itself is no more available!
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: A pointer to a constant

Post by Shield »

Receiving a pointer to a string constant does make sense.
However, Fred's points are valid too.

I could imagine that PB combines constants (literal or #const) with the same value
into one single entry in the data section of the executable, so receiving a pointer
to that location makes perfect sense and is a valid reason for a feature request.
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
skywalk
Addict
Addict
Posts: 4210
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: A pointer to a constant

Post by skywalk »

Macro can't find the string pointer address :(

Code: Select all

#someconst$ = "Hello to you!"
Macro StrCmp(str1, str2, UseCase=#PB_String_NoCase)
  Bool(CompareMemoryString(@str1, @str2, UseCase) = #PB_String_Equal) ;<-- Syntax Error in Macro @#Somestring$
EndMacro
s2$ = "hello World!"
Debug StrCmp(#SomeConst$,s2$)  ; ,#PB_String_CaseSensitive)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply