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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:16:48+00:00 2026-05-28T20:16:48+00:00

If I declare an array inside a function, is the memory deallocated upon leaving

  • 0

If I declare an array inside a function, is the memory deallocated upon leaving the function?

I wouldn’t have thought so, however, when I declare an array inside a function, write the pointer to the array to a global variable and then attempt (outside the function) to dereference the pointer to an element in the array, I get a memory access violation. I don’t get a memory access violation if I use the same code inside the function.

Clarification would be much appreciated.

Thanks in advance.

  • 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-28T20:16:49+00:00Added an answer on May 28, 2026 at 8:16 pm

    An array declared in a function is allocated on the program stack. When your program exits the function, the local variables on the stack are popped and the memory containing the array is no longer accessible. The alternative is to new an array pointer which is allocated on the program heap, will survive the function exit and must subsequently be delete‘d or a memory leak will occur.

    A fairly general explanation of the program stack is a block of memory set aside to hold the local variables for your functions. When a function is called, the amount of memory required to hold the local variables for the function is pushed on the top of the stack, i.e the stack pointer is moved up by that amount. When the function exits, that exact amount of memory is popped off the top of the stack and the stack pointer moves back down to its previous location prior to the function call. The program heap on the other hand is memory that doesn’t have stack semantics and is used when a function requests a new block of memory. The program is then responsible for managing the deallocation of that memory.

    Smart pointers are often used in C++ to automatically handle the allocation and deallocation of memory to avoid bugs/memory leaks associated with memory management.

    A quick google threw up this explanation of stack versus heap in C++.

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

Sidebar

Related Questions

I am trying to declare a function parameter inside my array, but I'm having
Possible Duplicate: Declaring a global variable inside a function Is it possible to declare
I have the following code where I declare a PHP array variable and inside
I declare a static char array, then I pass it to a function. How
This is the code that I have to dynamically declaring an array inside a
I need to declare a global two-dimensional array in C. The size of the
I have a struct called Ambigous, and inside the struct I have an array
i declared an array called finalArray inside a viewController and in the function ViewDidLoad
I have an array of structs. The struct has two function pointers. Each element
I have a class say ClassA with a string array inside class. In my

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.