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

  • Home
  • SEARCH
  • 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 6004477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:16:07+00:00 2026-05-23T01:16:07+00:00

This is my c++ code: class base { public: int bval; base(){ bval=0;} void

  • 0

This is my c++ code:

class base
{
public:
    int bval;
    base(){ bval=0;}
    void give() { std::cout<<bval<<":"; }
};

void SomeFunc(base *arr,int size)
{
    for(int i=0; i<size; i++,arr++)
        std::cout<<arr->bval;

    std::cout<<"\n";
    for(int i=0; i<size; i++,arr++)
        arr->give();

    std::cout<<"\n";
}

void test_case4()
{
    base BaseArr[5];
    SomeFunc(BaseArr,6);
}

I am getting following output for above code:

000003379188
0:-1079809464:134515567:134515888:0:-1079809336:

Why isn’t the second line same as the first and why I am getting a output for the 6th loop in SomeFunc when that place hasn’t been allocated, I mean there is no code for give() function to execute.

One more thing I know that sizeof operator on a object gives only the size of its data elements not the function that object contain. So if the functions are not located inside the object memory space then where are they stored?

EDIT:

I intentionally incremented the arr for 6th time just to check what happens on executing give() function that isn’t located on the 6th position.

See the comment to Cem Kalyoncu reply.
Please also answer the first part of my question.

  • 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-23T01:16:08+00:00Added an answer on May 23, 2026 at 1:16 am

    Your array, BaseArr has 5 elements, not 6. Its upper bound is 4.

    base BaseArr[5];
    SomeFunc(BaseArr,5); //should fix it.
    

    EDIT:
    For why you are getting output: in C arrays doesnt have fixed limits, you can easily go out of bounds without getting any errors. Result is generally catastrophic. You will never know what will happen.

    EDIT 2:
    Today my mind works extra slow, now I understand why you are puzzled. Ok, even if the object pointed by arr is not valid. It is still a memory location. While calling arr->give you are actually calling a function similar to give(arr) . since arr is just a pointer to a memory location call is valid and will not raise any errors, like in c. However, some debuggers are able to notice that you run out of bounds. It is for this purpose vectors are always recommended.

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

Sidebar

Related Questions

Suppose I have some code like this: class Base { public: virtual int Foo(int)
Here is my code - #include<iostream> using namespace std; class base { public: void
Suppose I have this code class Base{ public: int getVal(); private: int a, b;
Why does this code: class A { public: explicit A(int x) {} }; class
I have something like this: class Base { public: static int Lolz() { return
#include<iostream> using namespace std; class A { public: int i; A() {cout<<A()<<endl;} ~A() {cout<<~A()<<endl;}
Given this code: public class Car { public virtual int CarId { get; set;
Consider this demo program: #include <stdio.h> class Base { public: virtual int f(int) =0;
I'm trying to compile a POCO with this code public class MenuItem { public
take a look at this example code: public class Comment { private Comment() {

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.