Using wmemset on wchar_t arrays...

SephirothSephiroth Fayetteville, NC, USA
Well, how is it done? If I declare an array in ANSI I can use the old memset function on it, but if I create a wchar_t array I cannot use wmemset on it.
[code]
//Old ANSI style that works
char *pBuffer[1024];
memset(pBuffer, 0, sizeof(pBuffer));

//New UNICODE style that won't work or even compile
wchar_t *pBuffer[1024];
wmemset(pBuffer, 0, 1024);
[/code]
So how can I zero our a buffer in UNICODE?

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • : Well, how is it done? If I declare an array in ANSI I can use the
    : old memset function on it, but if I create a wchar_t array I cannot
    : use wmemset on it.
    : [code]:
    : //Old ANSI style that works
    : char *pBuffer[1024];
    : memset(pBuffer, 0, sizeof(pBuffer));
    :
    : //New UNICODE style that won't work or even compile
    : wchar_t *pBuffer[1024];
    : wmemset(pBuffer, 0, 1024);
    : [/code]:
    : So how can I zero our a buffer in UNICODE?
    :
    : -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/gre
    : en][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h
    : [/red][/b][/italic]

    wmemset is used for setting an array of wchar_t, with wchar_t strings in mind. You're trying to set an array of strings.
    The proper way to do this is:
    [code]
    wchar_t *pBuffer[1024];
    memset(pBuffer, 0, 1024 * sizeof(wchar_t*));
    [/code]
    Or, since it's statically allocated:
    [code]
    memset(pBuffer, 0, sizeof(pBuffer));
    [/code]


    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • SephirothSephiroth Fayetteville, NC, USA
    : wmemset is used for setting an array of wchar_t, with wchar_t
    : strings in mind. You're trying to set an array of strings.
    : The proper way to do this is:
    : [code]:
    : wchar_t *pBuffer[1024];
    : memset(pBuffer, 0, 1024 * sizeof(wchar_t*));
    : [/code]:
    : Or, since it's statically allocated:
    : [code]:
    : memset(pBuffer, 0, sizeof(pBuffer));
    : [/code]:
    :
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry
    I thought that and tried memset, but it fails due to being wchar_t instead of char. Not only that, but memset would require a formula like "(sizeof(wchar_t) * 1024)" in the last parameter to work properly. Still, memset won't work.

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
  • : I thought that and tried memset, but it fails due to being wchar_t
    : instead of char. Not only that, but memset would require a formula
    : like "(sizeof(wchar_t) * 1024)" in the last parameter to work
    : properly. Still, memset won't work.
    :

    I don't fully understand your problem. How is it not working? Does it crash, or does it deliver unexpected results (or does it refuse to compile)?

    The following code:
    [code]
    wchar_t* szSomething[4];
    memset(szSomething, 0, sizeof(szSomething));
    [/code]
    Works for me and sets each entry of of szSomething to (wchar_t*)0.

    You should realise that memset and wmemset are totally different functions. wmemset is not the wide equivalent of memset.
    In fact, memset is a generic memory handling function that takes a pointer to any memory and sets Count bytes to Val.
    wmemset is a function that takes a wchar_t array and sets Count characters to Val.

    The side effect of memset is that it also functions perfectly on single-byte strings, because it sets values per byte. With the introduction of wide strings however, a function was needed that sets each word to a specific value.

    So: memset is general function for setting memory; works well on ascii strings
    memsetw is specifically designed to operate on unicode strings, where memset fails; with a little parameter-casting, it can be used as a wide memset for any memory.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • SephirothSephiroth Fayetteville, NC, USA
    : : I thought that and tried memset, but it fails due to being wchar_t
    : : instead of char. Not only that, but memset would require a formula
    : : like "(sizeof(wchar_t) * 1024)" in the last parameter to work
    : : properly. Still, memset won't work.
    : :
    :
    : I don't fully understand your problem. How is it not working? Does
    : it crash, or does it deliver unexpected results (or does it refuse
    : to compile)?
    :
    : The following code:
    : [code]:
    : wchar_t* szSomething[4];
    : memset(szSomething, 0, sizeof(szSomething));
    : [/code]:
    : Works for me and sets each entry of of szSomething to (wchar_t*)0.
    :
    : You should realise that memset and wmemset are totally different
    : functions. wmemset is not the wide equivalent of memset.
    : In fact, memset is a generic memory handling function that takes a
    : pointer to any memory and sets Count bytes to Val.
    : wmemset is a function that takes a wchar_t array and sets Count
    : characters to Val.
    :
    : The side effect of memset is that it also functions perfectly on
    : single-byte strings, because it sets values per byte. With the
    : introduction of wide strings however, a function was needed that
    : sets each word to a specific value.
    :
    : So: memset is general function for setting memory; works well on
    : ascii strings
    : memsetw is specifically designed to operate on unicode strings,
    : where memset fails; with a little parameter-casting, it can be used
    : as a wide memset for any memory.
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry
    AH I just noticed I was calling memset wrong. I was exhausted when I started coding in the zeroing methods and was attempting to use memset like wmemset after wmemset failed. As soon as I opened the source and glanced at it I saw the issue. That's what I get for working late!

    -[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories