OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 8:53 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: C++ toString (char*) implmentation
PostPosted: Tue Mar 22, 2022 12:19 pm 
Offline

Joined: Tue Oct 26, 2021 8:21 pm
Posts: 8
I'm trying to write a function to convert an int to a string for my basic kernel. This is my current implementation:
Code:
const char* toString(int i) {
   unsigned int it = 0;
   char *str;

   // TODO: Fix for negative numbers

   while(i > 0) {
      int n = i % 10;
      str[it] = (char)('0' + n);
      i = i / 10;
      it++;
   }

   return str; // This is line 155
}

However, C++ gives me the warning kernel/kernel.cpp:155:16: warning: 'str' is used uninitialized. I can't figure out what to initialize str to, as initializing it to "" also causes a warning, and ignoring the warning causes the test value of 53 appear as "S ".

Edit: I think that the "S " comes from it reading from a point in memory.


Top
 Profile  
 
 Post subject: Re: C++ toString (char*) implmentation
PostPosted: Tue Mar 22, 2022 1:01 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 5:26 am
Posts: 30
Hello RadPoseidon,

The warning is obvious, you're using 'str' without initializing it which should cause a segmentation fault if you run it. You need to either allocate some memory in your 'toString' function or pass it to it.

Code:
char *str = new char[10]; // you probably won't need more that 10 characters.
or
char *str = (char *)malloc(10 * sizeof(char));

Also note that:
Code:
char str[10] won't work because it will be allocated onto the stack of 'toString' which will be destroyed when 'toString' returns.


Finally, your code doesn't exactly do what you're hoping for, indeed it will convert an int value to an array of characters but the array is in reverse order.

Hope that helped.

_________________
Systems and Computer Engineering Researcher
"Do you pine for the nice days of Minix-1.1, when men were men and wrote their own device drivers?" -- Linus Torvalds
http://sce.carleton.ca/~maslan


Top
 Profile  
 
 Post subject: Re: C++ toString (char*) implmentation
PostPosted: Tue Mar 22, 2022 1:18 pm 
Offline

Joined: Tue Oct 26, 2021 8:21 pm
Posts: 8
Thank you, I felt it had something to do with memory allocation, but I wasn't sure what to do. I had also conveniently written a function to get the digits in an int shortly beforehand.
I'm getting the error in function `_Z8toStringi': kernel.cpp:(.text+0x514): undefined reference to `_Znam' on link now, but I think it's unrelated to this and more due to a prior implementation of something.


Top
 Profile  
 
 Post subject: Re: C++ toString (char*) implmentation
PostPosted: Tue Mar 22, 2022 1:32 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5099
That error means the linker can't find your implementation of "operator new[](unsigned long)". (Or perhaps you haven't implemented it yet?)

You can use c++filt or various websites to demangle symbol names.


Top
 Profile  
 
 Post subject: Re: C++ toString (char*) implmentation
PostPosted: Tue Mar 22, 2022 2:08 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1593
As to your original question, in most cases it is simpler to have the caller provide a buffer. Taking care of the whole domain, a simple implementation might be:
Code:
void toString(int i, char *s) {
  static_assert(-5 % 3 == -2, "Compiler uses symmetric modulo"); /* if this fails, the code below won't work. */
  if (i < 0)
    *s++ = '-';
  else
    i = -i;
  int t = i;
  do s++;
  while (t /= 10);
  *s = 0;
  do *--s = '0' - i % 10;
  while (i /= 10);
}
This always turns the argument negative, because while each positive int can be turned negative, the reverse is not true (INT_MIN cannot be turned positive on most systems). Then it is simply a matter of working with division in the negative numbers. I put an assert in to verify that symmetric modulo is used, but it is used by pretty much all implementations I have ever come across. The alternative would not be pretty. In the case of absolute modulo, you get the inverse of each digit except 0, and the divisions would raise the argument towards -1, unless the argument was 0 from the start.

Now, there are some obvious areas for improvement, still. The buffer is unbounded at the moment, so the caller needs some way of knowing how to allocate enough memory from the start. Or alternatively, the caller could provide a buffer size, but then you need to specify what happens when the buffer runs out. I shall leave that as exercise for the reader.

_________________
Carpe diem!


Top
 Profile  
 
 Post subject: Re: C++ toString (char*) implmentation
PostPosted: Wed Mar 23, 2022 7:28 am 
Offline

Joined: Tue Oct 26, 2021 8:21 pm
Posts: 8
Yeah, I missed an article that explained defining new/delete, etc. I thought I had objects working since it let me construct and use a test object.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 30 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group