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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:19:51+00:00 2026-06-17T16:19:51+00:00

I made a simple test program to play around with C++11 threads. #include <iostream>

  • 0

I made a simple test program to play around with C++11 threads.

#include <iostream>
#include <stdlib.h>
#include <thread>
using namespace std;

void tee(int civ)
{
    for(int loop=0; loop<19; loop++, civ++)
    {
        civ = civ%19;
        cout << loop << "\t" << civ << endl;
        this_thread::sleep_for(chrono::milliseconds(300));
    }
}

void koot()
{
    while(true)
    {
        cout << ":) ";
        this_thread::sleep_for(chrono::milliseconds(300));
    }
}

int main(int argc, char *argv[])
{
    thread saie(tee, atoi(argv[1])),
        kamaa(koot);
    saie.join();
    kamaa.join();

    return 0;
}

It works fine as long as I supply command line arguments, but if I don’t, it crashes.
How can this be solved?
I tried checking the argument count, and if they existed, to no avail.

EDIT: I had to add this line:

if(argc < 2) return 1;
  • 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-17T16:19:53+00:00Added an answer on June 17, 2026 at 4:19 pm

    It crashes because you are accessing

    argv[1]
    

    which would hold a command line argument (other than the program’s name) if there was one. You should check whether argc is greater than 1. Why greater than 1? Because the first command line argument is the name of the program itself. So argc is always greater than 0. And indexing starts at 0. So if argc == 1, only argv[0] is valid.

    #include <iostream>
    int main(int argc, char* argv[])
    {
      // no need to check argc for argv[0]
      std::cout << argc << " " << argv[0] << "\n";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a simple, test program to experiment using classes. It creates a
I've made this simple test program, and it sometimes triggers a Debug Break or
I just made a very simple test app using documentation from MSDN. All I
I have a made a simple program that reproduces the problem: #include <boost/archive/text_oarchive.hpp> #include
I have made a simple test application for the issue, two winforms each containing
I've made a simple test with a MemoryMappedFile as msdn says : 2 processes,
I'm putting together a simple test made up of two tutorials available online for
OK, I have made simple rectangular using OpenGL, and it looks pretty simple glNormal3f(0.0f,
On Windows, testing different OSes is made simple using VMs. Is there a simple
I made a very simple spinlock using the Interlocked functions in Windows and tested

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.