-6

can someone please tell me the difference between the type of memory i am using here in the two given methods because in the first on i am getting out put as this:

5-->1893664096-->32766-->-1358605976-->2-->1893664096

and in second method i am getting this:-

5-->0-->0-->0-->2-->0

below are the codes for this:-

Method 1:

#include<stdio.h>
#include <stdlib.h>
int main()
{
  int n;
  scanf("%d",&n);
  int arr[n];
  arr[3]=2;
  for(int i=0;i<n;i++)
    printf("-->%d",arr[i]);
  return 0;
}

Method 2:

int main()
{
  int *a;
  int n;
  scanf("%d",&n);
  a=(int*) malloc(sizeof(int)*n);
  a[3]=2;
  for(int i=0;i<n;i++)
    printf("-->%d",a[i]);
  return 0;
}
Amrit Singh
  • 143
  • 1
  • 7
  • 4
    In both cases you only initialised *one* array element (assuming that `n` is at least `4`), the rest are not, so the behaviour is undefined. – Weather Vane Jan 26 '18 at 14:36
  • You have to manually free the memory after using malloc. In order to do so you need to use free(a); If you don't do that the junk there will be reserved from your program and until you restart your computer you won't be able to use it. Also you are able to reallocate the size if needed. – dbl Jan 26 '18 at 14:37
  • What is the purpose of this program. looks like non-sense to me. – machine_1 Jan 26 '18 at 14:37
  • Although it's not the cause of your problem, you should read and understand [the question on why not to cast the return value of `malloc()` and family in C](/q/605845). – Toby Speight Jan 26 '18 at 14:37
  • 1
    @dbl allocated memory is usually handed back to the system when the program exits. – Weather Vane Jan 26 '18 at 14:38
  • @WeatherVane your right here. https://stackoverflow.com/questions/654754/what-really-happens-when-you-dont-free-after-malloc – dbl Jan 26 '18 at 14:40
  • i want to know whats the difference between the two and why do i get only zeros in the second method. – Farazuddin Mohommad Jan 26 '18 at 14:40
  • 3
    It is not the difference between the two programs which is causing the strange output, but what they have in common: the arrays are not initialised. In one case they *happen* to be `0`, in the other they are not - the memory is allocated in different parts of the user space. – Weather Vane Jan 26 '18 at 14:42
  • 1
    ... and there is no *why* to be had at the C language level, because both of your programs exhibit **undefined behavior** as far as the C language is concerned. – John Bollinger Jan 26 '18 at 15:30

3 Answers3

2

malloc() is not supposed to zero the memory it returns. It seems that in your case it happens to be retrieving memory from the system that has not been previously used and is still zero. You certainly cannot count on this. calloc() on the other hand will zero memory for you before returning.

Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
1

What you are seeing is a manifestation of undefined behavior.

In the first case you define an array inside of a function, while in the second you allocate an array by calling malloc. In both cases, that memory is uninitialized, meaning you can't predict what the values will be.

dbush
  • 205,898
  • 23
  • 218
  • 273
-1

As you have asked the question about the difference between dynamic and static , dynamic memory location is in which the memory location allotted during run time , while as normal one is in which we initialize the memory before . Like ar[3]

  • 1) he said "normal", not static. "Normal" is meaningless as far as I know, but if you're answering the question you think he meant to ask, it's important to make that clear. 2) arr and a are *both* allocated at run-time. One is allocated on the stack, and the other on the heap. – zzxyz Apr 16 '18 at 00:09