Click here to Skip to main content
15,041,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the first case, segmentation fault error.
In the second case, output is 2.
In the first case as the array was declared in local function, once we move in to main function the array memmory gets destroyed.
But, In the second case why I am getting output and why there is no segmentation fault error?

What I have tried:

C++
#include<stdio.h>
int create()
{
    int a[3]={1,2,3};
    return a;
}
int main()
{
  int *c=create();
  printf("%d",c[1]);
  return 0;
}


// second case:

#include<stdio.h>
int create()
{
    int *p=(int*)malloc(sizeof(int)*3);
    p[0]=1;
    p[1]=2;
    p[3]=3;
    return p;
}
int main()
{
    int *c=create();
    printf("%d",c[1]);
    return 0;
}
Posted
Updated 6-May-21 5:17am
v2
Comments
Richard MacCutchan 6-May-21 4:26am
   
In case 1 the array a is created on the stack within the create function. But when that function returns its stack is destroyed so the array no longer exists. So the address returned from the function is no longer valid.
_-_-_-me 6-May-21 8:24am
   
okay, thank you very much!
jeron1 6-May-21 10:02am
   
Also, this line

p[3]=3;

will produce non-determinant results, the largest index an array of size = 3, is 2.

Quote:
But , In the second case why I am getting output and why there is no segmentation fault error ?
because, in the second case, you are returning the address of a dynamically allocated memory block which is only released ('destroyed') on request (calling the free function).
Note, your code is not robust, it should be:
C
#include<stdio.h>
int * create()
{
    int *p= (int*) malloc(sizeof(int)*3);
    if ( p )
    {
      p[0]=1;
      p[1]=2;
      p[3]=3;
    }
    return p;
}

int main()
{
    int *c = create();
    if ( c )
    {
      printf("%d",c[1]);
      free(c);
    }
    return 0;
}
   
Comments
_-_-_-me 6-May-21 8:23am
   
Oh,Thank you very much!
Why we should include if statement there? and why we should pass p as argument there in p? Is it because to make sure that p points to some memmory location?
CPallini 6-May-21 8:55am
   
because malloc may fail, see:
http://www.cplusplus.com/reference/cstdlib/malloc/
There is a technical term for what your first code is doing: returning a dangling pointer[^]

When you create variables in a function, they are allocated on a small area of memory called the stack, which acts like a "stack of coins":
You call a function, and the address you cam from is "pushed" on the top of the stack.
The function then allocates memory and pushes that to the top of the stack.
The function runs, and used the variables
When you return, the function "pops" all the variables it stacked off again, then "pops" the return address off and goes back where it came from with that.

When you return a pointer to a local variable from teh function, the pointer is to memory that has been freed, and that will be used by the next function call. So the call to printf will corrupt memory that you pointed to and things ... start to go wrong in unpredictable ways ...

When you allocate memory using malloc, it doesn't come from the stack - it comes from a much, much larger area of memory called the heap and that isn't released until you explicitly call free and pass the pointer to it.
If you don't call free then your code probably won't crash for a long time - but it will start to "leak memory" because the heap while large is not infinite!

The size of the stack and the usage above also explains why you will get "out of memory" or "stack overflow" errors if you deliberately or accidentally start using direct or indirect recursion without adequate checks in place!
   
Comments
CPallini 6-May-21 6:24am
   
5.
_-_-_-me 6-May-21 8:19am
   
You explained it very clearly. Thank you very much!
OriginalGriff 6-May-21 8:46am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900