Download

Issue related to pointer arithmetic

Good afternoon, guys!

I have a issue related to pointer arithmetic, and I think I’m forgetting something fundamental to pointer arithmetic, but sincerely I’m not detecting what is wrong here, since the left-hand operand of my = operator is a lvalue.

Here is the snapshot:

And here is the snippet: FString IStringUtils::UnsignedIntToString(const uint32& NumericValue, FString De - Pastebin.com

Also, I tried to change the snippet above to: FString IStringUtils::UnsignedIntToString(const uint32& NumericValue, FString De - Pastebin.com

But, strangely enough, the local variable String address remains the same, for each do while() iteration, inducing the Buffer pointer to point to the same address, instead pointing to another memory address, as expected from a local variable.

Could you guys point me my mistake here?

Thanks!

What are you trying to do? Direct pointer manipulation gets weird with strings in UE4 since they could be an array of formats / encodings (Ascii, U8, U16, etc).

If you’re just trying to change from an int to a string and back, there’s tons of options in the Lex name space (Lex::ToString, Lex::FromStriing, Lex::TryParse, etc).

1 Like

The code makes little sense. Buffer is a pointer to an empty string. You keep overwriting one character which is effectively the null terminator of that string, don’t store the result, then in the next loop try to read memory that is very much outside the buffer.
Use one of the built in methods to print a number to string, like FString::Printf.

1 Like

Hello there! Thanks for your replies, guys.

I did this function because I didn’t see an option to convert uint32 to FString. By default, FString has the FromInt function, that accepts an int32, and, since the max value of an uint32 is greater than the int32 max positive value, I tried to avoid an overflow.

I didn’t know about the Lex namespace, but when I googled it, I didn’t find anything related to it in the UE4 documentation.

Yea, documentation is never going to be up to date. The best documentation is the engine itself. If you look around for console commands and such (basically anything that does string to int or parameter parsing), you’ll find tons of examples.

1 Like

Thanks! :slight_smile:

You’re initializing the value of buffer as the address of an array of pointers of an array of pointers…well what I understand is that you are dynamically allocating memory in the heap that doesn’t even exist for memory in the heap that doesn’t exist (in this case *String) during compilation. You’re basically telling unreal engine hey allocate the address of this thing that doesn’t exist. That’s illegal and what you’re doing is criminal. And not only that you’re dynamically allocating memory in the heap for something that doesn’t really require you to dynamically allocate memory in the heap because you’re casting to const, typically you don’t really do this unless you want the array to grow/decrease past postcompilation<–>pretinitialization. So it’s like a paradox for the program in general, you want something that’s const but at the same time not const?

Instead of:

*buffer=*(const_cast<TCHAR*>(*String));

Try:
*buffer= new <TCHAR*>(String);

I could be wrong, on my solution though, I’m still a beginner, but when working with pointers you have to be really conscious of dynamic memory allocation management when dealing with arrays.

Here’s a snippet of a book I know that talks about this:

#include <iostream>
using namespace std;
int main()
{
cout << "How big?" << endl;
int size; // try and use a variable for size..
cin >> size; // get size from user
int array[ size ]; // get error: "unknown size"
}

^Bad

#include <iostream>
using namespace std;
int main()
{
 cout << "How big?" << endl;
int size; // try and use a variable for size..
cin >> size;
int *array = new int[ size ]; // this works
// fill the array and print
for( int index = 0; index < size; index++ )
{
array[ index ] = index * 2;
cout << array[ index ] << endl;
}
delete[] array; // must call delete[] on array allocated with
// new[]!
}

^Good

Hope this helps.