+ 2

Getting unknown characters as output in the c++ code

I have done a code for the below problem https://i.stack.imgur.com/X4hO0.png But in the below code if I remove printf("",res); I'm getting some unknown special characters. https://code.sololearn.com/cItJOP22xcP7 If not in this way what are the other ways that I can do this code?. I have completed this problem using java and c#. The above code is working in my compiler(code blocks) but not working in this test and getting some garbage characters as output. I think the problem is with the conversion of char[] to char*. Try to remove printf("",res); at line 36 and run with inputs 5 and 891 to see error. Please reply what is wrong in code and if possible solution for it.

16th Jul 2020, 6:18 PM
Padala Vamsi
Padala Vamsi - avatar
5 Réponses
+ 2
sai vamsi line number 35 ( char *res = s;) in your code is culprit... Why so : Your char* is not on heap and it's on stack region.. scope of same is limited to that function only... If you want to access that memory address out side of that function , that address should be still valid accessible even after scope of function ends Option : Just comment line 35 and do add below two lines and you are done... Don't forget to include cstring as well: char* res = new char[c+1]; memcpy(res,s,c+1); As you are getting output , doesnot mean your problem is solved... Ideally speaking , now issue is that we are not deallocating memory of char* which we allocated using new... It should be done using delete (or free in case you have allocated using malloc due to c code)... This is just to showcase actual problem in your code and char* allocated on heap should be handled properly to avoid memory issue...
16th Jul 2020, 9:43 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 3
This is corrected version of your code. One more thing to note is that you need to create dynamic array inside function because you are defining their sizes at runtime. Otherwise it could lead to other issues. Here I have again used string instead of char array. string Decimal(int n,int num) { int temp=num; int c=1; while(temp/n != 0) { temp = temp/n; c++; } temp = num; int *arr = new (nothrow) int[c]; if (arr == nullptr) { cout << "Error assigning memory\n"; return nullptr; } for(int i=c-1;i>-1;i--) { arr[i] = temp%n; temp = temp/n; } string s = ""; for(int i=0;i<c;i++) { if(arr[i]>=0 && arr[i]<10) { char t = char(arr[i]+48); s += t; } else { char t = char(arr[i]+55); s += t; } } delete[] arr; return s; }
16th Jul 2020, 8:34 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 3
Well in that case, do as Ketan Lalcheta suggested. And don't forget to delete the space allocated on heap inside the main function.
17th Jul 2020, 8:16 AM
blACk sh4d0w
blACk sh4d0w - avatar
+ 2
Since you are doing it in C++, I would suggest you to do it with strings instead char array as that would be pretty easy. Below is a shorter version of what you have done so far. #include <algorithm> #include <iostream> using namespace std; string Decimal(int n, int num) { string s = ""; while(num > 0) { int temp = num % n; if(temp >= 0 && temp < 10) { char t = char(temp + 48); s += t; } else { char t = char(temp + 55); s += t; } num /= n; } reverse(s.begin(), s.end()); return s; } int main() { int n; cin >> n; int num; cin >> num; string res = Decimal(n,num); cout << res << endl; return 0; }
16th Jul 2020, 8:29 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 2
I was not allowed to chande main function or the function declaration in that question so I must return char* I'm not allowed to use string that is why I got solution in Java but not in c++. I'm not very good in pointers concept so please give answer in pointers only so that I can learn from my errors. Thank you all for the reply
17th Jul 2020, 6:09 AM
Padala Vamsi
Padala Vamsi - avatar