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

The Archive Base Latest Questions

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

I am writing a programm in C++. In my programm I need to create

  • 0

I am writing a programm in C++. In my programm I need to create an array with dynamic size inside one function, but this array should be also accessable for other functions. I will not post here my code, just write one dummy example.

char *array;


void function_1() {
    array = new char(3);
    array[0] = "value 1";
    array[1] = "value 2";
    array[2] = "value 3";
}

void function_2() {
    array[0] = "new value 1";
}

int main() {
    function_1();
    function_2();

    delete[] array;
}

My question is: I am not sure, if the array will exist outside the function_1, where it was initialised, until I delocate a memory of array.
Or the array will have just a behaviour of local variable inside one function. What means, that the memory, which stores the array values, will be dellocated after the function is finished and the memory addresses of my array can be rewroten with something else later in my programm.

Thank you.

  • 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-23T07:25:07+00:00Added an answer on May 23, 2026 at 7:25 am

    First, of course it will exist outside, that’s all what dynamic allocation is about. Also, the variable itself is global. Also, it should be a char const** array; and the allocation should be new char const*[3] (note the square brackets). The const because you won’t change the contents of the strings here.

    Second, don’t do that. Just put it in a class and use a std::vector!

    #include <vector>
    
    class Foo{
    public:
      function_1(){
        _array.push_back("value 1");
        _array.push_back("value 2");
        _array.push_back("value 3");
      }
    
      function_2(){
        _array[0] = ("new value 1");
      }
    
    private:
      std::vector<std::string> _array;
    };
    
    int main(){
      Foo f;
      f.function_1();
      f.function_2();
    }
    

    Even better, have a std::vector<std::string>, so you can safely modify the contents without having to worry about memory management. Though, to this won’t be a single block any more. Now I got to ask, how exactly do you want to pass the buffer to the socket?

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

Sidebar

Related Questions

I need to create a 2 column array in ABAP so that a program
I'm writing this program that will need to access the registry to pull some
I need to create files inside of a specific folder (C:\Windows\System32\oobe\info\backgrounds) and possibly create
OK, so this should be simple... But I still can't figure it out. I
I'm writing a program in Java and one of the things that I need
I'm writing a Rhomobile application targeted at iPhone and Android. I need to create
I'm writing a program and I need to determine if bits 3 and 6
I'm writing a program in C# that will need to store a few Data
I'm writing a program (for Mac OS X, using Objective-C) and I need to
I am writing a program that needs to run a java.jar server. I need

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.