Re: object creation and naming at runtime?
From: Gary Labowitz (glabowitz_at_comcast.net)
Date: 01/08/05
- Next message: Alwyn: "Re: Linked list tutorial (in C)?"
- Previous message: xm: "Using flags"
- In reply to: wil: "Re: object creation and naming at runtime?"
- Next in thread: B. v Ingen Schenau: "Re: object creation and naming at runtime?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 8 Jan 2005 17:35:05 -0500
"wil" <willy@spamless.nl> wrote in message
news:m6e0u057pc8qhdhar0uldv8gutb6k7b03m@4ax.com...
> On Sat, 08 Jan 2005 19:43:00 +0100, "B. v Ingen Schenau"
> <bart@ingen.ddns.info> wrote:
> >In this forum, a significant number of posters is actually asking
> >questions that relate to programming classes that they are following,
> >so don't be offended when someone makes that assumption.
>
> I'm on a short fuse this week, my apologies for being blunt myself.
>
> >Well, you can't do anything like that in C or C++.
> >In both languages, all the names that appear in the source code are
> >replaced with memory references by the compiler. In the final
> >executable, you will not find any names (unless the compiler included
> >debugging information, but that is never used by the executable
> >itself).
> >
> >Perhaps you can explain to us what problem you tried to solve with this
> >dynamic creation of names, so that we can point you in a direction that
> >does lead to a solution.
>
> Here goes,
>
> A text file with data, which at a certain point goes:
>
>
> [CARS]
> toyota-yaris
> mercedes-C
> audi-A4
> [toyata-yaris]
> doors 2
> headlights normal
> airbag 2
> [mercedes-C]
> doors 4
> headlights xeon
> ABS 1
> ESP 1
> airbag 4
> [audi-A4Q]
> doors 4
> headlights xeon
> ABS 1
> airbag 2
> 4wheeldrive 1
>
> First under [CARS] a list of models which is followed by more
> detailed info further down.
> Each car has a few properties which always come back...like 'doors'
> but there are also things that apply only to a certain car which will
> not there for other cars.
> So every line has to be checked against a list of keywords.
>
> I've got all that figured out and it works.
>
> But now i'm at the point where i want those properties written into
> an object of class CARS.
> quick and dirty:
> class CARS{
> int doors;
> vector<string> headlights;
> bool abs;
> and so on and so on.
> }
>
> Each car gets it's own object so later the info in each object can
> be shown on screen and/or edited...and/or written away in a new file
> if so desired.
> basically i have everything going, one of things it does is count the
> amount of cars under [CARS] so i know how many objects need to be
> made(once in runtime).
> It's the creating and naming objects dynamically which i can't figure
> out.
> In the above example it would be 3 objects...but it could aswel be 10
> or a 100..
You are focusing on "naming" the objects which you create dynamically. This
can't be done. The names used in a source program are a substitute for the
actual address that will be used in the computer when the program is run.
Think of it this way: I don't know where the computer will allocate space in
memory for my int (or whatever) so I'll use a name for the address and let
the compiler fill in the address when it knows where the int is. Usually
this is done by assigning an offset value from the start of the module and
adjust that to an actual address (well, a virtual address in memory) when
the program is loaded into memory and the starting address of the program is
known. So names of objects are a way of referring to an object when you
don't have the address yet --- namely, at compile time.
When a program is running, it is in memory and the addresses are known. Your
names, however, are long gone. So for a dynamically created object the
system does an interesting thing; it supplies the address of the object just
allocated by the program. The program stores this address away in an object
that is had non-dynamically allocated. This object is an area of the
allocated space of the program that can contain an address. This is what is
known as a pointer type. In the source code the pointer object (which will
contain an address once it is known) is at some known offset from the
beginning of the program. In source code you use a name for the pointer
object because it will always be at a specific place. A smart programmer
will save the address of a dynamic object in such a pointer so that a later
access can be made to the address stored in the pointer. This requires a
special syntax to tell the computer to take the contents of the (named)
object and use the value as the address of an (unnamed) object. A code
example is:
int* aDynamicObject;
aDynamicObject = new int;
*aDynamicObject = 17;
The pointer (named aDynamicObject) is declared as being a container for an
address of an int. That's the int* part of the first line.
The object is dynamically created by the syntax "new int" which allocates
the memory for whatever size value the particular compiler uses. The
guarantee is that it can hold at least a 15-bit value and a sign. (The
default is a signed int.) The new operation returns an address of the
allocated object (if there is memory for it; I have left out error checking
in this example).
The smart programmer stores the address returned by the new operation in the
pointer named aDynamicObject. That the second line.
Finally, any reference to the int that was dynamically created is done using
the syntax in line three. That's the special syntax that tells the compiler
to generate instructions that will "indirectly" reference a value, also
known as "dereferencing."
If you are going to have a lot of these addresses to store, however, it
would be insane to build a program with a multitude of named pointers.
That's where arrays come in. You set up an array of pointers to whatever
kind of object you will be building dynamically. The example would become
int* someDynamicObjects[100]; //or whatever size
someDynamicObjects[0] = new int; //probably repeated in a loop with the
index a variable
*someDynamicObjects[0] = 17;
Good luck.
-- Gary
- Next message: Alwyn: "Re: Linked list tutorial (in C)?"
- Previous message: xm: "Using flags"
- In reply to: wil: "Re: object creation and naming at runtime?"
- Next in thread: B. v Ingen Schenau: "Re: object creation and naming at runtime?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|