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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:04:23+00:00 2026-06-07T01:04:23+00:00

In the following codes I try to build a 2D array with c++, but

  • 0

In the following codes I try to build a 2D array with c++, but when I run this program it fails.

#include <iostream>
#include <vector>
using namespace std;

int obtain_options(  char ** optionLine)
{
   vector< char*> options;
   options.push_back("abc");
   options.push_back("def");


   std::copy(options.begin(), options.end(), const_cast< char**>(optionLine));

   return options.size();
}

int main(int ac, char* av[])
{
    char** optionLine;
    int len;
    optionLine = new char* [2];
    for (int i= 0; i<2; i++)
    {
       optionLine[i] = new char [200];
    }
     obtain_options(optionLine);
    for (int i=0; i<2; i++)
    {
        cout<<optionLine[i]<<endl;
     }

    for (int i=0; i<2; i++)
        delete  [] (optionLine[i]);
    delete []optionLine;

   return 0;

} 

I understand there are some problems with allocating memory to optionLine in function obtain_options(), and if I change obtain_options() in this way, it will work:

int obtain_options(  char ** optionLine)
{
    vector< char*> options;
    char *t1 = new char [100];
    t1[0] = 'a';
    t1[1] = 'b';
    t1[2] = 'c';
    t1[3] = '/0';
   options.push_back(t1);
    char *t2 = new char [100];
    t2[0] = 'd';
    t2[1] = 'e';
    t2[2] = 'f';
    t2[3] = '/0';
   options.push_back(t2);


   std::copy(options.begin(), options.end(), const_cast< char**>(optionLine));

   return options.size();
}

My question is if I do not change obtain_options(), how could I delete the 2D array optionLine in a proper way.

  • 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-07T01:04:24+00:00Added an answer on June 7, 2026 at 1:04 am

    your vector contains a set of character pointers. But, the actual memory pointed to by these strings are not continuous as you expect them to be. So, this call will not work as you expect.

    std::copy(options.begin(), options.end(), const_cast< char**>(optionLine));
    

    In worst case you could do like this, which is what you almost do yourself now.

    for (int i= 0; i<2; i++)
    {
         strcpy(optionLine[i],options[i]);
    }
    

    But avoid all these memory handling unless you are learning the pointers and allocations.

    See how neat you can code this like this in C++:

    int obtain_options( vector<string>& anOptions_out)
    {
        anOptions_out.push_back("abc");
        anOptions_out.push_back("def");
    
        return anOptions_out.size();
    }
    
    int main(int ac, char* av[])
    {
        vector<string> anOptions;
        obtain_options( anOptions );
        for (int i=0; i<anOptions.size(); i++)
        {
            cout<< anOptions[i].c_str() <<endl;
        }
    
        return 0;
    } 
    

    No allocations/deallocations by yourself.

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

Sidebar

Related Questions

I get the following errors when I try to build this project: error C2182:
I've got the following code: try { ... try { // This is *never*
I use the following code try to create an array of string vectors, I
I am creating date using following code try { newdatetime = new DateTime(2012, 2,
I'm currently downloading an HTML page, using the following code: Try Dim req As
Using following code I try to get updated list of checkbuttons' corresponding text values,
Using the following code to try and add the page slug as a class
I'm getting a compiler error when i try to build the following code. Its
I am using the following code, upload.php, to try to have someone upload a
I'm using the following code to build a list of email addresses on the

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.