-
October 31st, 2013, 02:54 PM
#1
[RESOLVED] error on a code
Code:
#include <process.h>
#include <string>
#include <memory.h>
#include <windows.h>
using namespace std;
struct Blink
{
string Text;
int x;
int y;
int TextColor;
int BackColor;
};
unsigned __stdcall BlinkLoop(void *params)
{
Blink *p = static_cast<Blink *>(params);
char *EmptyText;
EmptyText=new char[ p->Text.length() ] ;
int i;
for( i=0;i<=(int)p->Text.length();i++)
{
EmptyText[i] = ' ';
}
EmptyText[i] ='\0';
HDC hDC=GetDC(GetConsoleWindow());
SetTextColor(hDC,RGB(0,255,0));
SetBkMode(hDC,TRANSPARENT);
while (true)
{
TextOut(hDC,p->x,p->y,p->Text.c_str(),(int)p->Text.length()+1);
Sleep(1000);
TextOut(hDC,p->x,p->y,EmptyText,(int)p->Text.length()+1);
Sleep(500);
}
return 0;
}
void TextBlink(string Text, int x, int y, int TextColor, int BackColor)
{
Blink *b;
b->Text=Text;
b->BackColor=BackColor;
b->TextColor=TextColor;
b->x=x;
b->y =y;
_beginthreadex(NULL, 0, BlinkLoop ,&b, 0, NULL);
}
i have 1(at least) error on thses code, but the compiler don't found it
only the windows "the program stops to working" and then close it.
can anyone advice me?
-
October 31st, 2013, 03:15 PM
#2
-
October 31st, 2013, 03:21 PM
#3
Re: error on a code
Originally Posted by GCDEF
Run it in the debugger.
it's tell me something about:
Code:
void TextBlink(string text, int x, int y, int TextColor, int BackColor)
{
Blink *b;
b->Text=text;
what it's that error?
i think that it's correct
"Program received signal SIGSEGV, Segmentation fault." is these message that i get
-
October 31st, 2013, 03:25 PM
#4
Re: error on a code
In that code, b is unitialized.
-
October 31st, 2013, 03:27 PM
#5
Re: error on a code
Originally Posted by GCDEF
In that code, b is unitialized.
i did now to NULL, but i get the same problem
-
October 31st, 2013, 03:33 PM
#6
Re: error on a code
Where are you allocating memory for b as b is a pointer?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 31st, 2013, 03:34 PM
#7
Re: error on a code
Originally Posted by Cambalinho
i did now to NULL, but i get the same problem
You can't dereference a null pointer. It has to point to valid memory.
-
October 31st, 2013, 03:37 PM
#8
Re: error on a code
the b is these struct:
Code:
struct Blink
{
string Text;
int x;
int y;
int TextColor;
int BackColor;
};
how i call the function:
Code:
TextBlink("Hello world", 20,20,3,3);
and heres the function:
Code:
void TextBlink(string text, int x, int y, int TextColor, int BackColor)
{
Blink *b;
b->Text=text;
b->BackColor=BackColor;
b->TextColor=TextColor;
b->x=x;
b->y =y;
_beginthreadex(NULL, 0, BlinkLoop ,&b, 0, NULL);
}
so what is the problem?
-
October 31st, 2013, 03:39 PM
#9
Re: error on a code
How many times do you need to hear it? b is uninitialized. You do know b is a pointer, right and that pointers need to point to something before you can dereference them?
-
October 31st, 2013, 03:42 PM
#10
Re: error on a code
Originally Posted by GCDEF
How many times do you need to hear it? b is uninitialized. You do know b is a pointer, right and that pointers need to point to something before you can dereference them?
to a pointer must be initializated to NULL, when we don't know the value\reference, right?
-
October 31st, 2013, 03:44 PM
#11
Re: error on a code
Originally Posted by Cambalinho
to a pointer must be initializated to NULL, when we don't know the value\reference, right?
It's a good idea, but you have to assign it a value before you try and access what it points to. As you're finding out, dereferencing a pointer that points to NULL, or an address that doesn't belong to your app will cause a crash. You're not point to a b object, yet you're using it as if it does.
-
October 31st, 2013, 03:55 PM
#12
Re: error on a code
Originally Posted by GCDEF
It's a good idea, but you have to assign it a value before you try and access what it points to. As you're finding out, dereferencing a pointer that points to NULL, or an address that doesn't belong to your app will cause a crash. You're not point to a b object, yet you're using it as if it does.
you have right... i must apoint it to new type:
Code:
Blink *b=new Blink;
now i get the same error here:
Code:
unsigned __stdcall BlinkLoop(void *params)
{
Blink *p = static_cast<Blink *>(params);
char *EmptyText=(char*)p->Text.c_str();
int i;
for( i=0;i<(int)p->Text.length();i++)
the pointer is inicializated. but what isn't right?
-
October 31st, 2013, 04:20 PM
#13
Re: error on a code
Code:
_beginthreadex(NULL, 0, BlinkLoop ,b, 0, NULL);
b is already a pointer, you were passing the address of a pointer!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
October 31st, 2013, 04:41 PM
#14
Re: error on a code
thanks for all to all
i have another error
Code:
char *EmptyText;
EmptyText=new char[ p->Text.length()+1] ;
int i;
for( i=0;i<=(int)p->Text.length();i++)
{
EmptyText[i] = ' ';
}
EmptyText[i+1] ='\0';
these code is for give EmptyText the empty spaces of p->Text size.
(transform any character to ' '(space))
but by some reason the loop don't work with all string size
can anyone advice me?
-
October 31st, 2013, 05:36 PM
#15
Re: error on a code
Originally Posted by Cambalinho
thanks for all to all
i have another error
Code:
char *EmptyText;
EmptyText=new char[ p->Text.length()+1] ;
int i;
for( i=0;i<=(int)p->Text.length();i++)
{
EmptyText[i] = ' ';
}
EmptyText[i+1] ='\0';
these code is for give EmptyText the empty spaces of p->Text size.
(transform any character to ' '(space))
but by some reason the loop don't work with all string size
can anyone advice me?
Good grief. Arrays are 0 based. Say Text = "dog". Text.length will return 3. EmptyText will therefore contain 4 bytes.
When you loop from 0 through Text.length(), you're trying to access the fourth character of Text, which doesn't exist. Further, at the end of your loop, i will have the value 4. You then try to set EmptyText[i + 1], which is EmptyTex[5], but the highest valid index of EmptyText is 3.
Whatever book you're learning from, go reread it. You're a little rusty on the basics.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|