c99/c89

S

shaanxxx

what is the problem with following programme.

int * myMalloc(int Size)
{
int a[Size];
return a;
}

can we use sizeof operator to find sizeof array 'a';

Thanks
shaan.
 
C

CBFalconer

shaanxxx said:
what is the problem with following programme.

int * myMalloc(int Size)
{
int a[Size];
return a;
}

can we use sizeof operator to find sizeof array 'a';

You are returning something that no longer exists.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

shaanxxx said:
what is the problem with following programme.

int * myMalloc(int Size)
{
int a[Size];
return a;
}

can we use sizeof operator to find sizeof array 'a';
In c99, yes.

You are however returning a pointer to something that
doesn't exist when the function finishes. Don't do that.
 
C

Chris Dollin

CBFalconer said:
shaanxxx said:
what is the problem with following programme.

int * myMalloc(int Size)
{
int a[Size];
return a;
}

can we use sizeof operator to find sizeof array 'a';

You are returning something that no longer exists.

Nit: they're returning a /pointer/ to something that no longer exists.

Note: this assumes C99 (or a false friend with variable-length
arrays).
 
J

jaysome

what is the problem with following programme.

int * myMalloc(int Size)
{
int a[Size];
return a;
}

can we use sizeof operator to find sizeof array 'a';

No, if I understand your question. Even if you avoid the undefined
behavior by doing something like this:

int * myMalloc(int Size)
{
static int a[Size];
return a;
}

there is no standard way to find the "sizeof" 'a' usng only the return
value from the function myMalloc.

Best regards
 
S

shaanxxx

shaanxxx said:
what is the problem with following programme.

int * myMalloc(int Size)
{
int a[Size];
return a;
}

can we use sizeof operator to find sizeof array 'a';

Thanks
shaan.

In C++ and pre-C99 versions of C, an array's size must be a constant
integral expression so that it can be calculated at compile-time. In
C99, this restriction was relaxed. An array's size must be an integral
expression, not necessarily a constant one. This allows you to declare
a variable-length array or an array whose size is determined at
runtime. For example:
void func(int sz)
{
int arr[sz]; // sz ins't const; allowed only in C99
}

The size of arr may be different for every func() call. For example:
int main()
{
int size;
printf("enter array's size : ");
scanf("%d", &size);
func(size);
}

To support variable-length arrays, C99 adds the notation [*] in a
function's parameter list to indicate a variable-length array. In
addition, C99 extends the functionality of sizeof. When applied to a
variable-length array, it calculates the its size at runtime. In my
opinion, this is the most daring change in C99. Not only does it alter
the semantics of a built-in operator from compile-time to runtime
operation (only when using variable-length arrays), it's also a "quiet
change" -- there's no explicit syntactic clue to distinguish between
the two forms.

ref:
http://www.informit.com/guides/content.asp?g=cplusplus&seqNum=215&rl=1
 
F

Frederick Gotham

shaanxxx posted:
what is the problem with following programme.

int * myMalloc(int Size)
{
int a[Size];
return a;
}

can we use sizeof operator to find sizeof array 'a';


Get a C book and learn about the scope of variables.
 
S

Simon Biber

jaysome said:
what is the problem with following programme.

int * myMalloc(int Size)
{
int a[Size];
return a;
}

can we use sizeof operator to find sizeof array 'a';

No, if I understand your question. Even if you avoid the undefined
behavior by doing something like this:

int * myMalloc(int Size)
{
static int a[Size];
return a;
}

That's not allowed either. Variable length arrays may not have static
duration.

C99 6.7.5.2 Array declarators, Example 4 says
"Array objects declared with the static or extern storage-class
specifier cannot have a variable length array (VLA) type."

However, examples are not normative. Is there normative text that
expresses this requirement? Is it a constraint violation?
 
S

shaanxxx

Frederick said:
shaanxxx posted:
what is the problem with following programme.

int * myMalloc(int Size)
{
int a[Size];
return a;
}

can we use sizeof operator to find sizeof array 'a';


Get a C book and learn about the scope of variables.

Thanks , I put this question for discussion. Definately i will do what
you suggested me.
 
B

Bill Pursell

Chris said:
CBFalconer said:
shaanxxx said:
what is the problem with following programme.

int * myMalloc(int Size)
{
int a[Size];
return a;
}

can we use sizeof operator to find sizeof array 'a';

You are returning something that no longer exists.

Nit: they're returning a /pointer/ to something that no longer exists.

Note: this assumes C99 (or a false friend with variable-length
arrays).

Double nit:
the function is returning a pointer to something that going to cease to
exist before the caller will receive the value.
 
R

Robert Gamble

Simon said:
jaysome said:
what is the problem with following programme.

int * myMalloc(int Size)
{
int a[Size];
return a;
}

can we use sizeof operator to find sizeof array 'a';

No, if I understand your question. Even if you avoid the undefined
behavior by doing something like this:

int * myMalloc(int Size)
{
static int a[Size];
return a;
}

That's not allowed either. Variable length arrays may not have static
duration.

C99 6.7.5.2 Array declarators, Example 4 says
"Array objects declared with the static or extern storage-class
specifier cannot have a variable length array (VLA) type."

However, examples are not normative. Is there normative text that
expresses this requirement? Is it a constraint violation?

Yes, it is a constraint violation:
6.7.5.2p2:
"If an identifier is declared to be an object with static storage
duration, it shall not have a variable length array type."

Robert Gamble
 
K

Keith Thompson

Frederick Gotham said:
shaanxxx posted:
what is the problem with following programme.

int * myMalloc(int Size)
{
int a[Size];
return a;
}

can we use sizeof operator to find sizeof array 'a';

Get a C book and learn about the scope of variables.

Scope has little to do with it. The issue here is the *lifetime* of
the variable, not its scope.
 
K

Keith Thompson

shaanxxx said:
Get a C book and learn about the scope of variables. :)

Please don't snip attribution lines, and provide enough context so
your followup makes sense on its own. I happen to know what you're
talking about because I've read the previous articles in the thread;
if I hadn't, your followup wouldn't have made any sense at all.
 
F

Frederick Gotham

Keith Thompson posted:
Scope has little to do with it. The issue here is the *lifetime* of
the variable, not its scope.


They are equivalent, are they not? As per my current understanding, an
object's lifetime ends when it goes out of scope.
 
P

P.J. Plauger

Keith Thompson posted:



They are equivalent, are they not? As per my current understanding, an
object's lifetime ends when it goes out of scope.

int f()
{
int a; /* lifetime ends at end of block */
static int b; /* lifetime ends at program termination */
extern int c; /* lifetime ends at program termination */
}

Scope is about visibility, not lifetime.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
 
G

Guest

Frederick said:
Keith Thompson posted:


They are equivalent, are they not? As per my current understanding, an
object's lifetime ends when it goes out of scope.

In the general case, this is not true. A really simple example:

#include <stdio.h>

char *f(void) {
static char hello[] = "Hello, world!";
return hello;
}

int main(void) {
puts(f()); /* valid, hello's lifetime has not ended */
puts(hello); /* invalid, no object "hello" in scope */
}
 
R

Robert Gamble

Frederick said:
Keith Thompson posted:



They are equivalent, are they not?

No, they are related but not equivalent.
As per my current understanding, an
object's lifetime ends when it goes out of scope.

Scope defines where an *identifier* is visible to the program.
Lifetime refers to the part of the program where an *object* is
guaranteed to exist. They are not always the same, for example, a
static object declared inside a function has a lifetime of the entire
program execution and can be accessed outside the function via
pointers. The identifier for the object defined in the function has a
scope limited to the function.

Robert Gamble
 
K

Keith Thompson

Frederick Gotham said:
Keith Thompson posted:

They are equivalent, are they not? As per my current understanding, an
object's lifetime ends when it goes out of scope.

No, they're not equivalent. Think about static objects.

Get a copy of the standard (the latest post-C99 draft is n1124.pdf)
and read sections 6.2.1 (Scopes of identifiers) and 6.2.4 (Storage
durations of objects).
 
P

pete

Frederick said:
Keith Thompson posted:


They are equivalent, are they not? As per my current understanding, an
object's lifetime ends when it goes out of scope.

Scope is related to linkage.
Lifetime has nothing to do with linkage.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top