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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:50:23+00:00 2026-06-05T22:50:23+00:00

I’m trying to do my first baby steps with C++. Can somebody explain why

  • 0

I’m trying to do my first baby steps with C++.

Can somebody explain why this does not work the way I’d expect?

#include <iostream>
#include <stdio.h>

using namespace std;

int main (int argc, char *argv[]) {
    int i;
    printf("[D] sizeof argv is: %d\n", sizeof(argv) );
    printf("[D] sizeof int  is: %d\n", sizeof(int) );
    printf(
        "[D] sizeof argv / sizeof int is: %d\n",
        sizeof(argv) / sizeof(int)
    );

    for (i = 0; i < (sizeof(argv) / sizeof(int)); i++ ) {
        printf("[D] i is: %d\n", i );
        cout << argv[i] << endl;
    }

    if (cout.fail()) {
        printf("cout failed!\n");
    }

    cout << "Hello world" << endl;
}

Now the testing:

aloism@azzgoat:~/sandbox/tuts$ g++ string02-d.cpp
aloism@azzgoat:~/sandbox/tuts$ ./a.out hello world
[D] sizeof argv is: 8
[D] sizeof int  is: 4
[D] sizeof argv / sizeof int is: 2
[D] i is: 0
./a.out
[D] i is: 1
hello
Hello world
aloism@azzgoat:~/sandbox/tuts$ ./a.out
[D] sizeof argv is: 8
[D] sizeof int  is: 4
[D] sizeof argv / sizeof int is: 2
[D] i is: 0
./a.out
[D] i is: 1
cout failed!
aloism@azzgoat:~/sandbox/tuts$ echo $?
0
aloism@azzgoat:~/sandbox/tuts$

Why when there is only one item in argv (argv[0], being “./a.out”), the “Hello world” is skipped? I can’t find any reason at all why this would be skipped, no matter the argc or argv!

The OS is Debian Squeeze on 64-bit (so sizeof(int) == 4).

Update: Removed #include noise, added some debugging.

Update 2: Added some debugging based on user315052’s answer. (Now it shows that he’s right.)

  • 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-05T22:50:24+00:00Added an answer on June 5, 2026 at 10:50 pm

    Edit: since undefined behavior is invoked, anything can happen. Adjusting answer.

    The “Hello World” is probably not being skipped. Realize your for loop is improper. One reason that it may not be appearing is that you have printed something that is covering up the appearance of the message. To test this, redirect the output into something that will reveal binary contents of the output stream.

    ./a.out | xxd
    

    Another reason may be that the output is being blocked because std::cout suffered a failure from an attempt to print a NULL pointer as a string. To test for this, check for std::cout.fail() after the print statement in your loop, and direct a message to std::err if you encounter one.

    The program may also crash, as indicated by Boann. However, the operating system would have provided a diagnostic to that effect, and you reported no such behavior.

    For arrays that are function parameters, the compiler treats them as a pointer. This is a behavior inherited from C, where arrays are passed by “reference” by allowing the name of an array to decay to the address of its first element (with a few exceptions, sizeof being one of them). So, using sizeof to determine the number of elements in argv won’t work. Both C and C++ provide two ways to determine when the end of the argv vector is reached. One is argc which is the argument count. The second is that the last element of argv must be a NULL pointer.

    The next time you have a true array, you can calculate the number of elements with sizeof using the following:

    SomeThing array[] = { /* ... things ... */ };
    size_t array_size = sizeof(array)/sizeof(array[0]);
    

    This way, if SomeThing changes, the array size is still properly calculated.

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

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:

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.