What are anonymous objects?

G

G Fernandes

I've heard some mention "anonymous" objects in c.l.c and other places
(conversations), and I was wondering what exactly these are. Anonymous
seems to imply that there is no name, but no name implies that one
can't access the object, which implies that the object would be
useless.

So what exactly are they?
 
G

G Fernandes

G said:
I've heard some mention "anonymous" objects in c.l.c and other places
(conversations), and I was wondering what exactly these are. Anonymous
seems to imply that there is no name, but no name implies that one
can't access the object, which implies that the object would be
useless.

So what exactly are they?

Just wanted to add that I'm not confused about objects associated with
classes, I'm talking about objects like variables.
 
E

E. Robert Tisdale

G said:
I've heard some mention "anonymous" objects in c.l.c and other places
(conversations), and I was wondering what exactly these are.
Anonymous seems to imply that there is no name,
but no name implies that one can't access the object,
which implies that the object would be useless.

No.
There could be a pointer to it.

void* p = malloc(10);

The object to which p points doesn't have a name.

struct {
int I;
} x;

The type of the object named x doesn't have a name.
 
J

Jack Klein

Just wanted to add that I'm not confused about objects associated with
classes, I'm talking about objects like variables.

That's good, because there are no classes in C.
 
J

Jack Klein

I've heard some mention "anonymous" objects in c.l.c and other places
(conversations), and I was wondering what exactly these are. Anonymous
seems to imply that there is no name, but no name implies that one
can't access the object, which implies that the object would be
useless.

So what exactly are they?

There are two things that can be considered anonymous objects in C.

The first are string literals, both normal and wide.

In the canonical C program:

#include <stdio.h>
int main(void)
{
printf("Hello, World!\n");
return 0;
}

The string literal "Hello, World\n", is most certainly an object, in
fact it is an array of 14 characters, including the '\0' at the end,
but it has no name.

Likewise:

char *cp = "A string literal";

This string literal has no name either, yet can be accessed from many
different places in a program, both where the declaration is in scope
and any place where cp might be passed as an argument. The pointer
has a name, the object it points to does not.

The other case of what might or might not be called anonymous objects
in C are objects "imposed" on dynamically allocated memory. This is
not really the same thing, but since the term is not defined by the C
standard, you could stretch the point.

Consider:

struct anon { int x; double y; long z; };

Then later in code:

struct anon *ap = malloc(sizeof *ap);

Assuming the malloc() succeeds, once you actually assign a value to
any or all of ap->x, ap->y, or ap->z, that block of memory takes on
the effective type of a struct anon, although technically it is not
actually a struct anon object.
 
G

G Fernandes

Jack said:
That's good, because there are no classes in C.


Yes I know. Just wanted to prevent possible "this is not C++
newsgroup" response, because I really was asking a C question.
 
A

Arthur J. O'Dwyer

I've heard some mention "anonymous" objects in c.l.c and other places
(conversations), and I was wondering what exactly these are. Anonymous
seems to imply that there is no name, but no name implies that one
can't access the object, which implies that the object would be
useless.

Not true. "Anonymous" means "nameless." But just because an object
has no name doesn't mean you can't access it. Consider:

int *foo(void)
{
int *p = malloc(sizeof *p);
return p;
}

'p' is a named object (namely, a pointer-to-int named 'p'). 'p' points
to an anonymous object; in other words, '*p' is an anonymous object.
Sure, you can refer to it as '*p', but that's not a "name" in this
sense. In this context, a "name" is just an identifier.
Repeat: '*p' is an anonymous object. So is the lvalue pointed to
by the return value of 'malloc(sizeof *p)'. So is '*foo()'.
It really is that simple! :)

-Arthur,
waiting for someone to contend that
it's not that simple after all
 
C

Chris Torek

There are two things that can be considered anonymous objects in C.

The first are string literals, both normal and wide. [snippage]
The other case of what might or might not be called anonymous objects
in C are objects "imposed" on dynamically allocated memory. This is
not really the same thing, but since the term is not defined by the C
standard, you could stretch the point.

There is a third in C99: the objects created by compound literals.
You can (and in some cases, have to) take the address of one:

void f(void) {
const int *p = (const int []){1, 2, 3};
...
}

Here p points to the first of three "int"s in an array object, in
pretty much the same way that "cp" points to an anonymous array
created by a string literal in Jack Klein's example (which I snipped).
 
L

Luke Wu

E. Robert Tisdale said:
No.
There could be a pointer to it.

void* p = malloc(10);

You could have used something other than void* so keep the focus on the
purpose and context of your statement, rather than introduce extra
"questions" in the minds of the OP or other curious readers.
The object to which p points doesn't have a name.

struct {
int I;
} x;

The type of the object named x doesn't have a name.

Are you seriously saying x doesn't have a name? So what is 'x'?

A name of an object is the primary lvalue that comes alone with the
definition/declaration.
You defined a structure 'x', and you got a freebie lvalue 'x' for your
efforts. It's not anonymous!!!!!!!!!!!!!!!!!
 
R

Richard Bos

Sebastian Hungerecker said:
No, he's saying that the type of x doesn't have a name.

The question was about anonymous objects. A type is not an object. x is
not an anonymous object; it is called x. Its type is anonymous, but that
is irrelevant to the question.

Richard
 
J

Jack Klein

There are two things that can be considered anonymous objects in C.

The first are string literals, both normal and wide. [snippage]
The other case of what might or might not be called anonymous objects
in C are objects "imposed" on dynamically allocated memory. This is
not really the same thing, but since the term is not defined by the C
standard, you could stretch the point.

There is a third in C99: the objects created by compound literals.
You can (and in some cases, have to) take the address of one:

void f(void) {
const int *p = (const int []){1, 2, 3};
...
}

Here p points to the first of three "int"s in an array object, in
pretty much the same way that "cp" points to an anonymous array
created by a string literal in Jack Klein's example (which I snipped).

Yes, you're absolutely right, I completely forgot them. Thanks.

Perhaps because one compiler that I do have that claims to support
that C99 feature crashed when I tried the feature. The compiler, that
is, not the executable, which was never produced. Not that I'm
complaining, it's a freeware compiler and does many things, including
many C99 features extremely well.
 

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

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top