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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T06:12:31+00:00 2026-06-11T06:12:31+00:00

A C++ program consists of two .cpp files, main.cpp and f.cpp. Code of the

  • 0

A C++ program consists of two .cpp files, main.cpp and f.cpp. Code of the main.cpp file is as follows:

//main.cpp
#include <iostream>
using namespace std;

void f(char* s,int n);

const int N=10;
static char s[N];
static char a[N];

int main ()
{
    int i;  
    for (i=0; i<N; i++)
        a[i]='0'+i;

    for (i=0; i<N; i++)
        cout<<a[i]; 
    cout<<'\n';

    f(s,N);

    for (i=0; i<N; i++)
        cout<<a[i]; 

    cout<<'\n';
}

The function f is defined in the file f.cpp. The program compiles without errors & warnings. When executed, the program regularly ends, leaving the following on cout:

0123456789
!123456789

What’s your comment on validity and behavior of this program? Explain in detail.

I suppose the f function is illegally accessing a’s memory somehow, maybe because s is right before a in memory, so something with indexes occurs… I’m really not sure, however, because I guess static also changes things somehow..

  • 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-11T06:12:32+00:00Added an answer on June 11, 2026 at 6:12 am

    It depends on the compiler. With gcc-4.3.4, if you can define f as:

    void f(char* s,int n) { s[-n] = '!' ; } //danger!
    

    to produce that output which you have posted. Here is online demo : http://ideone.com/8YT7k

    But be warned that such coding is really really bad, and you should not be coding like that, as the actual behavior depends on the compiler, its version, settings, and options.

    Whether static arrays are placed near each other or not depends on the compiler, and your program shouldn’t assume that. In this case, however, they are placed adjacent, but then it seems to depend on the other factors. For example, if I print the address of s and a at the end of your code, without removing anything from your code, then s has higher address than a, but if I removed your code, then a has higher address than s. See yourself:

    • http://ideone.com/HTtKn (s has higher address than a)
    • http://ideone.com/ZVSjk (a has higher address than s)

    The first one prints:

    0x804a0df   //address of s
    0x804a0d5   //address of a
    0xa         //difference of s and a i.e (s-a)
    

    but then second one prints this:

    0x804a0d5   //address of s
    0x804a0df   //address of a
    0xa         //difference of a and s i.e (a-s) [reversed!]
    

    So it depends on the compiler’s mood where it places the static arrays!

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

Sidebar

Related Questions

I have a very small c++ program that consists of two files: main.cpp and
I have a program which consists of two simple Java Swing files. How do
I'm having trouble with the linking of my files. Basically, my program consists of:
I am working on a program which consists of two parts Service component will
When you're writing a program that consists of a small bit of main logic
THIS IS HOMEWORK: I have a program that consists of two winforms and three
The code below consists of two classes : SmartForm (simple model class) SmartForms (plural
I have two forms, let's call them Main and Form2 . Main form consists
My program consists of 3 main 'sections'. Main function, Login form and App form.
I have a program that consists of two programs: Updater and WorkMaker. Whenever there

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.