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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:34:29+00:00 2026-05-21T09:34:29+00:00

I’ve written a small program to find the number who is both prime and

  • 0

I’ve written a small program to find the number who is both prime and a factor of a specific number n. I get the number n from a file and print it to another.

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <iostream>
#include <fstream>

using namespace std;
bool PrimeFactor(int );
int countSize(int);


char * p_str = "";//store result of prime factor
int size = 0;//length of p_str

int main(int argc, char** argv) {
std::ifstream fin;
fin.open("input.txt");

std::ofstream fout;
fout.open("output.txt", std::ios::app);

int N;//number to factor

while (fin >> N){   
    //Find prime factor
    PrimeFactor(N);
    fout << p_str;// print string storing result
    fout << endl;
}

fin.close();
fout.close();

return 0;
}

bool PrimeFactor( int n ){
int count = 0;// count divisors

for (int i = 2 ; i < n; i++){
    if ( n % i == 0 ){
        // convert integer to string
        char * tmpstr = new char[ countSize(i) ]  ;// allocate according to size of integer (not waste memory)
        sprintf( tmpstr, "%d ", i); // NOTE : if not have the blank after %d -> no space

        // condition : prime and not duplicate existing number in global string
        if ( PrimeFactor(i) && !strstr( p_str, tmpstr ) ){

            char * new_p = new char [ size + countSize(i) ];
            strcpy( new_p, p_str );// copy the global string to new place
            strcat( new_p, tmpstr );// append new integer value
            p_str = new_p ; // let glocal string be renewed with appending new result
            size = size + countSize(i);
            cout << size << endl;
        }
        count ++;
    }
}

if (count > 0)//not prime
    return false;
return true;
}

//counting the number of digit of an integer
int countSize(int n){
    int count = 0;
    if (n < 10)
        return 1;
    while ( n >= 10 ){
        count++;
        n = n/10;
    }
    return count + 1;
}

Using this approach, the result may be duplicated if I don’t store the founded numbers in a C-string and check whether the next number is already a number in the string. I choose C-string as it is more challenging than std::string.
So the question is about manipulating the string to minimize memory usage. I have to use a global pointer-to-char (as not have to define the size of array of char).
It seems that func CountSize() though return what is needed (length of number in string), the string still waste some piece of memory & the size variable is not what I meant it to be. Also I cant get the size by using sizeof() with a pointer-to-char.
Any one can 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-21T09:34:30+00:00Added an answer on May 21, 2026 at 9:34 am

    OK, so you want to use char* strings, presumably to get a handle on them for the future. That is admirable. But you’re gonna need a crash-course in the commandments of c-string management first…

    Thou shalt pair every new with a delete

    Your goal is to attempt to minimize memory usage right? Well, every time you create a string with new, but then don’t delete it, you are leaking that memory. This is bad practice in general, but also a definite memory waster. In your case you are allocating with new [] to make an array, and new [] calls must be paired with delete []. So in your PrimeFactor function:

    strcpy( new_p, p_str );// copy the global string to new place
    strcat( new_p, tmpstr );// append new integer value
    // delete original contents of p_str
    delete [] p_str;
    p_str = new_p ; // let glocal string be renewed with appending new result
    

    You will also need a delete [] at the very end of your program to clean up the memory of p_str before it exits.

    Thou shalt always make room for a null-terminator

    When the computer is reading a c-string, it doesn’t know beforehand how long it is. If you have a char[100] initialized with the contents “Hi”, how does the computer known to stop after the ‘i’, as opposed to the ‘H’, or the character 5 after ‘i’? C-strings are not valid unless they end with a null-terminator, written as ‘\0’. The null-terminator indicates to the computer, “Ok, we’re done here.” The string’s over. It’s kinda nifty, but problems can arise because the null-terminator takes up a spot in the character array. Storing “Hi” requires a char [3]; — char[0] is ‘H’, char[1] is ‘i’, and char[2] is ‘\0’. So your new string allocation code should look like this:

        char * tmpstr = new char[ countSize(i) + 1 ]  ;// allocate according to size of integer (not waste memory)
        ...
            char * new_p = new char [ size + countSize(i) + 1 ];
    

    Note the + 1s. This is to ensure your strings have room for the Null-terminator.

    Thou shalt use string-safe functions

    sprintf, strcpy, and strcat (and others) have been deprecated in favor of the new sprintf_s, strcpy_s, and strcat_s functions. The _s stands for “safe”. These functions take an extra parameter for the size of the string they are modifying, and ensure that size-limit is not broken. All the string-modifier functions ensure that the prior-mentioned null-terminator is tacked on, but in your code, you weren’t giving them the proper room to do that. So the non-string-safe functions were writing one character PAST your declared memory into unknown memory — bad — very bad. The string-safe versions of those functions would have instead crashed your program with an error, alerting you that something was wrong and needed fixin’. A string-safe implementation of your functions would look like this:

        int tmpstrSize = countSize( i ); // calculate tmpstrSize once
        char * tmpstr = new char[ tmpstrSize + 1 ]  ;// allocate according to size of integer (not waste memory)
        sprintf_s( tmpstr, tmpstrSize + 1, "%d ", i); // NOTE : if not have the blank after %d -> no space
        ...
            int new_pSize = size + tmpstrSize; // calculate new_pSize once
            char * new_p = new char [ new_pSize + 1 ];
            strcpy_s( new_p, new_pSize, p_str );// copy the global string to new place
            strcat_s( new_p, new_pSize, tmpstr );// append new integer value
    

    Now you are nice and safe, and if something goes wrong, you will know.

    Thou shalt write C++ code in a C++ manner

    Truth be told, the program you’ve written above isn’t really C++, it’s C. Sure, it’ll run fine in a C++ environment, but the method is totally C-based. C++ programs use std::strings for strings and std::vectors for lists of integers. So hey, I understand that you want to learn the low-level stuff for the challenge, I’ve been there myself. But once you know how to do it, the C++ functionality that makes basically everything I described above irrelevant for string-handling is a godsend, and you will never, ever want to go back.

    As a small side-note, I recommend checking out the Sieve of Eratosthenes for prime-number checking. It’s a good exercise to implement and will give your code a huge performance boost.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
I have a text area in my form which accepts all possible characters from

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.