Qbasicnews.com

Full Version: Problem with strings and pointers and whatnot?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a user-defined type whose first element is a string.

Code:
type myType
   itemName as string
   itemType as integer
   ...
end type

I've got a pointer to this type somewhere in my code, and I can set values by doing the standard stuff:

Code:
myThingy->itemType = 44

But, I can't set the string. It crashes the compiled program. E.g.,

Code:
myThingy->itemName = strName

kills it. (Yes, strName is a string.) Also, strName is an argument passed to a procedure. Something like...

Code:
function initializeThingy(strName as string, [and whatnot]) as myType pointer
   dim temp as myType pointer
   [allocate the memory]
   temp->itemName = strName
   [insert rest of function here]
end function

If I comment out the string assignment statement, the code runs as expected, and normal results are produced (the values other than the string are being stored correctly and whatnot.)

v1c, any ideas here? Must be something I missed :-)
How is "temp" allocated? You have to always use CAllocate because Allocate won't clear the allocated block and it will be filled with garbage, and an assignament to a dyn string field can generate an exception, because the rtlib will try to free an invalid pointer.

Anonymous

Code:
type myType
   itemName as String
   itemType as Integer
end Type

Screen 13

Dim p As mytype Ptr
p = CAllocate(Len(mytype))
p->itemname = "dsd"
? p->itemname


Sleep
end


edit: ah.. slower than the man himself ;p
compiles, runs fine... perhaps try

p->itemname = ""

? no idea beyond that