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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:50:52+00:00 2026-05-14T14:50:52+00:00

In this small example, c++ forgets size of an array, passed to a constructor.

  • 0

In this small example, c++ forgets size of an array, passed to a constructor. I guess it is something simple, but I cannot see it.

In classes.h, there is this code:

#ifndef CLASSES_INC
#define CLASSES_INC
#include <iostream>

class static_class {
 public:
 static_class(int array[]) {
  std::cout<<sizeof(array)/sizeof(int)<<"\n";
 } 
};

class my_class{
 public:
 static static_class s;
 static int array[4];
};

#endif

In classes.cpp, there is this code:

#include "classes.h"

int my_class::array[4]={1, 2, 3, 4}; 

static_class my_class::s = static_class(my_class::array);

In main.cpp, there is only simple

#include "classes.h"

int main () {

 return 0;
}

Now, the desired output (from the constructor of static_class) is 4. But what I get is 1. Why is that?

edit:

A lot of answers suggest that sizeof on pointer returns the size of a pointer. That is not true, AFAIK (from http://msdn.microsoft.com/en-us/library/4s7x1k91(VS.71).aspx – “When the sizeof operator is applied to an array, it yields the total number of bytes in that array, not the size of the pointer represented by the array identifier.”)

edit2:

ok, as I found out, when compiler can see the size, sizeof returns the whole size, but when it decays to a pointer (which is the case here), sizeof actually really returns size of pointer.

  • 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-14T14:50:52+00:00Added an answer on May 14, 2026 at 2:50 pm

    When declaring a function parameter, the following two are equivalent because an array decays to a pointer when it is passed as an argument:

    int array[]
    int *array;
    

    So, you end up computing sizeof(int*) / sizeof(int), which happens to be one on your platform because their sizes are the same.

    If you need the size of the array in your function, you should pass it as an argument to the function, or use a container that keeps track of the size for you.

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

Sidebar

Ask A Question

Stats

  • Questions 372k
  • Answers 372k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Continuous-speech recognition is a different and more difficult problem than… May 14, 2026 at 7:14 pm
  • Editorial Team
    Editorial Team added an answer Ok, now that I have published my app to the… May 14, 2026 at 7:14 pm
  • Editorial Team
    Editorial Team added an answer Is that so called Debugger? You can install ZendDebugger or… May 14, 2026 at 7:14 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.