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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:21:37+00:00 2026-05-20T08:21:37+00:00

I am getting some weird behavior when using cout in my programm, which is

  • 0

I am getting some weird behavior when using cout in my programm, which is similar to the following:

...
char *input = realpath(argv[1], NULL);
char *output = argv[2];

char *tarout = new char[strlen(output)+6];
strcpy(tarout, output);
strcat(tarout, ".temp");

cout << "Tarout: " << tarout << endl;

int tRet = tarball(input, tarout);
if(tRet != 1) {
    cerr << "Error: Could not compress directory!\nHalting package creation!" << endl;
    return 0;
}

int gRet = gzip(tarout, output);
if(gRet != 1) {
    cerr << "Error: Could not compress directory!\nHalting package creation!" << endl;
    return 0;
} else {
    cout << "TAROUT: " << tarout << endl;
    if((remove(tarout))!=0) {
        cerr << "Warning: Could not delete temporary file!" << endl;
        return 0;
    }
}
...

Basically this program creates a tar file and then compresses it with gzip, this is not the 100% actual code so it may not give the same odd behavior as have I been receiving.

If I removed the first cout << "TAROUT: " << tarout << endl; the second cout << "TAROUT: " << tarout << endl; would return nothing and the temporary file would not get removed, why is that?

  • 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-20T08:21:37+00:00Added an answer on May 20, 2026 at 8:21 am

    new/malloc do not initialize memory, so I’m fairly certain that you have no NULL terminator at the end of tarout.

    I suspect if you run your original code through a debugger or simply print out *(tarout+5), you would see that there is no ‘0’ there.

    Given your comments regrading the use of std::string, I would write:

    const char * file_ext = ".temp";
    
    // no magic numbers...and an obvious + 1 for null terminator
    size_t len = strlen(output)+strlen(file_ext)+1;
    
    char *tarout = new char[len];
    
    memset(tarout, 0, len);
    
    strcpy(tarout, output);
    strcat(tarout, file_ext);
    
    // If you feel memset is wasteful, you can use
    *(tarout+len-1) = 0;
    
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Fluid 960 at the moment, and I'm getting some weird behavior with
I'm getting some weird behavior from IE when trying to change a session variable
I'm getting some weird behavior in asp.net MVC2 (at least it's not what I'm
I'm getting a very strange behavior in MySQL, which looks like some kind of
I'm getting some weird behavior with a vector in C++ I was hoping someone
I'm getting some weird behaviour recompiling some applications in 2009 that used widestrings at
I'm working on a school project and I'm getting some weird errors from Xcode.
I've been struggling with some weird behavior of fop 0.95 (don't know if I'm
Hoo boy. A weird one I guess! getting input from a form, I want
I am just getting started again with Mac development. I am using CoreMIDI which

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.