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

  • Home
  • SEARCH
  • 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 8611783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:27:33+00:00 2026-06-12T04:27:33+00:00

The code below crashes when uncommented and it seems that shared_array<> argument in get()

  • 0

The code below crashes when uncommented and it seems that shared_array<> argument in get() is problematic.

print() doesn’t seem to crash at least for now…

What is the correct way to pass shared_array<> arguments?

#include <iostream>
#include <cstring>
#include <boost/shared_array.hpp>
using namespace std;
using namespace boost;

shared_array<wchar_t> get(const wchar_t* s) {
//shared_array<wchar_t> get(const shared_array<wchar_t>& s) {

    size_t size = wcslen(s);
    //size_t size = wcslen(s.get());

    shared_array<wchar_t> text(new wchar_t[size+1]);

    wcsncpy(text.get(), s, size+1);
    //wcsncpy(text.get(), s.get(), size+1);

    return text;
}

void print(shared_array<wchar_t> text) {
    wcout << text.get() << endl;
}

int wmain(int argc, wchar_t *argv[]) {
    //shared_array<wchar_t> param(argv[1]);

    shared_array<wchar_t> text = get(argv[1]);
    //shared_array<wchar_t> text = get(param);

    print(text);
    //print(text.get()); 
}

Edit:
Thanks. So the key point here is that I should always use only new/new[] when using boost::shared_ptr/array.

The main function fixed:

int wmain(int argc, wchar_t *argv[]) {
    size_t szArg = wcslen(argv[1]);
    wchar_t* paramBuf = new wchar_t[szArg+1];
    wcscpy_s(paramBuf, szArg+1, argv[1]);
    shared_array<wchar_t> param(paramBuf);

    shared_array<wchar_t> text = get(param);

    print(text);
}

Actually at first I allocated paramBuf in the stack so I couldn’t find the mistake.

WRONG:    
int wmain(...) {
    wchar_t paramBuf[100];
    wcscpy_s(paramBuf, 100, argv[1]);
    ...
}
  • 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-12T04:27:35+00:00Added an answer on June 12, 2026 at 4:27 am

    The problem is at the line:

    shared_array<wchar_t> param(argv[1]);
    

    shared_array requires to be initialized with a pointer to an array allocated with new[] but argv[1] is just an c-string, so when it goes out of scope (the variable param that is) the shared_array’s destructor calls delete[] on argv[1] which is not allowed.

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

Sidebar

Related Questions

The code below simply didn't work. document.getElementById('files').addEventListener('change', handleFileSelect, false); reported by firebug that this
my app crashes with a null pointer exception on the code below. i have
Cant please someone tell me why the code below crashes when executing, (compilation goes
The code below compiles, but immediately crashes for reasons obvious to others, but not
OK, so this code below sometimes crashes on the line below //bug!! The error
Somehow the code below crashes my app: NSString *filename = [NSString stringWithFormat:@%@%@, dbPath, @BAR];
Possible Duplicate: Objective C alloc/release error When I try the code below it crashes
The code below works fine in iOS 4 and 5 but crashes in iOS
For the code below if I nillify the indicator by self it crashes if
I have the following code below that is meant to change a class var

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.