473,320 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Is this valid?

AC
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;
}
Nov 14 '05 #1
11 2057
For C90: no.

Nov 14 '05 #2
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
Nov 14 '05 #3
In article <d8**********@license1.unx.sas.com>, AC <te**@test.test> 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++
Nov 14 '05 #4
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;
}

Nov 14 '05 #5
AC <te**@test.test> 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.
Nov 14 '05 #6

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:oU*****************@newsread1.news.atl.earthl 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
Nov 14 '05 #7
On Fri, 10 Jun 2005 19:20:00 GMT, Mark
<so***@localbar.com> 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
Nov 14 '05 #8
"Mark" <so***@localbar.com> 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) 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 #9

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Mark" <so***@localbar.com> 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
Nov 14 '05 #10
"Mark" <so***@localbar.com> writes:
"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"Mark" <so***@localbar.com> 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.


No, there was no significant change in the wording from C90 to C99.
(I don't think there was any change, but I'm too lazy to compare it.)

The phrase "quality of implementation issue" in the FAQ is not a quote
from the standard. Basically, once the program terminates the
standard doesn't care what happens next.

--
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 #11
On Fri, 10 Jun 2005 19:20:00 +0000, Mark wrote:

"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:oU*****************@newsread1.news.atl.earthl 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?


The C language has never required you to free memory before the program
terminates - strictly conforming programs do not need to do this.

The standard is concerned about the behaviour of a program while it is
executing up to the point it generates a termination status. What happens
after it terminates is of no concern to the standard. In particular it has
nothing to say about the reclamation of any of the resources that the
program used. That includes memory allocated for code, automatic objects,
static objects, allocated objects that have been freed, allocated objects
that have not been freed, and anything else that the implementation uses
to support the execution of the program.

Whether these things are reclaimed or not is therefore an implementation
issue. You may take a view that freed objects are more likely to be
reclaimed than unfreed ones, but there's nothing in the standard to
support that view, and very little evidence from real-world
implementations either. The fact is that a implementation that doesn't
reclaim fully will have resource leak and therefore stability issues
that are rarely if ever acceptable.

Perhaps a more solid reason for freeing memory before the program
terminates is to help debugging tools such as memory leak detectors.

Lawrence

Nov 14 '05 #12

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

Similar topics

12
by: lawrence | last post by:
I have a string which I want to send to eval(). How can I test it ahead of time to make sure it is valid code? I don't want to send it to eval and get parse errors. I want to do something like...
16
by: siliconmike | last post by:
Hi, I'm looking for a reliable script that would connect to a host and somehow determine whether an email address is valid. since getmxrr() only gets the mx records.. Links/pointers ? Mike
1
by: Anna | last post by:
Hi all. I have probably a rather stupid question. If there is an HTML document, XML-formed using JTidy, is there any tool to convert it to valid XHTML? I.e. so that all the tags and attribute...
7
by: JR | last post by:
Hey all, I have read part seven of the FAQ and searched for an answer but can not seem to find one. I am trying to do the all too common verify the data type with CIN. The code from the FAQ...
23
by: James Aguilar | last post by:
Someone showed me something today that I didn't understand. This doesn't seem like it should be valid C++. Specifically, I don't understand how the commas are accepted after the function...
3
by: Chris | last post by:
Hi, In C# I tried to save a file from a generated file name. Just before launching the dialog I check for a valid file name to be sure. There for I used the method ValidateNames from the save...
0
by: QA | last post by:
I am using a Business Scorecard Accelarator in a Sharepoint Portal 2003 using SQL Server 2005 I am getting the following error: Error,5/7/2005 10:50:14 AM,580,AUE1\Administrator,"Specified cast is...
1
by: Robert Morgan | last post by:
|I'm trying to run a query on a database using php and postgres functions ||<?php db_connect(); $stat = pg_exec($connstr,"SELECT WSID from tblWorkstation "); while ($row = pg_fetch_rows($stat))...
1
by: illegal.prime | last post by:
Hey all, I have an app, that could take two numbers of any type of numerical type int, long, double, float, uint, ulong, etc. I want to check that the numbers are part of a range that I consider...
10
by: SpreadTooThin | last post by:
Hi I'm writing a python script that creates directories from user input. Sometimes the user inputs characters that aren't valid characters for a file or directory name. Here are the characters...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.