Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8957849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:05:09+00:00 2026-06-15T15:05:09+00:00

void StaticControl::addText(LPWSTR text) { lpCurrentText = (LPWSTR)malloc((GetWindowTextLength(hStatic) + 1) * sizeof(LPWSTR)); GetWindowText(hStatic, lpCurrentText, GetWindowTextLength(hStatic)

  • 0
void StaticControl::addText(LPWSTR text)
{
    lpCurrentText = (LPWSTR)malloc((GetWindowTextLength(hStatic) + 1) * sizeof(LPWSTR));
    GetWindowText(hStatic, lpCurrentText, GetWindowTextLength(hStatic) + 1);

    lpInput = text;
    chInput = (char*)malloc(sizeof (lpInput) *sizeof(LPWSTR)/sizeof(char*));
    chCurrent = (char*)malloc(sizeof (lpCurrentText) *sizeof(LPWSTR)/sizeof(char*));
    wcstombs(chCurrent, lpCurrentText, wcslen(lpCurrentText) + 1);
    wcstombs(chInput, lpInput, wcslen(lpInput) + 1);
    strcat(chCurrent, chInput);
    lpNewText = (LPWSTR)malloc(strlen(chCurrent) * sizeof(char*)); //this is where it crashes
    mbstowcs(lpNewText, chCurrent, strlen(chCurrent) + 1);
    SetWindowText(hStatic, lpNewText);
    return;
}
//where
HWND hStatic;
LPWSTR lpCurrentText;
LPWSTR lpInput;
LPWSTR lpNewText;
char*  chInput;
char*  chCurrent;

This code works fine adding text to the control until the string becomes around 20 characters long in which the program crashes. Stepping through the program, it crashes where I allocate memory for the lpNewText buffer. I don’t know what’s wrong. Visual Studio takes me to the malloc.h header when it crashes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:05:11+00:00Added an answer on June 15, 2026 at 3:05 pm

    For a start I’d recommend that you abandon malloc and use C++ memory allocation techniques. Like new, new[], std::vector<>, std::string, std::wstring etc.

    That said, I can see the following errors in your code:

    lpCurrentText = (LPWSTR)malloc((GetWindowTextLength(hStatic) + 1) * sizeof(LPWSTR));
    // should be sizeof(*lpCurrentText), i.e. size of a wide char 
    // and not size of a pointer as you have it
    
    chInput = (char*)malloc(sizeof (lpInput) *sizeof(LPWSTR)/sizeof(char*));
    // sizeof(lpInput), sizeof(LPWSTR) and sizeof(char*) are all equal to the
    // same thing, the size of a pointer
    
    chCurrent = (char*)malloc(sizeof (lpCurrentText) *sizeof(LPWSTR)/sizeof(char*));
    // as above
    
    lpNewText = (LPWSTR)malloc(strlen(chCurrent) * sizeof(char*)); 
    // sizeof(char*) is the size of a pointer, not what you want
    
    return;
    // rather pointless when the return type is void
    

    I don’t really know what your code is trying to do so I’m not going to try to re-write it and correct everything. The fundamental problem you have is that you systematically write sizeof(…) and calculate the size of a pointer when you actually want the size of a character element.

    Perhaps what you really need to do is throw away all this horrid code and use std::wstring to do your concatenation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

void foo(char *p) { int i; int len = strlen(p); p = malloc(sizeof(char)*len+2); p[0]
void* heap = malloc(100); char *c = heap; strcpy(c, Terence); printf(heap = %s\n, heap);
void someCode() { char memory[sizeof(Foo)]; void* place = memory; Foo* f = new(place) Foo();
void *p = malloc(1000); *((int*)p) = 666; *((int*)p+sizeof(int)) = 777; int i; for (i
void test() { unsigned char c; c = (~0)>>1 ; printf(c is %u\n,c); }
void say(char msg[]) { // using pointer to print out the first char of
void return_input (void) { char array[30]; gets (array); printf(%s\n, array); } After compiling it
void max_min(sqlite3 *db) { //call back********* int i, ncols; sqlite3_stmt *stmt; char *sql; const
void expand_combinations(const char *remaining_string, string const & s, int rema in_depth) { if(remain_depth==0) {
void reverse_string(char* string, int str_size) { char tmp; int i = 0; int j

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.