473,714 Members | 2,623 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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

Jul 22 '05 #1
18 2942
"Xiangliang Meng" <xi************ *@hotmail.com> 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
Jul 22 '05 #2
On Thu, 10 Jun 2004 04:11:54 GMT, "Victor Bazarov"
<v.********@com Acast.net> wrote:
"Xiangliang Meng" <xi************ *@hotmail.com> 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>>
Jul 22 '05 #3
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" <v.********@com Acast.net> дÈëÏûÏ¢
news:ecRxc.1016 4$0y.743@attbi_ s03...
"Xiangliang Meng" <xi************ *@hotmail.com> 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

Jul 22 '05 #4
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" <jo****@hotmail .com> wrote in message
news:ca******** ***@mail.cn99.c om...
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" <v.********@com Acast.net> дÈëÏûÏ¢
news:ecRxc.1016 4$0y.743@attbi_ s03...
"Xiangliang Meng" <xi************ *@hotmail.com> 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


Jul 22 '05 #5
[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
Jul 22 '05 #6
On 10 Jun 2004 04:54:05 GMT in comp.lang.c++, Barry Schwarz
<sc******@deloz .net> wrote,
On Thu, 10 Jun 2004 04:11:54 GMT, "Victor Bazarov"
<v.********@co mAcast.net> wrote:
"Xiangliang Meng" <xi************ *@hotmail.com> 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.

Jul 22 '05 #7
"Xiangliang Meng" <xi************ *@hotmail.com> 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 "constraint s" 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
Jul 22 '05 #8
David Harmon <so****@netcom. com.invalid> wrote:
<sc******@deloz .net> wrote,
<v.********@co mAcast.net> wrote:
"Xiangliang Meng" <xi************ *@hotmail.com> 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
Jul 22 '05 #9
David Harmon wrote:
On 10 Jun 2004 04:54:05 GMT in comp.lang.c++, Barry Schwarz
<sc******@deloz .net> wrote,
On Thu, 10 Jun 2004 04:11:54 GMT, "Victor Bazarov"
<v.********@com Acast.net> wrote:
"Xiangliang Meng" <xi************ *@hotmail.com> 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."

Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

140
7844
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
6
2649
by: Niklaus | last post by:
Hi, Can someone point out what is wrong with this code ? How can i make it better optimize it. When run it gives me seg fault in linux. But windows it works fine(runs for a long time). Do we have something like stack size growing enormously and then saying you can't access ,so a segfault ? It would be helpful if someone can run the code and give me the output. It takes a long time on my PC.
22
411
by: Xiangliang Meng | last post by:
Hi. void setValue(int n) { int size = getValueLength(); int buffer_p; if (buffer_p) { ....
89
5735
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the right thing to use for every var that holds the number of or size in bytes of things. * size_t should only be used when dealing with library functions.
1
9074
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9015
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6634
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5947
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4464
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4725
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2520
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2110
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.