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

  • Home
  • SEARCH
  • 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 6783055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:49:08+00:00 2026-05-26T16:49:08+00:00

EDIT Found the answer. It’s a logical error located at if(carry == 0 &&

  • 0

EDIT

Found the answer. It’s a logical error located at

if(carry == 0 && index < 0)
    exit = true;

Since each segment starts with 18 digits (hence the index = 17; right before the while loop), when shift has a value less than that the code will keep writing beyond the answer[0]. I have fixed it by adding an additional condition to trip the exit flag.

Sorry for the confusion.

ORIGINAL QUESTION

Here’s a function I wrote to multiply two integers in the format of char arrays with each cell representing a single decimal number (eg. 1234 would be “1234+”).

char* multichar(char* one, char* two)
{
    // one has m digits, two has n digits
    int m = char_size(one) - 1;
    int n = char_size(two) - 1;
    int m_seg = m / 9 + 1;
    int n_seg = n / 9 + 1;
    int m_head = m % 9;
    int n_head = n % 9;
    int index, shift;
    bool exit = false, m_flag = true, n_flag = true;
    _int64 product, alpha, bravo;
    char carry = 0, sum;
    char temp[18];
    char* answer = new char[m + n + 1];
    memset(answer, 0, m + n + 1);
    if(m_head == 0)
    {
        m_seg--;
        m_flag = false;
    }
    if(n_head == 0)
    {
        n_seg--;
        n_flag = false;
    }

    for(int i = n_seg - 1; i > -1; i--)
    {
        for(int j = m_seg - 1; j > -1; j--)
        {
            shift = m + n - (m_seg + n_seg - i - j) * 9 + 17;
            if(i == 0 && n_head != 0)
                bravo = segtoint(two, 0, n_head);
            else
                bravo = segtoint(two, n_head + (i - n_flag) * 9, 9);
            if(j == 0 && m_head != 0)
                alpha = segtoint(one, 0, m_head);
            else
                alpha = segtoint(one, m_head + (j - m_flag) * 9, 9);

            product = alpha * bravo;
            if(product == 0)
                memset(temp, 0, 18);
            else
            {
                for(int k = 17; k > -1; k--)
                {
                    temp[k] = product % 10;
                    product /= 10;
                }
            }
            // add temp to answer from index backwards;
            index = 17;
            exit = false;
            while(!exit)
            {
                if(index < 0)
                    sum = answer[shift] + carry;
                else
                    sum = answer[shift] + temp[index] + carry;
                carry = sum / 10;
                answer[shift] = sum % 10;
                index--;
                shift--;
                if(carry == 0 && index < 0)
                    exit = true;
            }
        }
    }
    answer[m + n] = one[m] == two[n]? '+':'-';

    return answer;
}

In my int main() I put

char* omega = multichar(delta, echo);
delete[] omega;
return 0;

But that results in a BLOCK IS VALID error… Why can’t I delete the pointer?

  • 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-05-26T16:49:08+00:00Added an answer on May 26, 2026 at 4:49 pm

    Your size calculations are off. For example, for char_size(one) == 1 and char_size(one) == 1 you get m = n = 0 and m_seg = n_seg = 1. Then in the first iteration of the loops you have i = 0 and j = 0 and from that:

    shift = 0 + 0 - (1 + 1 - 0 - 0) * 9 + 17;
    

    That is shift = -1. Later in that loop you write to answer[shift], which will be out of bounds of the array.

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

Sidebar

Related Questions

EDIT: Answer found! Thank you very much people, a lot of answers worked, I
EDIT: I actually found the answer. I can't close the question as I am
* EDIT: I found the answer to the memory leak myself, and posted it
EDIT: I found the answer here. Returning the String found with a regular expression
[edit] Found the solution. Reinstall EVERYTHING - xcode, mono, monodevelop and monotouch. Now it
EDIT: I found out that I can get it to compile if I cast
EDIT 9-3-10: I found this blog entry recently that was very enlightening. http://optimizermagic.blogspot.com/2007/12/outerjoins-in-oracle.html There
I have found a few libraries to edit MP3 tags (UltraID3Lib is great) but
EDIT: The entire code and database creation script can be found from http://gitorious.org/scheator .
I've found that on some occasions I can edit the source while debugging. Are

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.