x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

FSTRUCT() pointers can be used as return value in none UFUNCTION() function, but not as a parameter return value

I made this observation when some of my functions could return a FStruct pointer but some couldn't (they where always Null). I have a TArray with FStructs that i wanted to get references from. To do that i made a function that creates a pointer to the struct i want, like this:

 void function(FStruct* structPointer)
 {
   structPointer = &MyArray[0];
 }

But this pointer is always Null outside the function. But if you do it like this it works:

 FStruct* function()
 {
   return &MyArray[0];
 }

Why does it work like this? And is it safe to use my solution to get a pointer to a struct? In UFUNCTIONs you can't even use FStruct pointers.

Product Version: UE 4.18
Tags:
more ▼

asked May 01 '18 at 09:32 AM in C++ Programming

avatar image

carpet200
88 3 4 7

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Thats because you just setting local argument variable which will get deleted when function is over. Pointers work like integers, which contain memory address to variable and you can access content of it with * and -> operators, ince it works like integer, when you do = you will simply change address of pointer in that argument value, but it still gonna be deleted same as you would use int32.

So you either need to use reference:

  void function(FStruct*& structPointer)
  {
    structPointer = &MyArray[0];
  }

Refrence works like pointer but it with constsant address and it behaves like normal varbale

http://en.cppreference.com/w/cpp/language/reference

or modify value that pointer pointing to direcly:

  void function(FStruct* structPointer)
  {
    *structPointer = MyArray[0];
  }

more ▼

answered May 01 '18 at 02:24 PM

avatar image

Shadowriver
36.6k 932 169 1111

avatar image Shadowriver May 02 '18 at 01:58 AM

I forgot to mention something important, you should not create pointers from array values, because array can do memory reallocation when new item is added to it, which may cause change of memory address of array items and pointer you used array item will become invalid and wither give trash values or crash the game. so imo 2nd option i gaved you is most safe one

avatar image carpet200 May 02 '18 at 08:41 AM

Thank you for the answer. Luckily im never going to change the array after initialization so this will work.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question