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

The Archive Base Latest Questions

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

Ive been faced with a problem recently that I can’t think of a good

  • 0

Ive been faced with a problem recently that I can’t think of a good way to solve. I’m using a case structure to attempt to set attributes to a “character” that will be passed to an object constructor.

Example:

//note this is inside a function with a return type of int*
int selection;
cin >> selection;
int * iPtr;

switch(selection){

case 1:{
     int anArray[6] = {8,5,2,4,250,100} // str, dex, int, luck, hp, mp
     iPtr = anArray;
     return iPtr;
}

//more cases and such below

The issue that I’m having is that when I return my pointer it seems to be filled with a good amount of junk, rather than the information, rather than the information that I would be expecting it to hold. Is that because the array gets destroyed at the end of the scope? If so what should I do to make this work out how I’m hoping for it to (getting a pointer with the values that I want).

Thanks!

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

    Yes – anArray is declared on the stack. When the function exits, its stack frame is reclaimed, so it’s no longer valid to refer to that memory. If you want the array to persist, allocate it on the heap instead:

    int* anArray = new int[6]; // and initialize
    return anArray;
    

    Just remember to clean it up later at some point with the corresonding delete[].

    EDIT

    You should prefer to use something that automatically manages resources for you, like in Praetorian’s answer, so that you don’t accidentally leak memory.

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

Sidebar

Related Questions

I've been using Rapidxml lately and have faced following problem. When I try to
I've been looking into OSGi recently and think it looks like a really good
I've recently been developing a website using asp.net webforms that uses in proc sessions
ive been watching a tutorial that has told me to change the background color
Ive been on google all morning and here on SO. The problem im having
Ive been using bootstrap for a few months and am looking to clarify something
I've recently faced this problem in rails. Almost every code I've entered return an
I've been reading that people face problems when using the exit function in their
I recently started using Eclipse at work for my Java servlet projects. I've been
Hey math geeks, I've got a problem that's been stumping me for a while

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.