Okay to move an "object" will-nilly?

F

Frederick Gotham

(My dialect of English seems to puzzle people at times, so I'll first
clarify a few terms which I use in the following post:)

(1) By "domestic", I mean "ordinary, run-of-the-mill, not
extraordinary or strange".

(2) By "willy-nilly", I mean something along the lines of
"haphazardly", but without any sense of recklessness (i.e. gleefully
doing something without expecting any sort of negative effect, even
though there may in fact be a negative effect), e.g. "A child would drink
a glass of liquid left on the kitchen table willy-nilly, blissfully
ignorant as to whether it contained bleach".

======================================================

Okay I'll try to keep the C++ part brief (and yes, my eventual question
is about C):

In C++, there are things called objects. They behave just like
variables, but special things happen when they're created, destroyed,
copied, assigned to, etc.. (specifically, the programmer can specify code
which is to be executed).

If you want to copy an object in C++, is it ill-advised to simply do:

ArbitraryClass original_object;

ArbitraryClass *p = malloc( sizeof *p );

memcpy( p, &original_object, sizeof original_object );


The reason why it's il-advised is that when an object is created, code
can be executed via a "constructor" which may result in the acquisition
of resources such as dynamically allocated memory. If we perform a
"shallow copy" by using "memcpy", then the resulting copy is using the
same resources as the original (when in reality, we want it to get its
own). The matter is remedied by using a copy-constructor... but I won't
get into that here.

Anyway, notwithstanding any of that, I thought it would still be OK in
C++ to move an object in memory, i.e. simply re-locate it. This would
involve memcpy'ing its bytes to a different location and simply not
making any further use of the original. I was told however that this also
is il-advised in C++, because the object's address may be of some
significance to its internal workings (i.e. the special code that gets
executed when you assign to it, destroy it, etc.). A prime example would
be an object which contains a buffer, and which also contains a pointer
to the current position in the buffer, something like:

struct StringStream {

char buffer[1024];

char *p_pos; /* Points to an element of buffer */

};

If we were to move an object of the type "StringStream", then "p_pos"
would effectively become corrupt.

So what's the significance of this?

Recently, I wrote a funky kind of algorithm for sorting an array (whether
it be an array of char's, int's, double's or perhaps a C++ class type),
and the algorithm moved objects around willy-nilly by using memcpy and
discarding the original. However, my algorithm was "broken" in the sense
that it would corrupt objects whose address was of some significance to
their internal workings.

Hence, there is an accepted etiquette in C++ that, when writing re-usable
code such as a sorting algorithm, you DON'T just re-locate objects willy-
nilly, but rather you've to resort to more elustrious means.

My question is:
Does any such etiquette exist in C? If I were writing a domestic
sorting algorithm in C, would it be acceptable for me to re-locate the
objects willy-nilly?
 
T

Tom St Denis

Frederick Gotham wrote:
My question is:
Does any such etiquette exist in C? If I were writing a domestic
sorting algorithm in C, would it be acceptable for me to re-locate the
objects willy-nilly?

Provided nothing was relying on the previously established order of the
the array, yeah sure why not?

Though I should add that even in C++ if you had an array of class
instances you could sort them if you had overloaded the comparison and
assignment operators.

Tom
 
P

pete

In C++, there are things called objects.

In C
int int_object;
is a declaration of an object named int_object.
I believe that's also true in C++.
I was told however that this also
is il-advised in C++, because the object's address may be of some
significance to its internal workings (i.e. the special code that gets
executed when you assign to it, destroy it, etc.).

FILE type objects are like that in C.
My question is:
Does any such etiquette exist in C? If I were writing a domestic
sorting algorithm in C, would it be acceptable for me to re-locate the
objects willy-nilly?

When there's a problem with copying or overwriting objects,
such as with FILE type objects,
and the objects refered to by string literals,
you would be better off sorting an array of pointers to the objects.
 
R

Roberto Waltman

Frederick said:
...
struct StringStream {
char buffer[1024];
char *p_pos; /* Points to an element of buffer */
};

If we were to move an object of the type "StringStream", then "p_pos"
would effectively become corrupt.
...
My question is:
Does any such etiquette exist in C? If I were writing a domestic
sorting algorithm in C, would it be acceptable for me to re-locate the
objects willy-nilly?

Depends on the objects and their usage. In general, the answer is no.

Not only the objects may contain pointers that would be invalidated by
the relocation, (as in your own StringStream example,) their addresses
could also be stored in pointers somewhere else in the code and those
would be invalidated.

One more issue: How would you handle the relocation of objects
residing in dynamically allocated memory, either explicitly by calling
malloc() or because the objects were declared as automatic variables?
(The same for mixture of static and dynamic objects.)
 
M

Morris Dovey

Roberto Waltman (in (e-mail address removed)) said:

| Frederick Gotham wrote:
|| ...
|| struct StringStream {
|| char buffer[1024];
|| char *p_pos; /* Points to an element of buffer */
|| };
||
|| If we were to move an object of the type "StringStream", then
|| "p_pos" would effectively become corrupt.
|| ...
|| My question is:
|| Does any such etiquette exist in C? If I were writing a domestic
|| sorting algorithm in C, would it be acceptable for me to re-locate
|| the objects willy-nilly?
|
| Depends on the objects and their usage. In general, the answer is
| no.
|
| Not only the objects may contain pointers that would be invalidated
| by the relocation, (as in your own StringStream example,) their
| addresses could also be stored in pointers somewhere else in the
| code and those would be invalidated.
|
| One more issue: How would you handle the relocation of objects
| residing in dynamically allocated memory, either explicitly by
| calling malloc() or because the objects were declared as automatic
| variables? (The same for mixture of static and dynamic objects.)

OTOH, if you changed p_pos to be the size_t displacement into the
buffer of the (first) char of interest, then you may very well be able
to relocate StringStream(s) at will.
 
J

Jack Klein

(My dialect of English seems to puzzle people at times, so I'll first
clarify a few terms which I use in the following post:)

(1) By "domestic", I mean "ordinary, run-of-the-mill, not
extraordinary or strange".

(2) By "willy-nilly", I mean something along the lines of
"haphazardly", but without any sense of recklessness (i.e. gleefully
doing something without expecting any sort of negative effect, even
though there may in fact be a negative effect), e.g. "A child would drink
a glass of liquid left on the kitchen table willy-nilly, blissfully
ignorant as to whether it contained bleach".

======================================================

Okay I'll try to keep the C++ part brief (and yes, my eventual question
is about C):

In C++, there are things called objects. They behave just like
variables, but special things happen when they're created, destroyed,
copied, assigned to, etc.. (specifically, the programmer can specify code
which is to be executed).

If you want to copy an object in C++, is it ill-advised to simply do:

ArbitraryClass original_object;

ArbitraryClass *p = malloc( sizeof *p );

memcpy( p, &original_object, sizeof original_object );


The reason why it's il-advised is that when an object is created, code
can be executed via a "constructor" which may result in the acquisition
of resources such as dynamically allocated memory. If we perform a
"shallow copy" by using "memcpy", then the resulting copy is using the
same resources as the original (when in reality, we want it to get its
own). The matter is remedied by using a copy-constructor... but I won't
get into that here.

Anyway, notwithstanding any of that, I thought it would still be OK in
C++ to move an object in memory, i.e. simply re-locate it. This would
involve memcpy'ing its bytes to a different location and simply not
making any further use of the original. I was told however that this also
is il-advised in C++, because the object's address may be of some
significance to its internal workings (i.e. the special code that gets
executed when you assign to it, destroy it, etc.). A prime example would
be an object which contains a buffer, and which also contains a pointer
to the current position in the buffer, something like:

struct StringStream {

char buffer[1024];

char *p_pos; /* Points to an element of buffer */

};

If we were to move an object of the type "StringStream", then "p_pos"
would effectively become corrupt.

So what's the significance of this?

Recently, I wrote a funky kind of algorithm for sorting an array (whether
it be an array of char's, int's, double's or perhaps a C++ class type),
and the algorithm moved objects around willy-nilly by using memcpy and
discarding the original. However, my algorithm was "broken" in the sense
that it would corrupt objects whose address was of some significance to
their internal workings.

Hence, there is an accepted etiquette in C++ that, when writing re-usable
code such as a sorting algorithm, you DON'T just re-locate objects willy-
nilly, but rather you've to resort to more elustrious means.

My question is:
Does any such etiquette exist in C? If I were writing a domestic
sorting algorithm in C, would it be acceptable for me to re-locate the
objects willy-nilly?

The C qsort() function does exactly this. If you are sorting an array
of objects, the objects in the array must be move safe. If they are
not move safe, then you will have problems using them after you have
sorted them with qsort(), unless you have the degenerate case where
the array was already sorted so no swaps were performed by the sort.

In the case of your example StringStream structure, it would not be
safe to sort them by qsort(), nor would it be safe to sort them by any
other means that involved swapping.

As others have mentioned, you can define an array of pointers to the
structures, then sort this index array with another level of
indirection.

So C, just like C++, allows you to do some things that might not be
safe. You can use memcpy() or memmove() on C objects, just as you can
on POD objects in C++. It is up to the programmer to determine if the
internal structure of those objects makes the operation useful.
 
R

Richard Heathfield

Frederick Gotham said:
(My dialect of English seems to puzzle people at times, so I'll first
clarify a few terms which I use in the following post:)

(1) By "domestic", I mean "ordinary, run-of-the-mill, not
extraordinary or strange".
Canonical.

(2) By "willy-nilly",

That is a contraction of "will-he-nill-he" meaning "whether he wishes it or
whether he doesn't", i.e. "like it or not".
I mean something along the lines of
"haphazardly", but without any sense of recklessness

You appear to mean "arbitrarily".

<snip>


And now a brief observation on the actual question. :)
My question is:
Does any such etiquette exist in C? If I were writing a [canonical]
sorting algorithm in C, would it be acceptable for me to re-locate the
objects [arbitrarily]?

You haven't any choice, since that's what sorting /does/.

But if you have "deep" data, you'd be better off sorting pointers.
 
N

Nick Keighley

Frederick said:
(My dialect of English seems to puzzle people at times, so I'll first
clarify a few terms which I use in the following post:)

(1) By "domestic", I mean "ordinary, run-of-the-mill, not
extraordinary or strange".

(2) By "willy-nilly", I mean something along the lines of
"haphazardly", but without any sense of recklessness (i.e. gleefully
doing something without expecting any sort of negative effect, even
though there may in fact be a negative effect), e.g. "A child would drink
a glass of liquid left on the kitchen table willy-nilly, blissfully
ignorant as to whether it contained bleach".

just curious, but which dialect of english is this? I've never seen
"domestic"
used the way you use it.
 
C

Chris Dollin

Nick said:
just curious, but which dialect of english is this? I've never seen
"domestic"
used the way you use it.

I think he should have said "idiolect" rather than "dialect": I too
wouldn't interpret "domestic" that way (although I can see it being
used that way).
 
F

Frederick Gotham

Nick Keighley posted:
just curious, but which dialect of english is this? I've never seen
"domestic"
used the way you use it.


English as spoken in Dublin in Ireland. It's true that some of our
grammar and phrases are influenced by the Irish language, but I think our
use of "domestic" is pure English as I can't think off-hand of an
equivalent term in Irish.

We also use the term "domesticated" to indicate that an animal has been
tamed. For instance:

-John was attacked by dogs down by the lake.
-Were they wild dogs?
-No, domesticated.


All the forms of "domestic" indicate (to us in anyway) something which is
commonplace and not extraordinary.
 
A

Al Balmer

Nick Keighley posted:
<snip>

Since it's already consumed far too much time and space, I suggest
that you just use "ordinary", which I think everyone agrees on.
 
F

Frederick Gotham

Al Balmer posted:
<snip>

Since it's already consumed far too much time and space, I suggest
that you just use "ordinary", which I think everyone agrees on.


I disagree. It seems blatantly obvious that people are engaging in this
discussion purely out of curiosity and interest -- and that doesn't
constitute wasted time or space.

Feel free to killfile the thread if you like (if there's such a facility!).
 
W

Walter Roberson

Frederick Gotham said:
We also use the term "domesticated" to indicate that an animal has been
tamed. For instance:
-John was attacked by dogs down by the lake.
-Were they wild dogs?
-No, domesticated.
All the forms of "domestic" indicate (to us in anyway) something which is
commonplace and not extraordinary.

"domestic" is a shortened form of the Latin "domesticus" which is
derived from "domus" meaning house. The dogs in the example above
are domesticated because they live around humans, around human
habitation. Things likely to be found around houses are more likely
to be commonplace and not extraordinary, but it is possible, for example,
to have a domesticated leopard or boa constrictor.

In the Oxford English Dictionary (oed.com), *none* of the shades of
meaning of "domestic" (including in the latest drafts) is listed as
pertaining to that which is commonplace and ordinary.

Thus, I would judge that your use of "domestic" is a -relatively-
localized extension, which would be likely to confuse most people.
 
F

Frederick Gotham

Walter Roberson posted:

"domestic" is a shortened form of the Latin "domesticus" which is
derived from "domus" meaning house. The dogs in the example above
are domesticated because they live around humans, around human
habitation. Things likely to be found around houses are more likely
to be commonplace and not extraordinary, but it is possible, for
example, to have a domesticated leopard or boa constrictor.

In the Oxford English Dictionary (oed.com), *none* of the shades of
meaning of "domestic" (including in the latest drafts) is listed as
pertaining to that which is commonplace and ordinary.

Thus, I would judge that your use of "domestic" is a -relatively-
localized extension, which would be likely to confuse most people.


Actually, this thread has opened my eyes to something.

I've never thought of the word "domestic" as meaning "from home" or "from
this nation or country"... and only now do I see that that's how
American's tend to use the word.

Take the term, "domestic violence"; I always used to think that it meant
"common, everyday violence", but now it seems as though it actually means
"violence in the home". Am I right?
 
R

Roberto Waltman

Frederick Gotham wrote
Actually, this thread has opened my eyes to something.

I've never thought of the word "domestic" as meaning "from home" or "from
this nation or country"... and only now do I see that that's how
American's tend to use the word.

Take the term, "domestic violence"; I always used to think that it meant
"common, everyday violence", but now it seems as though it actually means
"violence in the home". Am I right?

That's the only meaning I understood for that expression.
(Not an American, but living in the USA.)
 
R

Richard Heathfield

Frederick Gotham said:

I've never thought of the word "domestic" as meaning "from home" or "from
this nation or country"... and only now do I see that that's how
American's tend to use the word.

It's how the British use the word, too - especially those with a Latin
O-level lurking in their background.
Take the term, "domestic violence"; I always used to think that it meant
"common, everyday violence",

No, the term for that is "football".
but now it seems as though it actually means "violence in the home".

That's precisely what it means, yes.
 
R

Robert Gamble

Frederick said:
Walter Roberson posted:




Actually, this thread has opened my eyes to something.

I've never thought of the word "domestic" as meaning "from home" or "from
this nation or country"... and only now do I see that that's how
American's tend to use the word.

I've never seen the word used any other way, by American or otherwise.

I took you up on your statement that the definition of domestic has a
different meaning in Ireland by searching for the word on Ireland-only
websites, after reviewing some several dozen uses of the word I am
beginning to think that you are alone in the confusion of its usage as
I didn't see one instance of it used the way you have described, either
that or it has an extremely localized connotation.
Take the term, "domestic violence"; I always used to think that it meant
"common, everyday violence", but now it seems as though it actually means
"violence in the home". Am I right?

Yes, that's what it means in Ireland too:
http://www.womensaid.ie/pages/domestic/domestic.htm

Robert Gamble
 
M

Morris Dovey

Frederick Gotham (in [email protected]) said:

| Take the term, "domestic violence"; I always used to think that it
| meant "common, everyday violence", but now it seems as though it
| actually means "violence in the home". Am I right?

Right. It's another oxymoron, since the phrase is applied only to
violence between members of a family. I've never heard of the term
used to describe violent treatment of intruders.
 
A

Al Balmer

Al Balmer posted:



I disagree. It seems blatantly obvious that people are engaging in this
discussion purely out of curiosity and interest -- and that doesn't
constitute wasted time or space.

Feel free to killfile the thread if you like (if there's such a facility!).

Nah, I'm currently set to switch threads on subject change, so it just
pops up again. How about we discuss the World Cup matches, instead?
 
F

Flash Gordon

Richard said:
Frederick Gotham said:



It's how the British use the word, too - especially those with a Latin
O-level lurking in their background.

The same applies to English people without any ability in any foreign
language.
No, the term for that is "football".

That is a comparatively recent and unfortunate development. I say that
as someone who does not like football.
That's precisely what it means, yes.

Indeed. That is the only meaning that would ever have occurred to me.
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top