Shift elements of an array

A

alf.p.steinbach

good for who?

"whom"

I.e., "good for whom?".

You can also ask, "who is it good for?".

For more info, see <url: http://www.elearnenglishlanguage.com/difficulties/whowhom.html>.

That said, goodness of code is sort of like entropy, in that the more accurately you try to pin it down, the more fuzzy and ill-defined it gets, BUT ordinarily the difference between much of it and none of it is so exceedingly great that the fine details don't matter.

I.e., to a first approximation, good code is good for everybody. ;-)


[snip]
i not agree, i consider all memory accessible read-write
and if there are mem place where i have to write only one time
i write it... but i never have problem about this

for example the string

"%s\n"

in the format of

printf("%s\n", a)

is conceptually a const string
for the compiler a const string
for the machine a read write string
but even if i possibly can write on that
never happen it

so i think all your tell of how usefull is of const keyword
is void
in pratical case

A practical motivation for using "const" is just that the tools support andenforce it.

For example,

Code:
#include <iostream>
using namespace std;

int main()
{
char* p = "Blah blah blah, const is so overrated.";
p[0] = 'b';
cout << p << endl;
}

Compiling and running with Visual C++ 11.0 (Visual C++ 2012):

[example]
[D:\dev\test]
CL=/EHsc /GR /FI"iso646.h" /we4627 /we4927 /W4 /D"_CRT_SECURE_NO_WARNINGS" /nologo

[D:\dev\test]
cl foo.cpp foo.cpp

[D:\dev\test]
foo

-- A crash dialog pops up, http://imgur.com/bpZDHIe

[D:\dev\test]
echo %errorlevel% -1073741819

[D:\dev\test]
[/code]

The process exit code reported by the ERRORLEVEL pseudo-variable happens tobe Windows error code 5 translated to an "NT status code" (what you have in Windows structured exceptions), 0x0xC0000005, where code 5 is "Access is denied" with symbolic constant ERROR_ACCESS_DENIED in <winerror.h>.

So the effect was as-if it that literal string were read only, which it probably was. ;-)

With "const" you would have got a compilation error instead.

For a larger program a compilation error (via "const") would save you a lotof systematic testing of runtime behavior, and associated bug hunting.


Cheers & hth.,

- Alf
 
M

Martin Shobe

вторник, 8 октÑÐ±Ñ€Ñ 2013 г., 22:06:18 UTC+4 пользователь Martin Shobe напиÑал:
On 10/8/2013 11:45 AM, Vlad from Moscow wrote:
[snip]
Those had been optimized away.
it is not guaranteed by the Standard.

And yet zero allocations is not the "at least two" that you claimed.

Martin Shobe
 
R

Rosario1903

"whom"

I.e., "good for whom?".

You can also ask, "who is it good for?".

ok
is it good for whom?

i above would say that the error of rewrite "%s\n" string for accident
is rare.

i not imagine where one const can be useful
on 99% of remain data
one use read-write
A practical motivation for using "const" is just that the tools support and enforce it.

For example,
Code:
#include <iostream>
using namespace std;

int main()
{
char* p = "Blah blah blah, const is so overrated.";
p[0] = 'b';
cout << p << endl;
}

Compiling and running with Visual C++ 11.0 (Visual C++ 2012):

[example]
[D:\dev\test]
CL=/EHsc /GR /FI"iso646.h" /we4627 /we4927 /W4 /D"_CRT_SECURE_NO_WARNINGS" /nologo

[D:\dev\test]
cl foo.cpp foo.cpp

[D:\dev\test]
foo

-- A crash dialog pops up, http://imgur.com/bpZDHIe

for me it is one error compiler/OS/cpu, and for who wrote the spec for
them
[D:\dev\test]
echo %errorlevel% -1073741819

[D:\dev\test]
[/code]

The process exit code reported by the ERRORLEVEL pseudo-variable
happens to be Windows error code 5 translated to an
"NT status code" (what you have in Windows structured
exceptions), 0x0xC0000005, where code 5 is
"Access is denied" with symbolic
constant ERROR_ACCESS_DENIED in <winerror.h>.
So the effect was as-if it that literal string
were read only, which it probably was. ;-)
With "const" you would have got a compilation error instead.

good,
this would be the example?

example of what?
of how complicate the easy

if i want to write in the string

"1234"

than why it is a error?

if i not want to write in the string

"1234"

i not write in it...

for error i can write "1234", but the error is rare
somehting can happen 3-5 times a life

and for this i have to write 2 3 "const" in each line
function is defined [arg defined etc varible defined]
and all the compiler iussue about const and not const etc
in functios and operator???
 
R

Rosario1903

On Mon, 7 Oct 2013 13:39:21 -0700 (PDT), alf wrote:

your line long is > of len==70 so there are problem in posting reply
to your posts, here
This group used to be frequented by the language creator (Bjarne),
by the committee secretaries who penned the standard (first
Andrew, then Pete), and by lots of committee members, compiler
writers (including Greg Comeau and Daveed) and so on.

if the above would be right some thread would with + post
but i think big programmers are better things to do that hear
some Ngs post...

they would program instead of write here
About the only authoritative people

authoritative is the experience one have
I see here now are some old-time clc++-ers like Ian Collins
and "Tib" (I can never recall the name!),
plus us old clc++m moderators (James, Victor and myself).
That is, to the degree that having participated for a
long time on Usenet confers any authority (except
that James is also a committee member, I think).
So it's gone downhill with the influx of novices
and trolls, called "November" in Usenet jargon.

the ethernal november...
 
Ö

Öö Tiib

For example,
Code:
#include <iostream>
using namespace std;

int main()
{
char* p = "Blah blah blah, const is so overrated.";
p[0] = 'b';
cout << p << endl;
}

Compiling and running with Visual C++ 11.0 (Visual C++ 2012):

[example]
[D:\dev\test]
CL=/EHsc /GR /FI"iso646.h" /we4627 /we4927 /W4 /D"_CRT_SECURE_NO_WARNINGS" /nologo

[D:\dev\test]
cl foo.cpp foo.cpp

[D:\dev\test]
foo

-- A crash dialog pops up, http://imgur.com/bpZDHIe

for me it is one error compiler/OS/cpu, and for who wrote the spec for
them

For you it is error of compiler but for C++ standard it is error of
developer. Attempts to modify string literals are undefined behavior.
Usually standard wins in comp.lang.c++ newsgroup.
if i want to write in the string
"1234"
than why it is a error?

Who said that it is error if you want to write into string?
Error is when you implement what you want incorrectly. Writing
into string literal is undefined. Writing into your own array
however must work on conforming compiler:

#include <iostream>
int main()
{
char p[] = "Blah blah blah, const is so overrated.";
p[0] = 'b';
std::cout << p << std::endl;
}
 
V

Vlad from Moscow

Ñреда, 9 октÑÐ±Ñ€Ñ 2013 г., 14:58:53 UTC+4 пользователь Juha Nieminen напиÑал:
Vlad said:
Before something saying about C++ you at first need to learn C++ and what are arrays in C++. One more arrays are not classes in C++. They have fixed sized at compile time that must be specified by constant expressions.



The next C++ standard will include support for variable-length arrays.

In other words, you will be able to say things like:



void foo(unsigned size)

{

int array[size];

...

}

Variable length arrays are not what you think about them. They are fixed size arrays. only the size is calculated during run-time when the array is created. Nevertheless you may not add new elements or delete existent elements of VLA.
What will you call them then?


I call them arrays because in fact nothing was changed in their organization.
I also find it amusing how you define std::array as "not an array" even

though it's right there in the name, and it fulfills *all* of your

definitions of "C++ array" (fixed size, size must be known at

compile time.)

std::array is a structure. Do you know what structure is in C/C++? I wonderthat you spend so much time writing here posts and you did not find time to read at least one time neither the definition of std::array nor the definition of arrays in C/C++ Standards.
 
V

Vlad from Moscow

Ñреда, 9 октÑÐ±Ñ€Ñ 2013 г., 15:38:27 UTC+4 пользователь Martin Shobe напиÑал:
вторник, 8 октÑÐ±Ñ€Ñ 2013 г., 22:06:18 UTC+4 пользователь Martin Shobe напиÑал:
On 10/8/2013 11:45 AM, Vlad from Moscow wrote:
[snip]


Secondly operator + again allocates memory to accomodate elements of this temporary object with elements of the original string. And thirdly thememory occupied by the original string has to be substituted for the new one memory.

Those had been optimized away.


it is not guaranteed by the Standard.



And yet zero allocations is not the "at least two" that you claimed.



Martin Shobe



I do not see a zero allocation. Before calling operator + its arguments must be ecaluated. So the constructor std:;string( n, ' ' ) will allocate memory. The operator + itself uses function insert std::move(rhs.insert(0, lhs)), that also must allocate memory to insert string referenced by lhs.

Even if the compiler optimizes some way the code it does not mean that you should write bad code hoping that the compiler will correct it.:)
 
A

Alf P. Steinbach

std::array is not an array meeting the
definition thereof in §3.9.2 and §8.3.4/1 of the standard.

8.3.4/1 provides a rich description, and has e.g. "element type" in
italics as a definition. But it does not define "array". The term
"array" is simply USED, not defined here.

That's much like the sentence "An elephant is an animal" is not a
definition of "animal" but a just a simple use of the term "animal".

In addition to the paragraphs you mention you can look up the use in (in
particular) the valarray section, where "array" is used in the computer
science meaning, there referring to class type objects.

However, you're also partly right, because throughout the standard,
including in 8.3.4, the word "array" GENERALLY denotes a "raw array".

E.g. "pointer to array" means "pointer to raw array".

But as mentioned, in particular in the valarray section "array" is
instead used in the general computer science meaning, so it's used with
at least two meanings, one very specific and one very general.

The underlying assumption is that the reader is knowledgeable enough to
understand the meaning from the context, like a compiler writer -- for
unlike e.g. the original C# standard, the C++ standard is not written or
organized for learners.

***

The main problem with the whole trolling about this, is that nobody's
pointed at the idea that one can telepathically know what the OP meant.

Since the 2000's trolls in nearly all technical forums have relied on
the notion of telepathy and magical insight into others' intentions and
motivations, very seldom with anyone pointing out how unrealistic that
is, but instead with regular users tacitly making the same assumptions.

So let me just mention now that

telepathy does not exist,
no matter what Alan Turing thought about that.

***

Anyway, in the absence of clarification the broadest possible meaning is
the default, much like "I drive a car" does not mean "I own a Mazda car"
-- there needs not be an ownership, the car needs not be a Mazda.


Cheers & hth.,

- Alf
 
A

Alf P. Steinbach

Sorry, I don't agree. §3.9.2, in describing the compound types,
italicizes "array" as a defined expression

No, that's clarifying emphasis of what each list item is about: none of
the items there have descriptions (so cannot be definitions), only some
have further references.

However, I am not going to involve myself further with one of your
prolix and pointlessly argumentative threads.

With the personal attack you say that you know that you're wrong, AND
that you're trolling.

Plink.

- Alf
 
Ö

Öö Tiib

No - see above. You simply misunderstand the standard. (And,
incidentally, this from someone who described another poster's comments
a few days ago as "inane".)

Pots and kettles. "Any argument about this [array] is pointless." said
you yesterday. So why you continue doing that pointless thing in 3
more posts? Alf is correct, that is trolling.
 
V

Vlad from Moscow

I do not have any desire to comment your stupidy. One more look at least one time in your life the definition of std::array. It is a structure if you d0 not know this till now. Do you know what is the structure? It seems thatyou know neither what is structure nor what is array.

Ñуббота, 12 октÑÐ±Ñ€Ñ 2013 г., 1:32:53 UTC+4 пользователь Juha Nieminen напиÑал:
 

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