Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8004727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T17:00:00+00:00 2026-06-04T17:00:00+00:00

I have tried to write a simple program that converts numbers to words (i.e

  • 0

I have tried to write a simple program that converts numbers to words (i.e it converts 123 to one hundred twenty three). The code compiles completely. The code uses pointer and recursion, which I always find tricky, in terms of memory management, in c++. Could any one point out if following code can have a memory leak when its executed? Thanks in advance.

#include <iostream>
#include <string>
using namespace std;
char *convert_number(int, int);

const char *tens[]={"","ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
const char *words[]={"zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine","ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen", "eighteen","ninteen"};
const char *place[]={"","thouands","million","billion","trillion"};


int main(int argc, char **argv)
 {
int number,conv_num,places;
places=1;
char *string= new char[1000];
char *temp_string = new char[100];
cout<<"Enter a number:";
cin>>number;
string = convert_number(number,0);
cout<<"The word is :"<<string<<endl;
}

char *convert_number(int number,int places)
{
int divisor;
char *word;
int conv_num;
char *temp_string = new char[100];
word = new char[100];
divisor=10;

if (number>=1000)
{   
    conv_num = number % 1000;
    number = (number-conv_num)/1000;
    places++;
    temp_string = convert_number(number,places);    
    word = strcat(word, temp_string);
    word = strcat(word, place[places]);
    word =strcat(word," ");
}
else
{
    conv_num = number;
}
if (conv_num>=100)
{
    word =strcat(word,words[conv_num/100]);
    word =strcat(word," hundred ");
    conv_num=conv_num%100;
}

if(conv_num >=20)
{
 word=strcat(word,tens[conv_num/10]);
 word =strcat(word," ");
 if(conv_num%divisor>=1)
 {
    word=strcat(word,words[conv_num%divisor]);  
    word =strcat(word," ");
  }
}

if(conv_num<20) 
{ 
    word=strcat(word,words[conv_num]);
    word =strcat(word," ");
}       
delete[] temp_string;       
return word;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T17:00:03+00:00Added an answer on June 4, 2026 at 5:00 pm

    “Could any one point out if following code can have a memory leak when its executed?”

    Yes, it can.

    char *temp_string = new char[100];  // first assignment
    
    [...]
    
    if (number>=1000)
    {   
    [...]
        temp_string = convert_number(number,places); 
    

    When you re-assign temp_string this way, the memory from the first assignment becomes inaccessible, and it has not been freed. You should delete[] that first.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried to write my first Boost program from information on the Boost
I have a simple program that takes an ID number and prints information for
I have a simple java program that runs fine in eclipse but cannot find
I have simple serial port program that is supposed to read the serial port
I am trying to write a simple Ruby program that I will run from
I wrote a simple program that reads the characters from external device (bar code
How can you write a VB.NET Windows Forms Application via CodeDom? I have tried
I'm trying to write a program that takes input of - hexadecimals, octals, and
Studing STL I have written a a simple program to test functors and modifiers.
I want to write a program in C or C++ that asks to user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.