Is there any memory wastage caused by char a[100][7] in this below code due to garbage value? Or it was rectified by compiler? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Is there any memory wastage caused by char a[100][7] in this below code due to garbage value? Or it was rectified by compiler?

In this below code, I use scanf() to get string as a input and store it in a char a[100][7].... Suppose, if I input only limited number of strings... i.e. only four strings eg: ["Chirp", "Rawr", "Grr", "Ssss"] But char a[100][7] can hold "100" string.. We know that unused memory in variable or array,is occupied by garbage values.... Here I input only 4 strings,then there will be 96 unused memory for string in char array.... Is this lead to more memory wastage?[due to unused memory] Or compiler, automatically remove that unused memory in char array, that was occupied by garbage values..... https://code.sololearn.com/cwLdur7XVO7g/?ref=app

29th Nov 2020, 6:00 AM
Yogeshwaran P
Yogeshwaran P - avatar
7 Answers
+ 6
Yes it will lead to Memory wastage .... Inorder to avoid tht u can create the char variable dynamically by using 'new'
29th Nov 2020, 6:07 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 3
29th Nov 2020, 6:19 AM
Alphin K Sajan
Alphin K Sajan - avatar
+ 3
You don't really need a 2D char array for this, you can reuse the same 1D char array to read input over and over again. The way you compare input to animal sound isn't quite right. The comparison only considers the first character to compare. It outputs 'Lion' when I pass 'Goal' as input, because the comparison only compares the 'G' letter inside both string.
29th Nov 2020, 6:39 AM
Ipang
+ 2
Alphin K Sajan thank you very much .... It means, compiler doesn't ignore garbage values...And it convert that garbage value to 0's and 1's.... Am I right?
29th Nov 2020, 6:13 AM
Yogeshwaran P
Yogeshwaran P - avatar
+ 2
Thank you very much Alphin K Sajan 😃
29th Nov 2020, 6:21 AM
Yogeshwaran P
Yogeshwaran P - avatar
+ 2
Ipang thanks for your comments....😃 Before creating this code, I also thought like this only,to compare only using first character of sounds.... But I don't know,how will the hidden test case,will function!😅 So, that only, I think not to take risk.....and to include sound names in my code😁 even though I included that sound names... it will compare only first character....[like what you say,on yours comment] here that is a quit fun😅
29th Nov 2020, 6:49 AM
Yogeshwaran P
Yogeshwaran P - avatar
+ 1
Yogeshwaran Wlcm :)
29th Nov 2020, 6:22 AM
Alphin K Sajan
Alphin K Sajan - avatar