Structure pointer

Just starting out? Need help? Post your questions and find answers here.
Nubcake
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Feb 03, 2011 7:44 pm

Structure pointer

Post by Nubcake »

Hi

Another newbish question but why is the pointer null in this case -> Is it because it doesn't point to anything? How do I correct this.

Code: Select all

EnableExplicit

Structure Pixel
 x.l
 y.l
Endstructure

Procedure.l someProc(someParam)
Define *Point.Pixel
*Point\x = 1 ;
Endprocedure


Edit: If i were to return a pointer from that procedure would I use PeekX to access the values of x or y or can I do something like *p.Pixel = @Pixel *p\x = 3... ?
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Structure pointer

Post by STARGÅTE »

Code: Select all

EnableExplicit

Structure Pixel
  x.l
  y.l
Endstructure

Procedure someProc(*Point.Pixel)
  *Point\x = 1 ;
Endprocedure

Define MyPixel.Pixel

someProc(@MyPixel)
Debug MyPixel\x
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: Structure pointer

Post by Bisonte »

First : Don't use .l as return if you want to return memory adresses ! If you started this on x64 machines, it may causes IMA's, if the adress range is out of the 2GB range...

A pointer is only ONE adress ! If you want to store a structure here a simple example

Code: Select all

Structure Pixel
 x.l
 y.l
EndStructure

Procedure someProc(someParam)
  
Protected *Point.Pixel = AllocateMemory(SizeOf(Pixel))

If *Point
  Debug "pointer ready"
  *Point\x = 1
  *Point\y = someParam
  ProcedureReturn *Point
Else
  Debug "oh oh ... cannot allocatememory"
  ProcedureReturn #False  
EndIf

EndProcedure

Define *Point.Pixel

*Point = someProc(15)
If *Point
  Debug *Point\x
  Debug *Point\y
  FreeMemory(*Point)
EndIf
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
Nubcake
Enthusiast
Enthusiast
Posts: 195
Joined: Thu Feb 03, 2011 7:44 pm

Re: Structure pointer

Post by Nubcake »

Bisonte wrote:
A pointer is only ONE adress ! If you want to store a structure here a simple example

Code: Select all

Structure Pixel
 x.l
 y.l
EndStructure

Procedure someProc(someParam)
  
Protected *Point.Pixel = AllocateMemory(SizeOf(Pixel))

If *Point
  Debug "pointer ready"
  *Point\x = 1
  *Point\y = someParam
  ProcedureReturn *Point
Else
  Debug "oh oh ... cannot allocatememory"
  ProcedureReturn #False  
EndIf

EndProcedure

Define *Point.Pixel

*Point = someProc(15)
If *Point
  Debug *Point\x
  Debug *Point\y
  FreeMemory(*Point)
EndIf
Yes this is what I was looking for thanks.
Edit:

Code: Select all

 *Point.Pixel = AllocateMemory(SizeOf(Pixel)) 
This pointer is not being freed ?
First : Don't use .l as return if you want to return memory adresses ! If you started this on x64 machines, it may causes IMA's, if the adress range is out of the 2GB range...
So I should use .i ?

Edit2: What's wrong with this code?

Code: Select all

Structure Pixel
  x.l
  y.l
EndStructure

Procedure.i proc(param)
  p.Pixel
  *ptr.Pixel = @p
  
  *ptr\x = param;
  *ptr\y = param;
  
  

  
  ProcedureReturn *ptr
  
EndProcedure

*p.Pixel = proc(32)


Debug *p\x
Debug *p\y
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: Structure pointer

Post by Bisonte »

Free up the memory is always necessary.

But in this little example not really, because if the program ends, PB will free up all pb interna like Sound, Images and so on...

in your second code is no allocation of memory.

Code: Select all

Structure Pixel
  x.l
  y.l
EndStructure

Procedure.i proc(param)

  *ptr.Pixel = AllocateMemory(SizeOf(Pixel))
 
  *ptr\x = param;
  *ptr\y = param;
  
  ProcedureReturn *ptr
 
EndProcedure

*p.Pixel = proc(32)

If *p 
  Debug *p\x
  Debug *p\y
EndIf

; and free
FreeMemory(*p)
If you use something like Variable.Pixel , PB allocate memory for this variable. so you can get the adress of this variable by @Variable.
But you make a new pointer in your example, so you have allocate the memory for this new pointer
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Structure pointer

Post by TI-994A »

Nubcake wrote:...So I should use .i ?

What's wrong with this code?

Code: Select all

Structure Pixel
  x.i
  y.i
EndStructure

Procedure proc(*p.Pixel, param)
  *p\x = param
  *p\y = param * 2
EndProcedure

Define p.Pixel

proc(@p, 32)

Debug p\x
Debug p\y
Hello Nubcake. The default type for PureBasic is the integer. So, if a variable is not explicitly declared, or defined without a type, PureBasic would automatically define it as an integer.

With reference to your code, if your requirement is simply to automate the assignment of values and the such, you may not wish to bother with all the memory management stuff. Instead, you could simply pass the address of the active structure to the procedure, and perform the required calculations and assignments on it directly; no pointers, no memory to allocate or free, and nothing to return either:

Code: Select all

Structure Pixel
  x.i
  y.i
EndStructure

Procedure proc(*p.Pixel, param)
  *p\x = param
  *p\y = param * 2
EndProcedure

Define p.Pixel

proc(@p, 32)

Debug p\x   ;returns 32
Debug p\y   ;returns 64
In fact, this is the approach that STARGÅTE had illustrated above. :wink:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Structure pointer

Post by Demivec »

Nubcake wrote:Edit2: What's wrong with this code?
Here's the code with comments to show where it went wrong:

Code: Select all

Structure Pixel
  x.l
  y.l
EndStructure

Procedure.i proc(param)
  p.Pixel
  *ptr.Pixel = @p  ;the pointer points to a memory location for the procedure's local variable 'p'
  
  *ptr\x = param;
  *ptr\y = param;
  
  

  
  ProcedureReturn *ptr ;you return the value of the pointer which is pointing to memory that is going to be freed and therefore doesn't point to anything

  ;You can correct this in two ways, the second method is the best way:
  ;1. You can make the variable P.Pixel a Static variable so that it won't be freed at the end of the procedure.  This will always show the last or current value
  ;    of the static variable so if you call the procedure multiple times it will use the same memory location but the procedure will keep overwriting the value
  ;    if it changes.
  ;2. You would allocate memory for the the variable which would then have to be freed outside of the procedure when you are done with it.
  ;     *ptr = AllocateMemory(SizeOf(Pixel)) ;always test if *ptr <> 0 to make sure this succeeds.
  
EndProcedure

*p.Pixel = proc(32)


Debug *p\x
Debug *p\y
Post Reply