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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:07:45+00:00 2026-06-13T05:07:45+00:00

I want to allocate variable sized 2D array in a function without using new

  • 0

I want to allocate variable sized 2D array in a function without using new operator such that this 2D array is available to other functions in the same file.

    void draw(int i)
    {   size=i;   }

    void assign(char symbol)
    {
        char one[size][size];
        /// ... Assigning values to one  ...
    }
    void display()
    {   /// Displaying values of one[size][size]
        for(int i=0;i<size;i++)
        {
            for(int j=0;j<size;j++)
            cout<<one[i][j];
            cout<<endl;
        }
    }

Execution order of functions is draw -> assign -> display

This question may have been asked earlier.
But my problem is..
-> I can’t declare the array outside assign function globally because value of size is not known.
-> I can’t use “one” array in “display” function because its scope is limited to “assign” function.

And I also don’t want to use either new or malloc operators.
Please help if there is any alternative available.

  • 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-13T05:07:46+00:00Added an answer on June 13, 2026 at 5:07 am

    C++ doesn’t support stack-allocated Variable Length Arrays like for example C99. You should use std::vector<T> instead. If you really want to use stack allocation you can use alloca().

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

Sidebar

Related Questions

I want to statically allocate the array. Look at the following code, this code
In C++ you can easily allocate one dimensional array like this: T *array=new T[N];
In C++, if you want to dynamically allocate an array, you can do something
Is it possible to dynamically allocate a temporary variable in C++ ? I want
I tried to allocate an array of structs in this way: struct T {
Using CUDA, I want to allocate memory for different arrays, one for each GPU
I'm writing a function in C that takes a variable number of arguments. size_t
Please be nice.. I am a noob with this stuff. I want to allocate
I want to allocate a row of a table to any user after they
I want to allocate rankings to users, based on a points field. Easy enough

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.