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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:10:00+00:00 2026-06-15T02:10:00+00:00

So, if I want to declare an array of characters I can go this

  • 0

So, if I want to declare an array of characters I can go this way

char a[2];
char * a ;
char * a = new char[2];

Ignoring the first declaration, the other two use pointers. As far as I know the third declaration is stored in heap and is freed using the delete operator . does the second declaration also hold the array in heap ? Does it mean that if something is stored in heap and not freed can be used anywhere in a file like a variable with file linkage ? I tried both third and second declaration in one function and then using the variable in another but it didn’t work, why ? Are there any other differences between the second and third declarations ?

  • 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-15T02:10:01+00:00Added an answer on June 15, 2026 at 2:10 am
    • In the first case, a[2] stores 2 chars on the stack.
    • In the second case, there is no allocation at all – a is an uninitialized
      pointer.
    • In the third case, 2 chars are allocated on the heap.

    You are right in thinking that heap allocated variables can be shared across your process, however, you will need to ensure that you pass the location of the allocated heap memory around – you do this e.g. by returning a from your method or function, or by increasing the scope of a e.g. to class scope.

    delete will free heap allocations. In your case, delete should only be used in scenario 3, since in #1, stack variables are cleaned up when they go out of scope, and in #2, you haven’t allocated any memory.

    Because the above can easily lead to chaos during the transfer of ownership over the heap allocations, smart pointers such as auto_ptr or boost’s shared_ptr can be used to make life simpler.

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

Sidebar

Related Questions

I want to declare a global array so I can use this in all
I want to declare a string array, I was using this way of doing
Is there a way to declare a 2D array of integers in two steps?
I'm new to c++. I just want to declare an array and find its
I want to declare a constant array which can be accessed from multiple C
I can declare NSMutableArray or NSArray but I want to declare class array. Let
i want to use javascript array globly using netbeans ide.. i declare the array
i want to declare a parametrized method array or method list. This means that
I'm trying to call method from .jar But when I want to declare array
I want to declare an array of handel as the following code: using namespace

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.