473,753 Members | 7,825 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
18 2949
Hi, all.

It's all my fault putting this both in comp.lang.c and comp.lang.c++. Very
sorry for that.

This fragment code is extracted out from a class member function. In
addition, those codes are compiled by gcc. I'm not familar with C++
standard. As far as I know, the variable length array is introduced in C99,
but I do NOT know whether C++ has this. So I cross-posted it.

It should be
void Componet::setVa lue(int n)
{
int size = getValueLength( );
int buffer_p[size];

if (buffer_p)
{
...
}
}

ATTENTION:
=============== =============== =============== =============== =============== =
=============== ====
Any one who answers my question, please post ONLY in comp.lang.c from now
on. Thank all of you.
=============== =============== =============== =============== =============== =
=============== ====

Best Regards,

Xiangliang Meng

"Xiangliang Meng" <xi************ *@hotmail.com> wrote in message
news:ca******** **@zcars0v6.ca. nortel.com...
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 #11
"Xiangliang Meng" <xi************ *@hotmail.com> wrote:
It's all my fault putting this both in comp.lang.c and comp.lang.c++. Very
sorry for that.

This fragment code is extracted out from a class member function.
That should have told you; C does not have, never has had, and by the
grace of the Committee never will have classes.
It should be
void Componet::setVa lue(int n)
This is _not_ C. Whether it is valid C++ I do not know, but it is not C
- neither C89 nor C99 nor even Ganuck.
{
int size = getValueLength( );
int buffer_p[size];
Therefore, talking about this code as if it is guaranteed to have C99
semantics is misleading.
Any one who answers my question, please post ONLY in comp.lang.c from now
on. Thank all of you.


No, do _not_ answer this in comp.lang.c. Whatever it is, it is some kind
of (possibly Ganuck++-specific) C++. It is certainly not C, therefore it
does not belong in comp.lang.c. Follow-ups set the other way.

Richard

[ Learn to snip, btw. ]
Jul 22 '05 #12
* Xiangliang Meng:

It's all my fault putting this both in comp.lang.c and comp.lang.c++. Very
sorry for that.

This fragment code is extracted out from a class member function. In
addition, those codes are compiled by gcc. I'm not familar with C++
standard. As far as I know, the variable length array is introduced in C99,
but I do NOT know whether C++ has this. So I cross-posted it.

It should be
void Componet::setVa lue(int n)
{
int size = getValueLength( );
int buffer_p[size];

if (buffer_p)
{
...
}
}


Above you're using a C99 feature in a C++ program.

C99 does not have classes, and C++ does not have variable length arrays.

So the mix of the two features is something not allowed in either
language.

However, in C++ you can use the standard library's vector class to
achieve the same effect, as follows:

void Componet::setVa lue( int n )
{
int size = getValueLength( );
std::vector<int > buffer( size );
...
}

and in C99 you can rewrite the thing as e.g.

void Componet_setval ue( Component* self, int n )
{
int size = self->getValueLength ();
if( size == 0 ) { return; }
int buffer[size];
...
}

But first of all you need to decide which language you're using.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #13
Jowtte wrote:
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.


I think the standard dictates that calls to new with size 0 will return
a valid pointer, therefore this is well-defined behavior (although what
really happens is likely to be an allocation of 1, or sizeof(T);
Jul 22 '05 #14
On Thu, 10 Jun 2004 10:08:51 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote:
This is _not_ C. Whether it is valid C++ I do not know, but it is not C
- neither C89 nor C99 nor even Ganuck.


<OT>
Ganuck? What is this?
</OT>

--
Andrew
Jul 22 '05 #15

"Xiangliang Meng" <xi************ *@hotmail.com> wrote in message
news:ca******** **@zcars0v6.ca. nortel.com...
............... ...........
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?


This is g++ specific extension in this case.
If size is 0 then nothing will happen, though you can't access
array elements 'cause there are none.
In order to avoid this situation use this:
void Componet::setVa lue(int n)
{
int size = getValueLength( );
int buffer_p[size];
//> if (buffer_p)
// buffer_p would be always non null pointer
if(sizeof buffer_p) // sizeof would be evaluated in run time {
...
}
}


Finally, I have question for C newsgroup.
Given,

void func(size_t size)
{
assert(size != 0);
int buf[size];
size_t tmp= sizeof (buf);
}

what would be result of sizeof? Some compile time constant
or run time evaluated value?

Greetings, Bane.
Jul 22 '05 #16
Jorge Rivera wrote:
Jowtte wrote:
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.


I think the standard dictates that calls to new with size 0 will return
a valid pointer, therefore this is well-defined behavior (although what
really happens is likely to be an allocation of 1, or sizeof(T);


This is not valid C code, and is OT in c.l.c. F'ups set.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Jul 22 '05 #17
Xiangliang Meng wrote:

It's all my fault putting this both in comp.lang.c and
comp.lang.c++. Very sorry for that.
.... snip ...
ATTENTION:
=============== ====
Any one who answers my question, please post ONLY in comp.lang.c
from now on. Thank all of you.


You have a simple mechanism available for that, which should have
been applied to the original post. Set followups.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Jul 22 '05 #18
On Thu, 10 Jun 2004 15:41:00 +0200, "Branimir Maksimovic"
<bm***@eunet.yu > wrote in comp.lang.c:

"Xiangliang Meng" <xi************ *@hotmail.com> wrote in message
news:ca******** **@zcars0v6.ca. nortel.com...
............... ..........
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?


This is g++ specific extension in this case.
If size is 0 then nothing will happen, though you can't access
array elements 'cause there are none.
In order to avoid this situation use this:
void Componet::setVa lue(int n)
{
int size = getValueLength( );
int buffer_p[size];

//> if (buffer_p)
// buffer_p would be always non null pointer
if(sizeof buffer_p) // sizeof would be evaluated in run time
{
...
}
}


Finally, I have question for C newsgroup.
Given,

void func(size_t size)
{
assert(size != 0);
int buf[size];
size_t tmp= sizeof (buf);
}

what would be result of sizeof? Some compile time constant
or run time evaluated value?

Greetings, Bane.


The sizeof operator in C99 is evaluated at run time when it is applied
to a VLA. In all other cases it is the same as before, a compile time
operation that yields a compile time constant.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #19

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

Similar topics

140
7878
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
2651
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
5753
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.
0
8896
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9421
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
9333
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...
0
8328
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6869
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
6151
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();...
2
2872
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2284
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.