0

Why are garbage values stored in a pointer array even though i am dynamically allocating the right amount of space needed

Here after the first call of functn..the pointer is freed..then why are some garbage values printed along with my second output 'red' https://code.sololearn.com/cxymtWCVLTgg/?ref=app

19th Mar 2020, 11:30 AM
Y AD Ù
Y AD Ù - avatar
10 Respostas
+ 3
You forgot to put null-character at the end of strings after copying. So printf outputs any data after string (garbage) untill it read some random null-character. Also storing a lowercase string into temporary buffer (wrd2) is extra operation, you can store it directly to allocated memory. #include<stdio.h> #include<string.h> #include<stdlib.h> #include<ctype.h> char *strlower(const char *wrd) { char *str = (char*)malloc(strlen(wrd) + 1); char *ptr = str; while(*wrd) *ptr++ = tolower(*wrd++); *ptr = 0; return str; } int main() { char* str1 = strlower ("HHdrrIrbe\n"); char* str2 = strlower ("rEd"); printf("%s%s\n",str1, str2); free(str1); free(str2); return 0; }
19th Mar 2020, 11:51 AM
andriy kan
andriy kan - avatar
+ 2
Memory is "continuous". When you allocate space, malloc() only marks in its service data that block of memory at some address are used. The size of the block is defined by passed argument. But right after your block there are other blocks of memory (allocated or free). And that blocks may contain any data (not zeros). malloc doesn't zero allocated memory. When printf outputs passed string it reads the string by one character at the time. If character is not null-character then printf outputs it and move on to the next charcter. If character is null then output stops. printf know nothing about memory allocation and the size of allocated block. It just outputs characters till it reads a null-character. Even if null-character is outside of an allocated memory block (in another memory block which contains some data that printed as garbage).
19th Mar 2020, 2:14 PM
andriy kan
andriy kan - avatar
+ 2
in const char* wrd const says that you do not change the data pointed by wrd. Compiler may better optimize code if it knows that some data will not be changed. Also it prevent accidental changes to source string. If you try to modify source string in the function compiler will give an error. ptr pointer is used to iterate over the whole string and store converted letters to allocated buffer. str stores the address of an allocated bufer. It needed because ptr will be changed after iterating over source string but the function should return that address as a result.
19th Mar 2020, 6:02 PM
andriy kan
andriy kan - avatar
+ 2
Y AD Ù step by step: char *str = (char*)malloc(strlen(wrd) + 1); // str now stores the address of allocated buffer. char *ptr = str; // ptr also stores the address of allocated buffer. while(*wrd) *ptr++ = tolower(*wrd++); // in this loop ptr iterates over all characters of the buffer to store converted character // on each iteration it is increased by 1, to move to the next character. // after loop ptr will point to the end of the converted string (right after the last character) *ptr = 0; // stores null-character (you can use '\0' but it is the same) at the end of the converted string return str; // return address of allocated buffer that stores at str // we can't return value of ptr, as it points to the null-character of the converted string you can use only one pointer ptr and subtract length of the source string from it to get an address of allocated buffer but in that case you need to store the length of the source string (or call strlen() again but it is an extra operation)
19th Mar 2020, 6:29 PM
andriy kan
andriy kan - avatar
+ 1
Y AD Ù because you don't append null-character to wrd2 and pass it to strlen. strlen also seraches for the null-character and it will find null-character in a wrong place. As a result return value is wrong length. you should change line #13 to: int len=strlen(wrd); // replace wrd2 with wrd and append after it new line: wrd2[len]='\0'; Did you try the code I've posted? It is shorter.
19th Mar 2020, 2:39 PM
andriy kan
andriy kan - avatar
0
But when malloc function is used to assign the memory wanted..how can it read some random values.,as no values are present andriy kan
19th Mar 2020, 1:23 PM
Y AD Ù
Y AD Ù - avatar
0
suppose malloc is used to assign 6 spaces..and the word black is entered wont the 6 place be replaced as\0 andriy kan
19th Mar 2020, 1:29 PM
Y AD Ù
Y AD Ù - avatar
0
andriy kan now i have modified my code.,why aint it working now i have assigned the final null charector and stored the stlower("") to a char pointer and freed it..still not working
19th Mar 2020, 2:30 PM
Y AD Ù
Y AD Ù - avatar
0
yea thanks andriy kan it worked..yea i took some of your code,,but i didnt understand some parts lyk const char why<using two pointers lyk *ptr *str.,but still it worked
19th Mar 2020, 3:14 PM
Y AD Ù
Y AD Ù - avatar
0
But andriy kan why didnt u jst use *str and store the word directly and return str..why ptr?
19th Mar 2020, 6:11 PM
Y AD Ù
Y AD Ù - avatar