dynamic memory

can we use dynamic memory allocation in our program to store arrays or how to take input of 5000000 strings as input???

in a C program, the OS typically does not provide large stack memory. so, declaring a static array of large sizes will normally crash. on the other hand, the heap memory is available for use, the limit of which is much larger. so, dynamic allocation is typically advised when you have such enormously large requirements.

the FAQ advises using global variables. but again it comes with some limitations!

you can use dynamic memory allocation but if you want to take input in codechef and prrint some output for every string… then use a loop read string one by one and print simultaenously… no need to store…

i=5000000;
while(i--)
{
    scanf("%s",&string);
   ///processs
    //print answer
}

how ever if you want to store them in an array then @avinrox is right

1 Like

@isha_25 Yes you can use Dynamic allocation to store 5000000 strings and you can do any operations on these strings

follow this code for more clarifications

#include<stdio.h>

#include<stdlib.h>

#define max 5000000

int main()
{
	char **str;
	str = (char *)malloc(sizeof(char *)*max);
	
	int size = 20, i; // size is the maximum length of string that you can store. 
	
	for(i=0;i<max;i++) {

		str[i] = (char *)malloc(sizeof(char)*size);
		scanf("%s",str[i]); 
	
	}
	
	for(i=0;i<max;i++) {
		
		//Do operations on stored strings.
		//printf("%s\n",str[i]);
	
	}
	return 0;
}

May this will help you .

Happy Coding!!!

2 Likes