1 ) i did not get the code written from line 16 to line 23.
2) why dynamic memory is needed ??
3) why pointer to pointer variable is needed
4) why is there any need to pass argument in main function and how is it even allowed ??
I get the logic discussed in previous lecture but this program is so complex
The variables are named argc (argument count) and argv (argument vector) by convention, but they can be given any valid identifier: int main ( int num_args, char arg_strings) is equally valid. They can also be omitted entirely, yielding int main () , if you do not intend to process command line arguments.
dynamic memory is allocated during run time. Exact sizes or amounts (like the size of an array, for example) does not have to be known by the compiler in advance. This memory is allocated by the run time system.
to allocate dynamic memory we use pointers
Advantage of allocating memory dynamically: When we do not know how much amount of memory would be needed for the program beforehand. it will save our memory from wasting
and to understand how 2D array is allocated dynamically
you can see this allocation of dynamic 2 Darray [Plz understand me dry run of this]
asaurabh_26
- hey, you explained it very nicely.
but in this line int *arr=new int [n];
as int [n] is a pointer because it is pointing to address so it should be like new int *[n] and also left part should be like int**arr because it is pointing to pointer. ?? - how it make any difference if arguement in int main will not be passed.
i mean what would be code if arguement in int main will not be passed. ?? - even why there is any need of dynamic memory
by input we get values of m and n then define the array and pass values in it and call the function.
why do we need dynamic memory here ??
the address of this dynamically allocated array is stored in arr so it is pointer
pointer stores the address
arr is storing address of array which is given by keyword new
there is no relation in passing argument to main() with dynamic memory allocation
dynamic memory allocation is use only to avoid wastage of memory
if you allocate it simply
int arr[100][100]
then if your n and m are 5 each
whole memory other than 5X5 is wasted
why is there any need to pass argument in main function ??
i mean how the program would affect if we did not pass the argument in main function.
what is the significance of argc and argv
this is not needed but if you want to understand
i hope this helps
these are Command-line arguments
Command-line arguments are given after the name of the program in command-line shell of Operating Systems.
To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments.
argc (ARGument Count) is int and stores number of command-line arguments passed by the user including the name of the program
argv(ARGument Vector) is array of character pointers listing all the arguments.
int main(int argc, char argv[]) { / … */ }
or
int main(int argc, char *argv) { / … */ }
#include <iostream>
using
namespace
std;
int
main(
int
argc,
char
** argv)
{
cout <<
"You have entered "
<< argc
<<
" arguments:"
<<
"\n"
;
for
(
int
i = 0; i < argc; ++i)
cout << argv[i] <<
"\n"
;
return
0;
}
in command line if we write
sudo g++ test.cpp
./a.out every thing you want to write
output will be
You have entered 4 arguments:
./a.out
every
thing
you
want
to
write
yeh stuff mere course m kahi aage h ??
it’s not related to data structure and algorithms
it is more about command line
i hope this help
if you have more doubts regarding this feel free to ask
if your doubt is resolved mark it as resolved from your doubt section inside your course