473,473 Members | 2,155 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

is variant-length array declaration standard compatible?

Hi, everyone,
Is the following code

<CODE>
void foo(int n)
{
int a[n];
.....
}
</CODE>

standard compliant and portable?

I use intel's, M$'s, borland's Digital Mars's and GUN(c89)'s compiler
compiled it, the first three report error but the last two were passed
silently. I want to know whether that code is portable.

Ths in advance.
£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£* £*£*
Best regards,
Nov 14 '05 #1
12 1801
bingfeng wrote:
Hi, everyone,
Is the following code

<CODE>
void foo(int n)
{
int a[n];
.....
}
</CODE>

standard compliant and portable?

I use intel's, M$'s, borland's Digital Mars's and GUN(c89)'s compiler
compiled it, the first three report error but the last two were passed
silently. I want to know whether that code is portable.

Ths in advance.
£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£*£* £*£*
Best regards,

This is defined in the C99 standard, and it is portable as long as you
use a C99 compiler.
To use it with gcc use the std=C99 option, if my memory doesn't betray
me.

jacob

Nov 14 '05 #2
"bingfeng" <zh***********@topsec.com.cn> wrote:
void foo(int n)
{
int a[n];
.....
}

standard compliant and portable?

I use intel's, M$'s, borland's Digital Mars's and GUN(c89)'s compiler
compiled it, the first three report error but the last two were passed
silently.


It's correct C99, not C89. It's a Ganuck extension to C89, IIRC; don't
know about Digital Mars. If you want to stay with C89, then no, it's not
portable.

Richard
Nov 14 '05 #3
bingfeng wrote:

Is the following code

<CODE>
void foo(int n)
{
int a[n];
.....
}
</CODE>

standard compliant and portable?

I use intel's, M$'s, borland's Digital Mars's and GUN(c89)'s
compiler compiled it, the first three report error but the last
two were passed silently. I want to know whether that code is
portable.


At least in the GNU case you misused the compiler. It needs -ansi
-pedantic, and usefully also -W -Wall, to properly compile C.
Otherwise it compiles a mishmash of extensions.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #4
CBFalconer <cb********@yahoo.com> writes:
[...]
At least in the GNU case you misused the compiler. It needs -ansi
-pedantic, and usefully also -W -Wall, to properly compile C.
Otherwise it compiles a mishmash of extensions.


Or replace "-ansi" with "-std=c99" to compile a substantial subset of
C99.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
bingfeng wrote:
Hi, everyone,
Is the following code

<CODE>
void foo(int n)
{
int a[n];
.....
}
</CODE>

standard compliant and portable?
No. '.....' is not a legal statement of any sort.
I use intel's, M$'s, borland's Digital Mars's and GUN(c89)'s compiler
compiled it,


I don't believe you.
Nov 14 '05 #6
In article <ln************@nuthaus.mib.org>, ks***@mib.org says...
CBFalconer <cb********@yahoo.com> writes:
[...]
At least in the GNU case you misused the compiler. It needs -ansi
-pedantic, and usefully also -W -Wall, to properly compile C.
Otherwise it compiles a mishmash of extensions.


Or replace "-ansi" with "-std=c99" to compile a substantial subset of
C99.


Why bother? If you want portable C, you don't write anything containing
C99 extensions. Maybe that will change someday. In the meantime,
there isn't a lot you can't do with good old C89/90.
--
Randy Howard (2reply remove FOOBAR)
"I don't really care about being right you know,
I just care about success." --Steve Jobs
Nov 14 '05 #7
Randy Howard <ra*********@FOOverizonBAR.net> writes:
In article <ln************@nuthaus.mib.org>, ks***@mib.org says...
CBFalconer <cb********@yahoo.com> writes:
[...]
> At least in the GNU case you misused the compiler. It needs -ansi
> -pedantic, and usefully also -W -Wall, to properly compile C.
> Otherwise it compiles a mishmash of extensions.


Or replace "-ansi" with "-std=c99" to compile a substantial subset of
C99.


Why bother? If you want portable C, you don't write anything containing
C99 extensions. Maybe that will change someday. In the meantime,
there isn't a lot you can't do with good old C89/90.


Since your claim involves some implicited definition of "a lot", I won't
claim to refute you. But, whatever the drawbacks, there are some very nifty
thing you /can/ do with C99 extensions, that you can't in C89.

In particular, you can do things like:

---------------------------------------------
#define WITH_MUTEX_LOCK(l) \
for (int WITH_MUTEX_LOCK_ret = (0 == pthread_mutex_lock(l)); \
WITH_MUTEX_LOCK_ret; \
WITH_MUTEX_LOCK_ret = 0, (void) pthread_mutex_unlock(l)) \

/* code ... */

void
foo (void) {
WITH_MUTEX_LOCK (lock) {
/* locked code */
}
}
---------------------------------------------

Or:

---------------------------------------------
#define WITH_OPEN_FILE(svar, file, mode) /* body */ \
for (void * svar = (void *) fopen(file, mode), * once_only = (void *) 1; \
once_only; \
fclose((FILE *) svar), once_only = (void *) 0)

void
foo (void) {
WITH_OPEN_FILE (stream, "/dev/random", "r") {
/* declarations */
fread(longdata, sizeof(long), 15, stream);
}
}
----------------------------------------------

These sorts of macros make (IMO) source code much more readable, mistakes
less likely (e.g. "Too many open files" if you're fopen()ing in a loop),
and mistake fixes more localized.

Code following this idiom can be multiplied ... but only because of the
ability to declare new variable within the initialize step of the for loop:
a C99 feature.

-Denis
Nov 14 '05 #8

Le 13/06/2005 22:43, dans m2************@taggart.local, «*Denis Bueno*»
<De*********@gtri.gatech.edu> a écrit*:
These sorts of macros make (IMO) source code much more readable, mistakes
less likely (e.g. "Too many open files" if you're fopen()ing in a loop),
and mistake fixes more localized.

Code following this idiom can be multiplied ... but only because of the
ability to declare new variable within the initialize step of the for loop:
a C99 feature.

-Denis


What about this ?

#define WITH_MUTEX_LOCK(l) \
int WITH_MUTEX_LOCK_ret; \
for (WITH_MUTEX_LOCK_ret = (0 == pthread_mutex_lock(l)); \
WITH_MUTEX_LOCK_ret; \
WITH_MUTEX_LOCK_ret = 0, (void) pthread_mutex_unlock(l))

void foo(void) {
{
WITH_MUTEX_LOCK(lock) {
/* locked code */
}
}
}

It seems only slighlty less readable...

Nov 14 '05 #9
Jean-Claude Arbaut <je****************@laposte.net> writes:
#define WITH_MUTEX_LOCK(l) \
int WITH_MUTEX_LOCK_ret; \
for (WITH_MUTEX_LOCK_ret = (0 == pthread_mutex_lock(l)); \
WITH_MUTEX_LOCK_ret; \
WITH_MUTEX_LOCK_ret = 0, (void) pthread_mutex_unlock(l))

void foo(void) {
{
WITH_MUTEX_LOCK(lock) {
/* locked code */
}
}
}

It seems only slighlty less readable...


Consider, with your definition of WITH_MUTEX_LOCK:

WITH_MUTEX_LOCK (mutex) {
// stuff
}
// stuff
WITH_MUTEX_LOCK (mutex) {
// stuff
}

That code, if I'm not mistaken, will give you a compile error. You need an
independent scope for the macro expansions.

Also, if for any reason you (accidentally) refer to WITH_MUTEX_LOCK_ret
(typo, or whatever) outside of the WITH_MUTEX_LOCK (l) { ... } block, you
will capture whatever garbage is in the variable declared by the
macro. This behavior /completely/ violates the semantics of the block
structure.

Although with my version the code inside the block can capture the
WITH_MUTEX_LOCK_ret variable (which is because there is no way to generate
unique variable names automatically in C), it doesn't violate the semantics
of the block structure, and it doesn't cause the compiler to barf if the
macro is used twice in the same scope.

-Denis
Remove dvorak-noise from From.
Nov 14 '05 #10

Le 17/06/2005 03:29, dans m2************@taggart.local, «*Denis Bueno*»
<db********@UEOAgmail.com> a écrit*:
Jean-Claude Arbaut <je****************@laposte.net> writes:
#define WITH_MUTEX_LOCK(l) \
int WITH_MUTEX_LOCK_ret; \
for (WITH_MUTEX_LOCK_ret = (0 == pthread_mutex_lock(l)); \
WITH_MUTEX_LOCK_ret; \
WITH_MUTEX_LOCK_ret = 0, (void) pthread_mutex_unlock(l))

void foo(void) {
{
WITH_MUTEX_LOCK(lock) {
/* locked code */
}
}
}

It seems only slighlty less readable...
Consider, with your definition of WITH_MUTEX_LOCK:

WITH_MUTEX_LOCK (mutex) {
// stuff
}
// stuff
WITH_MUTEX_LOCK (mutex) {
// stuff
}

That code, if I'm not mistaken, will give you a compile error. You need an
independent scope for the macro expansions.


That's why I put curly braces around !
Also, if for any reason you (accidentally) refer to WITH_MUTEX_LOCK_ret
(typo, or whatever) outside of the WITH_MUTEX_LOCK (l) { ... } block, you
will capture whatever garbage is in the variable declared by the
macro.
It simply should never happen. It suffices to remember the outer {...} block
can only contain one macro expansion and nothing else. I use it merely to
be able to declare a local variable with a name common to all macros.
This behavior /completely/ violates the semantics of the block
structure.
I'm not sure, but I'm not sure you are wrong either :-)
Although with my version the code inside the block can capture the
WITH_MUTEX_LOCK_ret variable (which is because there is no way to generate
unique variable names automatically in C), it doesn't violate the semantics
of the block structure, and it doesn't cause the compiler to barf if the
macro is used twice in the same scope.


I was just trying to show you C99 is not absolutely needed for that trick.
Nov 14 '05 #11
I was just trying to show you C99 is not absolutely needed for that trick.


Anyway, even if mine works, your solution is more elegant.

Nov 14 '05 #12
Me
<snip nifty tricks about C99>
#define WITH_OPEN_FILE(svar, file, mode) /* body */ \
for (void * svar = (void *) fopen(file, mode), * once_only = (void *) 1; \
once_only; \
fclose((FILE *) svar), once_only = (void *) 0)

void
foo (void) {
WITH_OPEN_FILE (stream, "/dev/random", "r") {
/* declarations */
fread(longdata, sizeof(long), 15, stream);
}
}

<snip>

Change once_only's initializer to &svar (and get rid of all the (void*)
casting), otherwise it's not guaranteed what (void*)1 will return when
being evaluated for a truth value. Infact, I'd probably rewrite it as
the following since the above doesn't handle fopen failing:

#define WITH_OPEN_FILE(svar, file, mode) \
for (FILE *svar = fopen(file, mode); svar; fclose(svar), svar = 0)

In C++, this would be much better because it allows initializers in
conditionals. That feature combined with destructors lets you could do:

if (file_t file = create_file(name, mode)) {
// read file
} else {
// handle failure
}

And not let the break/return statement accidentally leak a filehandle.
But, I'm already off-topic for this newsgroup.

Nov 14 '05 #13

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

Similar topics

5
by: Matt Smith | last post by:
Hi, all. Just a quick question, when setting a COM process to read a value from a pre-defined register index, I think, I have to change the variable that the value will be returned to (as I have...
0
by: Matt Smith | last post by:
Hi, all. Just a quick question, when setting a COM process to read a value from a pre-defined register index, I think, I have to change the variable that the value will be returned to (as I have...
3
by: Annie | last post by:
I am trying to call a COM object which expects a variant datatype to be passed in to the COM API object. However, I get "<COMObject <unknown>>" and the program fails. Below is my code in Python,...
8
by: MLH | last post by:
A97 HELP shows the proper syntax for using Nz as Nz(variant) I'm wondering what to expect from potential past misuse I've made. For example, consider the following... Private Sub...
5
by: Torben Laursen | last post by:
Hi Can anyone show me how to convert between VARIANT and std::string and back, thanks! Torben ---
19
by: Jon Davis | last post by:
Hi guys! Just wanted to let you all know that I created a Variant structure, inspired by the old VB6 days. This is written in C#, but you can build a CLR/.NET class library assembly and reference...
0
by: .::alex::. | last post by:
Hello, Maybe you already guess the problem. I have a COM with a method in a Interface like that: HRESULT GetArray(VARIANT *pVal); so this method will return a VARIANT. This VARIANT contains...
1
by: morten | last post by:
When i compile this code in VC71 or VC80 i get the following errors: The code is copy/paste from the boost.org tutorial. Please help! error C2039: 'begin' : is not a member of...
1
by: captainc | last post by:
I have C++ code to import a .tlb and use a .dll that has functions that return VARIANT types and accepts BSTRs (bstrings). I have seen that python has modules that can manipulate VARIANTs and BSTRs...
4
by: jainchar | last post by:
hello I am creating a VARIANT that stores a integer value of variable but variable is not initialize.In my code the variable are "r" and"c" where r and c are the value of row and column of a table.I...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
1
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...
0
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.