Gamestudio Links
Zorro Links
Newest Posts
Trouble getting data with IB Gateway
by richs. 07/16/20 03:12
Futures with IB
by jbhunter. 07/16/20 01:08
A9
by 3run. 07/15/20 19:07
Maximum Value
by AndrewAMD. 07/15/20 12:50
What are you working on?
by NeoDumont. 07/14/20 23:06
Engine window and DPI scaling
by Dooley. 07/14/20 20:27
AUM Magazine
Latest Screens
The Space Between
Pogostuck: Rage With Your Friends
Worst Case Z
AckCon'18 - Lotter vs the World 2 - Preview Release
Who's Online Now
10 registered members (Michael_Schwarz, AndrewAMD, Quad, 20BN, onoff, richs, kvm, JeyKey II, FLD, Nymphodora), 382 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Estripa, nowater, dasanc, DavidFdez, TraderKevin
18478 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
[SOLVED] Detecting invalid pointer #480570
06/16/20 17:23
06/16/20 17:23
Joined: Sep 2009
Posts: 943
Budapest
Aku_Aku Offline OP
User
Aku_Aku  Offline OP
User

Joined: Sep 2009
Posts: 943
Budapest
Hi!
My program creates a TEXT object like this:
Code
	TEXT* text_lines;
	text_lines = txt_create(100,0);

In one case the object doesn't get strings, it remains in original state.
But, in this case when the control flow reaches this statement:
Code
if(NULL == (text_lines->pstring)[line_number])

I get W1501 Invalid pointer message.
I assume a pointer is uninitialized. How can I detect this case?

Last edited by Aku_Aku; 06/17/20 09:01.
Re: Detecting invalid pointer [Re: Aku_Aku] #480577
06/17/20 08:07
06/17/20 08:07
Joined: May 2009
Posts: 5,276
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,276
Caucasus
Hi!

Quote
Warning W1501: Empty Pointer

You are using a pointer that was not initialized or does not point to any valid object. Typical beginner's mistakes are using an entity pointer before it was set up in another action, or after it became invalid due to removing the entity.

Originally Posted by Aku_Aku
Code
if(NULL == (text_lines->pstring)[line_number])
I can't really imagine what could cause that message... Maybe your 'line_numer' is out of the range?
txt_create automatically creates empty strings, so if you are in range (0...99) it should be no empty pointer message.

Here how it works for me
Code
#define PRAGMA_POINTER

void main()
{
	TEXT *text_lines = txt_create(100, 0);
	
	var line_number = 0;
	str_cpy((text_lines->pstring)[line_number], "Hello world");
	
	while(!key_esc)
	{
		draw_text((text_lines->pstring)[line_number], 10, 10, COLOR_WHITE);
		
		// check if second string is empty ?
		if(str_len((text_lines->pstring)[1]) <= 0)
		{
			draw_text("Empty string!", 10, 30, COLOR_WHITE);
		}
		
		wait(1);
	}
}


Greets!


Looking for free stuff?? Take a look here: http://badcom.at.ua
Re: Detecting invalid pointer [Re: Aku_Aku] #480579
06/17/20 09:00
06/17/20 09:00
Joined: Sep 2009
Posts: 943
Budapest
Aku_Aku Offline OP
User
Aku_Aku  Offline OP
User

Joined: Sep 2009
Posts: 943
Budapest
@3run: Thanks for the goodwill to help me. Unfortunately, the situation is something else as you thought.
First of all, two things:
1. No need to use #define PRAGMA_POINTER
2. Although the A8 documentation says: "Empty strings are automatically created." it not helps. You will got the error message.

Here is a short demonstration of how to get W1501 Invalid pointer message. (with 'automatically created strings')
Run this, and you can see the message. Naturally, you don't need to fill the commented part with live code. Popping up the message will work without that.
Code
function main() {
	STRING* actual_line = str_create("");
	TEXT* text_lines;
	text_lines = txt_create(1,0);
	int line_number = 0;
/*
here goes your code that fills the text_lines...
*/
	str_cpy(actual_line, (text_lines->pstring)[line_number]);
	printf("We have valid empty text_line");
}


And, here is a short demonstration of how to get rid of W1501 Invalid pointer message.
Now, you must somehow fill the commented code part with: filling the text lines with strings and accordingly to it to update the correct value of the text_lines->strings.
Code
function main() {
	STRING* actual_line = str_create("");
	TEXT* text_lines;
	text_lines = txt_create(1,0);
	int line_number = 0;
	text_lines.strings = 0;
/*
here goes your code that fills the text_lines...
*/
	if (0 < text_lines->strings && line_number < text_lines->strings) {
		str_cpy(actual_line, (text_lines->pstring)[line_number]);
		printf("We have valid empty/or filled text_line");
	}
}

So, the key of the solution is to set the proper value in text_lines->strings.

Re: Detecting invalid pointer [Re: Aku_Aku] #480583
06/17/20 09:42
06/17/20 09:42
Joined: May 2009
Posts: 5,276
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,276
Caucasus
Originally Posted by Aku_Aku
1. No need to use #define PRAGMA_POINTER
First of all I highly doubt that, of course, if you don't want lite-c to mess around with pointers and shit.
It's better to code without shitty pointer autodetection, anyone will tell you this.

Secondly, why are you trying to copy an empty string into another string?? txt_create creates an empty string (""), that's causing the W1501 error.
Here is working example (yes with PRAGMA_POINTER, not because it helps to fix the problem, but because it prevents lite-c from doing it's "magic" under the certains)
Code
#define PRAGMA_POINTER

void main()
{
	STRING *actual_line = "";
	TEXT *text_lines = txt_create(100, 0);
	int line_number = 0;
	
	int i = 0; 
	for(i = 0; i < 100; i++)
	{
		str_cpy((text_lines->pstring)[i], " "); // 1 space
	}
	
	str_cpy(actual_line, (text_lines->pstring)[line_number]);
	str_cat(actual_line, "add text for testing"); // this is only for testing, so you can see that 1 space from text_lines is there
	
	while(!key_esc)
	{
		draw_text(actual_line, 10, 10, COLOR_WHITE);
		draw_text("example text", 10, 30, COLOR_WHITE);
		wait(1);
	}
}
Also, just friendly advice, don't do stuff like this, if you want anyone to read your code:
Code
if (0 < text_lines->strings && line_number < text_lines->strings)
Read about code convention, C language coding style, etc. You'll get your hands beaten with a stick if you do this in a big team or a company. grin

Greets!


Looking for free stuff?? Take a look here: http://badcom.at.ua
Re: Detecting invalid pointer [Re: 3run] #480590
06/17/20 12:27
06/17/20 12:27
Joined: Sep 2009
Posts: 943
Budapest
Aku_Aku Offline OP
User
Aku_Aku  Offline OP
User

Joined: Sep 2009
Posts: 943
Budapest
I am sorry, but you didn't understand, my codes are just examples.
So, don't need the PRAGMA_POINTER. Especially I want the engine recognizes my pointers as the documentation stated. (What shit did you write about?)
Secondly, I am trying to copy an empty string into another string, because my code is an example.
As I stated, in my last post:
Quote
Here is a short demonstration of ......

Thirdly, please enlightmen me what is wrong with that condition? smile

Re: Detecting invalid pointer [Re: Aku_Aku] #480592
06/17/20 13:51
06/17/20 13:51
Joined: Jul 2010
Posts: 282
Germany
J
jenGs Online
Member
jenGs  Online
Member
J

Joined: Jul 2010
Posts: 282
Germany
when you start with the static value like "if ( 0 < text.strings) it is not very readable. if(text.strings > 0) is much more clear, because we tend to read code from left to right.
Code
if (text_lines->strings > 0 && text_lines->strings >  line_number )


Concerning an invalid pointer in your original example: If you do (text.>pstring)[line_number] you can get out of bounds. It is always better to store the string number somewhere or rely on text.strings like you said and check that in your if too. Because if your array is out of bounds it is not NULL it accesses the next memoryblock and compares NULL == [messy messy pointer after array memory block]

Re: Detecting invalid pointer [Re: Aku_Aku] #480593
06/17/20 14:13
06/17/20 14:13
Joined: May 2009
Posts: 5,276
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,276
Caucasus
Originally Posted by Aku_Aku
I am trying to copy an empty string into another string, because my code is an example.
Thats the reason you get the error message. Give another example, when text_lines isn't empty and you try to copy it into another string.
If you want to check if it's null or not, compare the string to the null ('\0' - that's how it's done in C, maybe it will work with Lite-C too).

Edit: comparing it to null won't work, simply compare it to "" if you want to check, if it's empty or not (str_cmp(test_str, "")).
Why would you even need to try to copy empty strings into another one? confused

Edit2: well, ok... It seems a little weird, take a look at this example:
Code

#define PRAGMA_POINTER

void main()
{
	fps_max = 60;
	warn_level = 6;
	
	STRING *test_str = "";
	
	TEXT *text_lines = txt_create(1, 0);
	//str_cpy((text_lines->pstring)[0], ""); // uncomment this to solve the W1501
	
	while(!key_esc)
	{
		var res = str_cmp(test_str, (text_lines->pstring)[0]);
		DEBUG_VAR(res, 0);
		wait(1);
	}
}
After txt_create (text_lines->string)[0] should be "" at the start, but it seems that it's not... confused Maybe it's something beyond my knowledge, or maybe it's related to acknex itself... So I would set all text strings to "" by hand in a for loop, before doing anything with them, in order to avoid getting W1501 error. This is indeed weird.
Code
int i = 0;
for(i = 0; i < text_lines->strings; i++)
{
	if((text_lines->pstring)[i])
	{
		str_cpy((text_lines->pstring)[i], "");
	}
}


Greets.

Last edited by 3run; 06/17/20 14:31.

Looking for free stuff?? Take a look here: http://badcom.at.ua
Re: Detecting invalid pointer [Re: jenGs] #480595
06/17/20 14:28
06/17/20 14:28
Joined: Sep 2009
Posts: 943
Budapest
Aku_Aku Offline OP
User
Aku_Aku  Offline OP
User

Joined: Sep 2009
Posts: 943
Budapest
@jenGs: "if(text.strings > 0) is much more clear, because we tend to read code from left to right." I accept it, it is true. The reason why I used reversed order is the danger of the accidentaly value assign like if(variable = 0). It can be avoid with this if(0 = variable).
Concerning the original example, please notice line_number is 0. And when I create a text with 100 strings that must to have 100 string. The very first string is indexded with 0. Exactly equal with line_number that is also 0. So, there is no reason to occur an out of bounds error. I thought my really short example can be understanding by anyone easily, maybe i was wrong. So, i don't reply to the rest of your post. No offense, just my post isn't about out of bound. Peace smile

Re: Detecting invalid pointer [Re: 3run] #480596
06/17/20 14:46
06/17/20 14:46
Joined: Sep 2009
Posts: 943
Budapest
Aku_Aku Offline OP
User
Aku_Aku  Offline OP
User

Joined: Sep 2009
Posts: 943
Budapest
Originally Posted by 3run
Originally Posted by Aku_Aku
I am trying to copy an empty string into another string, because my code is an example.
Thats the reason you get the error message. Give another example, when text_lines isn't empty and you try to copy it into another string.

No. You didn't catch the original thought. That doesn't matter if i copy an empty string. The situation also is the same with filled string. I gave a code, the only thing needs to you to try and you will see.

I don't want to debate about the usage of the PRAGMA_POINTER, it does not change the situation, please notice it. So it is senseless to come up with it again.
I must to tell the truth, I know how can check a pointer or compare. What I don't know is how decide the A8 engine is my pointer is invalid or not not.

And I must to repeat and underline: I try to use a TEXT object somewhere in the deep of my code. And in only one case when the object was only call to life, (and no more activity, it is the important part in the story) referencing to an string of that with index will result W1501 message. Please consider this and the content and sense of my post. Nothing else, if it possible.

Originally Posted by 3run
Why would you even need to try to copy empty strings into another one? confused

Because it is an example. I could copy a two KB long string, the situation would not change.
I can give a more detailed case description, if it needs.
Greet, Aku smile

Edit: Sorry the scrolling wheel of my mouse doesn't work properly. So now I can see you caught the issue finally.

Last edited by Aku_Aku; 06/17/20 15:00. Reason: my mouse
Re: Detecting invalid pointer [Re: Aku_Aku] #480597
06/17/20 14:58
06/17/20 14:58
Joined: May 2009
Posts: 5,276
Caucasus
3run Offline
Senior Expert
3run  Offline
Senior Expert

Joined: May 2009
Posts: 5,276
Caucasus
Check my previous message, I've edited it 2 times. If that doesn't help you out, then I don't know. To be honest, I think we are facing a language barrier, because I sometimes don't understand what you are saying. Also my examples contain PRAGMA_POINTER, because I use it daily, not because it should solve the problem (read my previous messages about that).

Greets!


Looking for free stuff?? Take a look here: http://badcom.at.ua
Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1