Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > What will happen when the size of a local variable length array turns out to be 0 (zero)?

Reply
Thread Tools

What will happen when the size of a local variable length array turns out to be 0 (zero)?

 
 
Xiangliang Meng
Guest
Posts: n/a
 
      06-10-2004
Hi.

void setValue(int n)
{
int size = getValueLength();
int buffer_p[size];

if (buffer_p)
{
....
}
}
When the local variable size is 0, will buffer_p be a null pointer? I doubt
it.

What will happen when the size of a local variable length array turns out to
be 0 (zero)? Is it an undefined behavior? Or an implementation-defined one?

Thank you.

Best Regards,

Xiangliang Meng



 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-10-2004
"Xiangliang Meng" <(E-Mail Removed)> wrote...
> void setValue(int n)
> {
> int size = getValueLength();
> int buffer_p[size];


This is not C++.

>
> if (buffer_p)
> {
> ....
> }
> }
> When the local variable size is 0, will buffer_p be a null pointer? I

doubt
> it.


It won't compile.

> What will happen when the size of a local variable length array turns out

to
> be 0 (zero)? Is it an undefined behavior? Or an implementation-defined

one?

It's a semantic error. The expression in brackets has to be
a compile-time constant.

Victor


 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      06-10-2004
On Thu, 10 Jun 2004 04:11:54 GMT, "Victor Bazarov"
<(E-Mail Removed)> wrote:

>"Xiangliang Meng" <(E-Mail Removed)> wrote...
>> void setValue(int n)
>> {
>> int size = getValueLength();
>> int buffer_p[size];

>
>This is not C++.


No, it's perfectly good C99.

>
>>
>> if (buffer_p)
>> {
>> ....
>> }
>> }
>> When the local variable size is 0, will buffer_p be a null pointer? I

>doubt
>> it.

>
>It won't compile.
>
>> What will happen when the size of a local variable length array turns out

>to
>> be 0 (zero)? Is it an undefined behavior? Or an implementation-defined

>one?
>
>It's a semantic error. The expression in brackets has to be
>a compile-time constant.


Not in C99.


<<Remove the del for email>>
 
Reply With Quote
 
Jowtte
Guest
Posts: n/a
 
      06-10-2004
Consider this code:

int testAlloc()
{
int size = 0;
int* buffer = new int [ size ];
int* buffer2 = new int [ 1 ];
...
}

It does allocate memory for buffer, yet I don't know how large it is.

Using my compiler, before "new" operating, buffer = buffer2 = 0xcccccccc
and after "new", buffer = 0x0x00430060, buffer2 = 0x00430030

I don't know why.

"Victor Bazarov" <(E-Mail Removed)> 写入消息
news:ecRxc.10164$0y.743@attbi_s03...
> "Xiangliang Meng" <(E-Mail Removed)> wrote...
> > void setValue(int n)
> > {
> > int size = getValueLength();
> > int buffer_p[size];

>
> This is not C++.
>
> >
> > if (buffer_p)
> > {
> > ....
> > }
> > }
> > When the local variable size is 0, will buffer_p be a null pointer? I

> doubt
> > it.

>
> It won't compile.
>
> > What will happen when the size of a local variable length array turns

out
> to
> > be 0 (zero)? Is it an undefined behavior? Or an implementation-defined

> one?
>
> It's a semantic error. The expression in brackets has to be
> a compile-time constant.
>
> Victor
>
>



 
Reply With Quote
 
Dave Townsend
Guest
Posts: n/a
 
      06-10-2004
0xcccccc is just MSVC++ fill pattern for uninitialized variables
when you run in debug mode. If you look closely, you'll see size
has the same pattern before its initialized.


"Jowtte" <(E-Mail Removed)> wrote in message
news:ca8ph1$1gka$(E-Mail Removed)99.com...
> Consider this code:
>
> int testAlloc()
> {
> int size = 0;
> int* buffer = new int [ size ];
> int* buffer2 = new int [ 1 ];
> ...
> }
>
> It does allocate memory for buffer, yet I don't know how large it is.
>
> Using my compiler, before "new" operating, buffer = buffer2 = 0xcccccccc
> and after "new", buffer = 0x0x00430060, buffer2 = 0x00430030
>
> I don't know why.
>
> "Victor Bazarov" <(E-Mail Removed)> 写入消息
> news:ecRxc.10164$0y.743@attbi_s03...
> > "Xiangliang Meng" <(E-Mail Removed)> wrote...
> > > void setValue(int n)
> > > {
> > > int size = getValueLength();
> > > int buffer_p[size];

> >
> > This is not C++.
> >
> > >
> > > if (buffer_p)
> > > {
> > > ....
> > > }
> > > }
> > > When the local variable size is 0, will buffer_p be a null pointer? I

> > doubt
> > > it.

> >
> > It won't compile.
> >
> > > What will happen when the size of a local variable length array turns

> out
> > to
> > > be 0 (zero)? Is it an undefined behavior? Or an implementation-defined

> > one?
> >
> > It's a semantic error. The expression in brackets has to be
> > a compile-time constant.
> >
> > Victor
> >
> >

>
>



 
Reply With Quote
 
those who know me have no need of my name
Guest
Posts: n/a
 
      06-10-2004
[fu-t set]

in comp.lang.c i read:

>Hi.


hello. please do not cross-post between comp.lang.c and comp.lang.c++
unless you are reasonably certain the question and/or answer can or will
apply equally well to both languages -- not the case here.

> int size = getValueLength();
> int buffer_p[size];


this is not valid c++. as such the remainder of this response is c
oriented, and is the reason behind the followup-to header i've used.

in c you would have a vla with no usable elements.

> if (buffer_p)


>When the local variable size is 0, will buffer_p be a null pointer? I doubt
>it.


buffer_p is not a pointer, ever, in any way. when you use buffer_p the
value it yields is a pointer (to the first element), but buffer_p itself
remains an array. when the vla is zero sized buffer_p will not yield a
null pointer value, it will have a distinct non-null address.

>What will happen when the size of a local variable length array turns out to
>be 0 (zero)? Is it an undefined behavior? Or an implementation-defined one?


declaring an array with a constant expression that evaluates to zero is a
constraint violation, but it is valid and well defined for a vla. of
course none of the elements may be accessed -- attempting to do so has
undefined behavior -- so, about the only things you can do involve the &
and sizeof operators.

--
a signature
 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      06-10-2004
On 10 Jun 2004 04:54:05 GMT in comp.lang.c++, Barry Schwarz
<(E-Mail Removed)> wrote,
>On Thu, 10 Jun 2004 04:11:54 GMT, "Victor Bazarov"
><(E-Mail Removed)> wrote:
>
>>"Xiangliang Meng" <(E-Mail Removed)> wrote...
>>> void setValue(int n)
>>> {
>>> int size = getValueLength();
>>> int buffer_p[size];

>>
>>This is not C++.

>
>No, it's perfectly good C99.


What part of "This is not C++" do you not understand.
As long as you are crossposting your answers to comp.lang.c++,
please confine yourself to standard conforming C++ code.

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      06-10-2004
"Xiangliang Meng" <(E-Mail Removed)> wrote:

> void setValue(int n)
> {
> int size = getValueLength();
> int buffer_p[size];
>
> if (buffer_p)
> {
> ....
> }
> }
> When the local variable size is 0, will buffer_p be a null pointer? I doubt
> it.


Please do not cross-post questions like these to comp.lang.c and
comp.lang.c++. C and C++ are different languages, and some of the most
fundamental differences can be found in the area of memory allocation
and the type system. Therefore, the answer may well be different
depending on whether you're writing C or C++. I'll answer this question
presuming you want a C answer, and have set follow-ups accordingly.


Paragraph 6.7.5.2 of the C99 Standard says, amongst other things,

# If the size expression is not a constant expression, and it is
# evaluated at program execution time, it shall evaluate to a
# value greater than zero.

Since this appears outside the "constraints" part of that paragraph,
violating it causes undefined behaviour. IOW, if, in your code, size
turns out to be 0, the array declaration is allowed to result in
anything at all. A null pointer is allowed; an array of one member is
allowed; crashing the program is allowed, too.

Richard
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      06-10-2004
David Harmon <(E-Mail Removed)> wrote:

> <(E-Mail Removed)> wrote,
> ><(E-Mail Removed)> wrote:
> >
> >>"Xiangliang Meng" <(E-Mail Removed)> wrote...
> >>> int size = getValueLength();
> >>> int buffer_p[size];
> >>
> >>This is not C++.

> >
> >No, it's perfectly good C99.

>
> What part of "This is not C++" do you not understand.
> As long as you are crossposting your answers to comp.lang.c++,
> please confine yourself to standard conforming C++ code.


Since it is also cross-posted to comp.lang.c, please confine yourself to
Standard-conforming C code.

The moral of this post? Don't cross-post between c.l.c and c.l.c++;
you'll only make posters cross.

Richard
 
Reply With Quote
 
Grumble
Guest
Posts: n/a
 
      06-10-2004
David Harmon wrote:

> On 10 Jun 2004 04:54:05 GMT in comp.lang.c++, Barry Schwarz
> <(E-Mail Removed)> wrote,
>
>> On Thu, 10 Jun 2004 04:11:54 GMT, "Victor Bazarov"
>> <(E-Mail Removed)> wrote:
>>
>>> "Xiangliang Meng" <(E-Mail Removed)> wrote...
>>>
>>>> void setValue(int n)
>>>> {
>>>> int size = getValueLength();
>>>> int buffer_p[size];
>>>
>>> This is not C++.

>>
>> No, it's perfectly good C99.

>
> What part of "This is not C++" do you not understand. As long as you
> are crossposting your answers to comp.lang.c++, please confine
> yourself to standard conforming C++ code.


I think Barry meant:

"No [it is not C++ indeed], [however] it's perfectly good C99."

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
does value-initialization happen for global and local static object ? subramanian100in@yahoo.com, India C++ 1 04-21-2008 03:17 AM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Flexible array member + variable length array Adam Warner C Programming 10 02-10-2005 10:11 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
What will happen when the size of a local variable length array turns out to be 0 (zero)? Xiangliang Meng C Programming 22 06-11-2004 11:20 AM



Advertisments