469,953 Members | 2,284 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,953 developers. It's quick & easy.

What are anonymous objects?

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?

Nov 14 '05 #1
11 3018

G Fernandes wrote:
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.

Nov 14 '05 #2
On 9 Feb 2005 21:26:06 -0800, "G Fernandes" <ge**********@gmail.com>
wrote in comp.lang.c:

G Fernandes wrote:
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.


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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #3
On 9 Feb 2005 21:20:42 -0800, "G Fernandes" <ge**********@gmail.com>
wrote in comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4

Jack Klein wrote:
On 9 Feb 2005 21:26:06 -0800, "G Fernandes" <ge**********@gmail.com>
wrote in comp.lang.c:

G Fernandes wrote:
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.


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.

Nov 14 '05 #5
G Fernandes wrote:
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.
So what exactly are they?

Nov 14 '05 #6

On Thu, 9 Feb 2005, G Fernandes wrote:

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
Nov 14 '05 #7
E. Robert Tisdale wrote:
G Fernandes wrote:
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);


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.
So what exactly are they?


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!!!!!!!!!!!!!!!!!

Nov 14 '05 #8
In article <0m********************************@4ax.com>
Jack Klein <ja*******@spamcop.net> wrote:
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).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #9
Luke Wu wrote:
E. Robert Tisdale wrote:
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?


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

--
If geiger counter does not click,
the coffee, she is just not thick
Nov 14 '05 #10
Sebastian Hungerecker <se****@web.de> wrote:
Luke Wu wrote:
E. Robert Tisdale wrote:
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?


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
Nov 14 '05 #11
On 10 Feb 2005 06:18:32 GMT, Chris Torek <no****@torek.net> wrote in
comp.lang.c:
In article <0m********************************@4ax.com>
Jack Klein <ja*******@spamcop.net> wrote:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

699 posts views Thread by mike420 | last post: by
7 posts views Thread by Michele Simionato | last post: by
26 posts views Thread by Steven Bethard | last post: by
6 posts views Thread by Mark Brandyberry | last post: by
6 posts views Thread by Gaijinco | last post: by
6 posts views Thread by Bhuwan Bhaskar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.