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 6013037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:29:25+00:00 2026-05-23T02:29:25+00:00

I am working on some C++ code and am having some problems with the

  • 0

I am working on some C++ code and am having some problems with the function described below. I haven’t used much C++ before, at least not for a long time and so i’m trying to learn as I go along to a large extent. The win32api doesn’t help much with the confusion factor either…

The function is succesfully called twice, before failing when called at a later stage when it is called in the application.

PTSTR getDomainFromDN(PTSTR dnPtstr) {

size_t nDn=wcslen(dnPtstr);
size_t *pnNumCharConverted = new size_t;

wchar_t *szTemp = new wchar_t[10];          // for debugging purposes
_itow_s((int)nDn,szTemp,10,10);             // for debugging purposes

AddToMessageLog(EVENTLOG_ERROR_TYPE,szTemp);        // for debugging purposes (displays an integer value before failing)
AddToMessageLog(EVENTLOG_ERROR_TYPE,TEXT("Marker A"));  // for debugging purposes
char *dn = new char[nDn];
    // !!!!!!!!!!!! all goes wrong here, doesn't get to next line, nDn does have a value when it fails (61)
AddToMessageLog(EVENTLOG_ERROR_TYPE,TEXT("Marker B"));  // for debugging purposes

wcstombs_s(pnNumCharConverted,dn,nDn+1,dnPtstr,nDn+1);

...more code here...

delete[] dn;
delete pnNumCharConverted;

return result
}

At first i thought it was a memory allocation problem or something as it fails on the line char *dn = new char[nDn];, the last marker showing as ‘Marker A’. I used delete[] on the pointer further down to no avail. I know that nDn is a value because I print this out to a message log using _itow_s for debugging. I also know that dnPtrstr is a PTSTR.

I tried using malloc as well with free() in the old C style but this doesn’t improve things.

  • 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-05-23T02:29:26+00:00Added an answer on May 23, 2026 at 2:29 am

    I tried sanitizing your code a bit. One of the big tricks to C++ is to not explicitly use memory management when it can be avoided. Use vectors instead of raw arrays. Strings instead of char pointers.

    And don’t unnecessarily allocate objects dynamically. Put them on the stack, where they’re automatically freed.

    And, as in every other language, initialize your variables.

    PTSTR getDomainFromDN(PTSTR dnPtstr) {
        std::wstring someUnknownString = dnPtstr;
    
        size_t numCharConverted = 0;
    
        std::wstring temp; // for debugging purposes
        std::ostringstream sstr;
        sstr << temp;
        AddToMessageLog(EVENTLOG_ERROR_TYPE,sstr.str().c_str());        // for debugging purposes (displays an integer value before failing)
    
        AddToMessageLog(EVENTLOG_ERROR_TYPE,TEXT("Marker A"));  // for debugging purposes
        std::vector<char> dn(someUnknownString.size());
    
        AddToMessageLog(EVENTLOG_ERROR_TYPE,TEXT("Marker B"));  // for debugging purposes
    
        wcstombs_s(&numCharConverted, &dn[0], dn.size(), someUnknownString.c_str(), dn.size());
    
        ...more code here...
    
        return result
    }
    

    This might not have solved your problem, but it has eliminated a large number of potential errors.
    Given that I can’t reproduce your problem from the code you’ve supplied, this is really the best I can do.

    Now, if you could come up with sane names instead of dnPtstr and dn, it might actually be nearly readable. 😉

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

Sidebar

Related Questions

I'm working with some code (not mine I hasten to add, I don't trust
I am unfortunately having some more problems with JQuery of late. I am working
I am writing the memset function and my code is below, I am having
I'm having some slight problems with fading one div into another, here's my code
I am having problems with an AJAX function I am working on. The function
I'm working on some code to colorize an image in Java. Basically what I'd
I'm working on some code that uses the System.Diagnostics.Trace class and I'm wondering how
I'm working on some code for a loosely coupled cluster. To achieve optimal performance
I'm working with some code that is confusing me and I'm wondering if I'm
I was working on some code recently and came across a method that had

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.