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 6626031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:50:44+00:00 2026-05-25T21:50:44+00:00

I want to build a program which sum a big integers in C. So

  • 0

I want to build a program which sum a big integers in C.
So I’m ready with the code. The program compiling pass successfully with mingw and Visual C++ compiler. But I have a problem with the run part. The strange thing is that when I debug the program in Visual Studio there is no problems but when I run it my Windows show that the program stop working.
This is the code :

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include "sum.h"

int isNumber(char* number)
{
    int lenght = strlen(number);
    int i,result = 0;
    for (i = 0 ; i < lenght ; i++)
    {
        if (isdigit(*number++))
        {
            result = 1;
        }
        else
        {
            result = 0;
            break;
        }
    }
    return result;
}
int cti(char ch)
{
    return ch - '0';
}
char* addZeros(char* number,int lenght)
{
    int num_lenght = strlen(number),size = abs(lenght - num_lenght),i;
    char* zeros = (char*)malloc(size);

    strcpy(zeros,"");
    zeros[size - 1] = '\0';
    for (i = 0 ; i < abs(lenght - num_lenght) ; i++)
    {
        strcpy(&zeros[i],"0");
    }
    strncat(zeros,number,size);
    number = (char*)malloc(sizeof(char)*size);
    strncpy(number,zeros,size);
    return number;
}
char* sum(char* numberOne,char* numberTwo)
{
    if (numberOne == NULL || numberTwo == NULL)
        return NULL;
    if (!isNumber(numberOne) || !isNumber(numberTwo))
        return NULL;

    int CF = 0;
    int lenghtOne = strlen(numberOne),lenghtTwo = strlen(numberTwo);
    if (lenghtOne == 0 || lenghtTwo == 0)
        return lenghtOne == 0 ? numberTwo : numberOne;
    int max = lenghtOne > lenghtTwo? lenghtOne : lenghtTwo,index;
    char* result = (char*)malloc(max);
    int res = 0;
    result[max] = '\0';
    if (lenghtOne > lenghtTwo) numberTwo=addZeros(numberTwo,strlen(numberOne));
    else if (lenghtOne < lenghtTwo) numberOne = addZeros(numberOne,strlen(numberTwo));
    for ( index = max - 1 ; index >=0 ; index--)
    {
        res = cti(numberOne[index]) + cti(numberTwo[index]);
        if (((res%10) + CF) > 9) 
        {
            int num = ((res%10) + CF);

            result[index] = (char)((int)'0'+num%10);
            CF = num / 10;
        }
        else
        {
            result[index] = (char)((int)'0'+((res%10) + CF));
            CF = res / 10;
        }
    }
    return result;
}
int main(int argc, char *argv[])
{
    char* num = "12345678";
    char* num2= "2341";
    char* result = sum(num,num2);
    printf("%s\n",result);
    return 0;
}

I think that the problem is somewhere in the pointers but I’m not absolutely sure about this. Can anyone help me?

  • 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-25T21:50:45+00:00Added an answer on May 25, 2026 at 9:50 pm

    The amount of memory you are allocating is not sufficient. It does not include space for the null character terminating the string and it does not take into account changes in the length of the result for sums such as “9” + “1”. You are then writing the null terminating character after the end of the array.

    You should malloc with the length like this:

    char* result = (char*)malloc(max + 2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to build my sources by Mingw compiler which in not placed on
I have a quite big program in .net 3.5 SP1 which is compiling just
I would like to build a program which takes a username as parameter and
I want to build a back end of a general web form which contains
I want to get started using Boost. I'm programming a C++ program in Visual
I'm making a program which the user build directories (not in windows, in my
I want to program a chess engine which learns to make good moves and
I want to make a program which is able to dlopen() a series of
I have a WPF program that I'm developing in which I want to catch
I just wrote a little program which will be executed as a post-build step

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.