[RESOLVED] error on a code
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20

Thread: [RESOLVED] error on a code

Hybrid View

  1. #1
    Join Date
    Apr 2009
    Posts
    1,121

    [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?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,495

    Re: error on a code

    Run it in the debugger.

  3. #3
    Join Date
    Apr 2009
    Posts
    1,121

    Re: error on a code

    Quote Originally Posted by GCDEF View Post
    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

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,495

    Re: error on a code

    In that code, b is unitialized.

  5. #5
    Join Date
    Apr 2009
    Posts
    1,121

    Re: error on a code

    Quote Originally Posted by GCDEF View Post
    In that code, b is unitialized.
    i did now to NULL, but i get the same problem

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,495

    Re: error on a code

    Quote Originally Posted by Cambalinho View Post
    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.

  7. #7
    Join Date
    Apr 2009
    Posts
    1,121

    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?

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,113

    Re: error on a code

    Code:
    Blink *b;
    Where are you allocating memory for b as b is a pointer?
    All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. This post is public domain as specified at https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,495

    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?

  10. #10
    Join Date
    Apr 2009
    Posts
    1,121

    Re: error on a code

    Quote Originally Posted by GCDEF View Post
    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?

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,495

    Re: error on a code

    Quote Originally Posted by Cambalinho View Post
    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.

  12. #12
    Join Date
    Apr 2009
    Posts
    1,121

    Re: error on a code

    Quote Originally Posted by GCDEF View Post
    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?

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    5,113

    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. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. This post is public domain as specified at https://creativecommons.org/publicdomain/zero/1.0/

    C, C++ Compiler: Microsoft VS2017

  14. #14
    Join Date
    Apr 2009
    Posts
    1,121

    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?

  15. #15
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,495

    Re: error on a code

    Quote Originally Posted by Cambalinho View Post
    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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center