Question regarding const property of sizeof

S

somenath

Hi All,

The sizeof operator is evaluated at compile time .so it can be treated
as compile time constant expression.

But when I compile the following program in g++
#include<stdio.h>
int main (void)
{
size_t size = sizeof(int);
int ar[size]={0} ;
return 0;
}
I get the error

$ g++ size_of_test.cpp
size_of_test.cpp: In function `int main()':
size_of_test.cpp:5: error: variable-sized object `ar' may not be
initialized

Why it says ar is variable sized object?

But when I compile the following program it compiles with out error

#include<stdio.h>
int main (void)
{
size_t size = sizeof(int);
int ar[4]={0} ;
return 0;
}

Regards,
Somenath
 
S

Sana

Hi All,

The sizeof operator is evaluated at compile time .so it can be treated
as compile time constant expression.

But when I compile the following program in g++
#include<stdio.h>
int main (void)
{
        size_t size = sizeof(int);
        int ar[size]={0} ;
        return 0;}

in your code the variable 'size' isn't constant. try

size_t const size = sizeof(int);
 
S

SG

Hi All,

The sizeof operator is evaluated at compile time .so it can be treated
as compile time constant expression.

But when I compile the following program in g++
#include<stdio.h>
int main (void)
{
        size_t size = sizeof(int);
        int ar[size]={0} ;
        return 0;}

You need to make 'size' const:

int main (void)
{
const size_t size = sizeof(int);
int ar[size]={0} ;
return 0;
}

Cheers!
SG
 
Z

zhangyw80

The sizeof operator is evaluated at compile time .so it can be treated
as compile time constant expression.
But when I compile the following program in g++
#include<stdio.h>
int main (void)
{
size_t size = sizeof(int);
int ar[size]={0} ;
return 0;}

You need to make 'size' const:

int main (void)
{
const size_t size = sizeof(int);
int ar[size]={0} ;
return 0;
}

Cheers!
SG

using the following code directly is ok too:
int ar[sizeof(int)] = {0};
 
Z

zhangyw80

The sizeof operator is evaluated at compile time .so it can be treated
as compile time constant expression.
But when I compile the following program in g++
#include<stdio.h>
int main (void)
{
size_t size = sizeof(int);
int ar[size]={0} ;
return 0;}

You need to make 'size' const:

int main (void)
{
const size_t size = sizeof(int);
int ar[size]={0} ;
return 0;
}

Cheers!
SG

using the following code directly is ok too:
int ar[sizeof(int)] = {0};
of course, readability is no so good
 
J

Juha Nieminen

SG said:
int main (void)
{
const size_t size = sizeof(int);
int ar[size]={0} ;
return 0;
}

Btw, out of curiosity: What is the official type returned by sizeof?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top