0 registered members (),
950
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
[SOLVED] Detecting invalid pointer
#480570
06/16/20 17:23
06/16/20 17:23
|
Joined: Sep 2009
Posts: 993 Budapest
Aku_Aku
OP
User
|
OP
User
Joined: Sep 2009
Posts: 993
Budapest
|
Hi! My program creates a TEXT object like this: 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: 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,370 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
Hi! 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. 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
#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!
|
|
|
Re: Detecting invalid pointer
[Re: Aku_Aku]
#480579
06/17/20 09:00
06/17/20 09:00
|
Joined: Sep 2009
Posts: 993 Budapest
Aku_Aku
OP
User
|
OP
User
Joined: Sep 2009
Posts: 993
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. 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. 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,370 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
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)
#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: 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. Greets!
|
|
|
Re: Detecting invalid pointer
[Re: 3run]
#480590
06/17/20 12:27
06/17/20 12:27
|
Joined: Sep 2009
Posts: 993 Budapest
Aku_Aku
OP
User
|
OP
User
Joined: Sep 2009
Posts: 993
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: Here is a short demonstration of ...... Thirdly, please enlightmen me what is wrong with that condition?
|
|
|
Re: Detecting invalid pointer
[Re: Aku_Aku]
#480592
06/17/20 13:51
06/17/20 13:51
|
Joined: Jul 2010
Posts: 283 Germany
jenGs
Member
|
Member
Joined: Jul 2010
Posts: 283
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.
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,370 Caucasus
3run
Senior Expert
|
Senior Expert
Joined: May 2009
Posts: 5,370
Caucasus
|
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? Edit2: well, ok... It seems a little weird, take a look at this example:
#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... 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.
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.
|
|
|
Re: Detecting invalid pointer
[Re: 3run]
#480596
06/17/20 14:46
06/17/20 14:46
|
Joined: Sep 2009
Posts: 993 Budapest
Aku_Aku
OP
User
|
OP
User
Joined: Sep 2009
Posts: 993
Budapest
|
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. Why would you even need to try to copy empty strings into another one? 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 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
|
|
|
|