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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:52:26+00:00 2026-05-23T23:52:26+00:00

Why can I return from a function an array setup by malloc: int *dog

  • 0

Why can I return from a function an array setup by malloc:

int *dog = (int*)malloc(n * sizeof(int));

but not an array setup by

 int cat[3] = {0,0,0};

The “cat[ ]” array is returned with a Warning.

Thanks all for your help

  • 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-23T23:52:26+00:00Added an answer on May 23, 2026 at 11:52 pm

    There are two key parts of memory in a running program: the stack, and the heap. The stack is also referred to as the call stack.

    When you make a function call, information about the parameters, where to return, and all the variables defined in the scope of the function are pushed onto the stack. (It used to be the case that C variables could only be defined at the beginning of the function. Mostly because it made life easier for the compiler writers.)

    When you return from a function, everything on the stack is popped off and is gone (and soon when you make some more function calls you’ll overwrite that memory, so you don’t want to be pointing at it!)

    Anytime you allocate memory you are allocating if from the heap. That’s some other part of memory, maintained by the allocation manager. Once you “reserve” part of it, you are responsible for it, and if you want to stop pointing at it, you’re supposed to let the manager know. If you drop the pointer and can’t ask to have it released any more, that’s a leak.

    You’re also supposed to only look at the part of memory you said you wanted. Overwriting not just the part you said you wanted, but past (or before) that part of memory is a classic technique for exploits: writing information into part of memory that is holding computer instructions instead of data. Knowledge of how the compiler and the runtime manage things helps experts figure out how to do this. Well designed operating systems prevent them from doing that.

    heap:

    int *dog = (int*)malloc(n*sizeof(int*));
    

    stack:

    int cat[3] = {0,0,0};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to return a value from (transport) to the calling function, but can't
It is my understanding that you can return an array from a function in
In order to return a value from a VB.NET function one can assign a
How can I return multiple values from JQuery.Ajax() in the success function ? I
How can return a variable from a thead (I have the threads handle too).
How can i return xdocument from wcf service??? what i need to do to
Can I return a StreamReader from a method ?
How can I return a value from ShowStatus elapsed event? Here is my code.
How can I return null instead of a 'undefined' from a select element. Basically
How can i change the implementation of a method at runtime from return false;

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.