Can anyone tell me what’s wrong with this code?

Asked

Viewed 68 times

2

I am creating a function that receives and breaks a string into several, depending on the delimiter(s) chosen by the programmer. So I have:

void split(const wchar_t* text, const wchar_t* seps,wchar_t ***str, int *count)
  • text: the string to be broken
  • seps: the delimitators
  • str: string array (return)
  • Count: number of broken strings

The function seems to work without any problem and no memory Leak. The problem is that when using the text output function (in the X11 window), it appears with random colors. I’ve erased the entire body of the function, but the problem still persists.

The code is like this:

void split(const wchar_t* text, const wchar_t* seps,wchar_t ***str, int *count){
    //nota que o corpo da função está vazio
}

void ShowWindow(const char* title, const wchar_t* text)
{
    Display* dpy = NULL;
    Window win;

    wchar_t** text_splitted;
    int textLines;

    setlocale(LC_ALL,"");

    split(text,L"\n" , &text_splitted, &textLines);
    ...
}

It is something very strange, because the function is empty. And when I do not call it, the text appears with the black color, which is the normal.

The complete code.

Note: Colors do not appear when compilation the code on Ubuntu, but when compilation in Debian appear.

  • 1

    Not a X11 problem? How are you printing, on output? It can be a TTY type configuration with ANSI

  • I have no idea if X11 is a problem or not. Check this out https://pasteboard.co/HocpngM.png

  • You also comment on the code to debug the result? for(i = 0; i < textLines; i++) { wprintf(L"%ls\n", text_splitted[i]); } fflush(stdout);?

  • Yes. I just forgot to add in gist too.

  • These graphics capable terminals support color escapement characters, screen positioning, sparkling (flashing) characters... Could be, at some point in the code, some character like this is coming out.

  • I already managed to solve the problem. I was declaring the variables in the middle of the function ShowWindow. In c variables must be declared at the beginning of a code block. (at least I think that was it)

  • its problem is not to pass variables without initializing and not where they were declared?

  • It is not that. Even because the body of the function is empty.

Show 3 more comments

1 answer

0

The problem is in the gcValues variable. On line 84, before calling the Xcreategc function, you should initialize the foreground member variable. The right thing would be:

gcValues.font = XLoadFont(dpy, "7x13");
gcValues.foreground = BlackPixel(dpy, 0);
gc = XCreateGC(dpy, win, GCFont + GCForeground, &gcValues);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.