Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is this valid?

Reply
Thread Tools

Is this valid?

 
 
AC
Guest
Posts: n/a
 
      06-10-2005
Is the following code valid?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
int a;
char s[];
} test_struct;

int main(void)
{
test_struct *t;
t = malloc( sizeof *t + 4);
strcpy(t->s,"oye");
printf("%s\n",t->s);
return 0;
}


 
Reply With Quote
 
 
 
 
clayne@anodized.com
Guest
Posts: n/a
 
      06-10-2005
For C90: no.

 
Reply With Quote
 
 
 
 
Robert Gamble
Guest
Posts: n/a
 
      06-10-2005
On Fri, 10 Jun 2005 12:34:08 -0400, AC wrote:

> Is the following code valid?
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> typedef struct
> {
> int a;
> char s[];
> } test_struct;
>
> int main(void)
> {
> test_struct *t;
> t = malloc( sizeof *t + 4);
> strcpy(t->s,"oye");
> printf("%s\n",t->s);
> return 0;
> }


I don't see any problems with it. The flexible array feature is not
standardized until C99 although many implementations supported it in some
form prior to that (but that would, of course, not be portable).

Robert Gamble
 
Reply With Quote
 
Anonymous 7843
Guest
Posts: n/a
 
      06-10-2005
In article <d8cfe0$h0p$(E-Mail Removed)>, AC <(E-Mail Removed)> wrote:
>Is the following code valid?
>
>typedef struct
>{
> int a;
> char s[];
>} test_struct;


If you change the declaration of s to char s[1] the
trick will work more often, i.e. nearly always.

It is technically outside the bounds of c89 though.

Search for "struct hack" using your favorite search engine
if you would like more information.
--
7842++
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      06-10-2005
AC wrote:
> Is the following code valid?


It is not valid C89 (C90); it is valid C99.



>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> typedef struct
> {
> int a;
> char s[];
> } test_struct;
>
> int main(void)
> {
> test_struct *t;
> t = malloc( sizeof *t + 4);
> strcpy(t->s,"oye");
> printf("%s\n",t->s);
> return 0;
> }
>
>

 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      06-10-2005
AC <(E-Mail Removed)> wrote:

> Is the following code valid?


> (snip "struct hack" code)


FAQ 2.6 may be of interest:

http://www.eskimo.com/~scs/C-faq/q2.6.html

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      06-10-2005

"Martin Ambuhl" <(E-Mail Removed)> wrote in message
newsUkqe.2289$(E-Mail Removed) ink.net...
> AC wrote:
>> Is the following code valid?

>
> It is not valid C89 (C90); it is valid C99.

*May* be valid C99... depends on return from malloc() which is not checked.

Quick question:
cfaq question 7.24 (relating to whether allocated memory must be freed prior
to exit)
references a section 7.10.3.2 no longer existant (AFAICT) in the current
standard...
So what's the official take? Answer remains the same but unsubstantiated?

Mark


 
Reply With Quote
 
Chris Croughton
Guest
Posts: n/a
 
      06-10-2005
On Fri, 10 Jun 2005 19:20:00 GMT, Mark
<(E-Mail Removed)> wrote:

> Quick question:
> cfaq question 7.24 (relating to whether allocated memory must be freed
> prior to exit) references a section 7.10.3.2 no longer existant
> (AFAICT) in the current standard... So what's the official take?
> Answer remains the same but unsubstantiated?


Using a reference to a nonexistent object results in undefined behaviour
<g>.

Chris C
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      06-10-2005
"Mark" <(E-Mail Removed)> writes:
[...]
> Quick question:
> cfaq question 7.24 (relating to whether allocated memory must be
> freed prior to exit) references a section 7.10.3.2 no longer
> existant (AFAICT) in the current standard... So what's the official
> take? Answer remains the same but unsubstantiated?


In C90, 7.10.3.2 describes the "free" function. The corresponding
section in C99 is 7.20.3.2.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Mark
Guest
Posts: n/a
 
      06-10-2005

"Keith Thompson" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Mark" <(E-Mail Removed)> writes:
> [...]
>> Quick question:
>> cfaq question 7.24 (relating to whether allocated memory must be
>> freed prior to exit) references a section 7.10.3.2 no longer
>> existant (AFAICT) in the current standard... So what's the official
>> take? Answer remains the same but unsubstantiated?

>
> In C90, 7.10.3.2 describes the "free" function. The corresponding
> section in C99 is 7.20.3.2.


Ah... they must have modified the description substantially then.
I see nothing in the standard which would suggest free() is optional
and it seems the previously quoted text 'quality of implementation issue'
was removed from C99.

Mark


 
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




Advertisments