2

My Doubt is regarding only memory allocation so don't think about program output

#include<stdio.h>

int main(){

  for(int i=0;i<20;i++){
     char *str=malloc(sizeof(char)*6); //assuming length of each string is 6
     scanf("%s",str);
     insertinlinkedlist(str);
  } 

} 

whenever i allocate memory here as shown above only the base address of char array will pass to linked list,and that is the memory block allocated for char array is inside main only and i am storing the base address of that array in str which is local to main and is passed to insetinlinkedlist

I want to ask whenever memory is allocated inside loop than why the number of memory blocks(no of char arrays declared ) are created equal to n (number of time loop runs) since variable name is same we should be directed to same memory location Note I have checked in compiler by running the loop all the times when loop runs memory the value of str is different

is The above method is correct of allocating memory through loop and through same variable "Is the method ensures that every time we allocate memory in above manner their will be no conflicts while memory allocation and every time we will get the address of unique memory block"

Now above doubt also creates a doubt in my mind That if we do something like that

int main(){
   for(int i=0;i<n;i++){
   array[50];


   } 
}

then it will also create 50 array inside stack frame

3
  • The variable str merely contains a pointer to the allocated memory. Commented Oct 26, 2018 at 16:07
  • consider using punctuation marks when you write text for others to read because it makes it much easier to understand when one doesnt have to guess where one sentence ends and another starts its also considered polite to formulate an inquiry in a way that makes it easy to help you Commented Oct 26, 2018 at 16:13
  • A more efficient and flexible approach would be to scanf into a local array of large size, then malloc(strlen(array)+1) and strcpy(...) to save the results. Commented Oct 26, 2018 at 17:16

1 Answer 1

2

malloc returns a pointer to the first allocated byte. Internally it keeps track of how much memory was allocated so it knows how much to free (you do need to insert calls to free() or you'll leak memory, by the way). Usually, it does this by allocating a little bit of memory before the pointer it gives you and storing the length there, however it isn't required to do it that way.

The memory allocated by malloc is not tied to main in any way. Currently main is the only function whose local variables have a pointer to that memory, but you could pass the pointer to another function, and that function would also be able to access the memory. Additionally, when the function that called malloc returns, that memory will remain allocated unless manually freed.

The variable name doesn't matter. A pointer is (to first approximation) just a number. Much like how running int a = 42; a = 20; is permitted and replaces the previous value of a with a new one, int *p = malloc(n); p = malloc(n); will first assign the pointer returned by the first malloc call to p, then will replace it with the return value of the second call. You can also have multiple pointers that point to the same address:

int *a = malloc(42);
int *b = malloc(42);
int *c = a;
a = malloc(42);

At the end of that code, c will be set to the value returned by the first malloc call, and a will have the value returned by the last malloc call. Just like if you'd done:

//assume here that f() returns a different value each time
//it's called, like malloc does
int a = f(); 
int b = f(); 
int c = a;
a = f();

As for the second part of your question:

for(int i=0;i<n;i++){
   int array[50];
} 

The above code will create an array with enough space for 50 ints inside the current stack frame. It will be local to the block within the for loop, and won't persist between iterations, so it won't create n separate copies of the array. Since arrays declared this way are part of the local stack frame, you don't need to manually free them; they will cease to exist when you exit that block. But you could pass a pointer to that array to another function, and it would be valid as long as you haven't exited the block. So the following code...

int sum(int *arr, size_t n) {
   int count = 0;
   for (size_t i = 0; i < n; i++) { 
       count += arr[i];
   }
   return count;
}

for(int i=0;i<n;i++){
   int array[50];
   printf("%d\n", sum(array, 50));
}

...would be legal (from a memory-management perspective, anyway; you never initialize the array, so the result of the sum call is not defined).

As a minor side note, sizeof(char) is defined to be 1. You can just say malloc(6) in this case. sizeof is necessary when allocating an array of a larger type.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanx @Ray for such a great explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.