Shift elements of an array

A

alf.p.steinbach

one other error

Well you haven't got to a FIRST error yet.

But you think that you have.

This group used to be frequented by the language creator (Bjarne), by the committee secretaries who penned the standard (first Andrew, then Pete), andby lots of committee members, compiler writers (including Greg Comeau and Daveed) and so on.

About the only authoritative people 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 withthe influx of novices and trolls, called "November" in Usenet jargon.

This means that you would do well by generally IGNORING or at least CHECKING statements here that are not rooted in simple verifiable fact, or clear logic.

there is no need of this ungly word "const"

In the C++11 standard the easiest way to confirm that it indeed is needed is the Annex C (about C compatibility) discussion of §2.14.5 (string literals), which includes this example:

char* p = "abc"; // valid in C, invalid in C++

But if someone coughs that up as a reference, then don't just blindly believe it. For EXAMPLES in the standard are not "normative", which means that they can be wrong. Indeed, a whole bunch of examples in C++98 were incorrect(it's mostly corrected now in C++11).

The standardeese that that now forbids that implicit conversion is difficult to find, because it was done by simply REMOVING the C++03 text that allowed it,

C++03 §4.2/2
"A string literal (2.13.4) that is not a wide string literal can be
converted to an rvalue of type “pointer to char”;"

I.e., it's a case of finding the absence of text ;-); in C++11 there is no such.

Here's an example:

Code:
#include <iostream>
using namespace std;

// Intentionally does not compile with CONFORMING compiler:
void say( char* s )
{
cout << s << endl;
}

auto main() -> int
{
say( "Uh oh..." );
}

Compiling with a MinGW Windows variant of g++ (the GNU C++ compiler) in C++11-confoming mode -- I'm not quite sure what options I've used, because it's all in the configuration:

[example]
[D:\dev\test]
g++ foo.cpp
foo.cpp: In function 'int main()':
foo.cpp:12:21: error: deprecated conversion from string constant to 'char*'[-Werror=write-strings]
cc1plus.exe: some warnings being treated as errors

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


Cheers & hth.,

- Alf
 
A

alf.p.steinbach

influx of novices and trolls, called "November" in Usenet jargon.

Oh sorry I meant "September".

I was thinking about something else.

But as a relevant side note, I believe the free NNTP server called "EternalSeptember" is still up and running, and using that, e.g. via Mozilla Thunderbird, I will not have to deal with this Google Groups interface that double-spaces text, drops references, sabotages signature markers etc (I can't really believe that it's just incompetence!).


Cheers, & sorry 'bout that,

- Alf
 
R

red floyd

Oh sorry I meant "September".

I was thinking about something else.

But as a relevant side note, I believe the free NNTP server called "Eternal September" is still up and running, and using that, e.g. via Mozilla Thunderbird, I will not have to deal with this Google Groups interface that double-spaces text, drops references, sabotages signature markers etc (I can't really believe that it's just incompetence!).

eternal-september.org *is* still up and running, Alf. That's what I'm
using to post right now.
 
A

alf.p.steinbach

eternal-september.org *is* still up and running, Alf. That's what I'm

using to post right now.

Thanks!

Will get to it (register anew) today or tomorrow I think.

Nice to see yet another known face. :)


Cheers,

- Alf
 
R

Rosario1903

Well you haven't got to a FIRST error yet.

But you think that you have.

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.

About the only authoritative people I see here now are some
old-time clc++-ers like Ian Collins

yes Ian Collins... where is his example for resolve OP problem?
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.

i'm not too much smart, it is so that i like simple languages, that
show well what one do, and are scalable in complexity
This means that you would do well by generally IGNORING or at
least CHECKING statements here that are not rooted in
simple verifiable fact, or clear logic.

free of ignoring; i not have too much to show. i not wrote big
programs
In the C++11 standard the easiest way to confirm that it
indeed is needed is the Annex C (about C compatibility) discussion
of §2.14.5 (string literals), which includes this example:

char* p = "abc"; // valid in C, invalid in C++

why i would not write in the mem where there are "abc"?
But if someone coughs that up as a reference, then don't just
blindly believe it. For EXAMPLES in the standard are not "normative",
which means that they can be wrong. Indeed, a whole bunch of
examples in C++98 were incorrect (it's mostly corrected now in C++11).

why add one word "const", that has the only purpose of add complexity?
The standardeese that that now forbids that implicit conversion
is difficult to find, because it was done by simply REMOVING the
C++03 text that allowed it,

C++03 §4.2/2
"A string literal (2.13.4) that is not a wide string literal can be
converted to an rvalue of type “pointer to char”;"

I.e., it's a case of finding the absence of text ;-);
in C++11 there is no such.

Here's an example:

Code:
#include <iostream>
using namespace std;

// Intentionally does not compile with CONFORMING compiler:
void say( char* s )
{
cout << s << endl;
}

auto main() -> int
{
say( "Uh oh..." );
}

Compiling with a MinGW Windows variant of g++ (the GNU C++
compiler) in C++11-confoming mode -- I'm not quite sure what
options I've used, because it's all in the configuration:

[example]
[D:\dev\test]
g++ foo.cpp
foo.cpp: In function 'int main()':
foo.cpp:12:21: error: deprecated conversion from
string constant to 'char*' [-Werror=write-strings]

string costant is not a string constant
it is a char array where one can write
and we resolve the problem
cc1plus.exe: some warnings being treated as errors

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


Cheers & hth.,

- Alf
 
R

Rosario1903

It will be funny to count how many times the memory is
deallocated and allocated anew and how many times constructors
including the move assignment operator are called in
this stupid statement.

{ s = s.erase( 0, n ) + string( n, ' ' ); }

why you not write some code and post it?
 
T

Tobias Müller

Rosario1903 said:
why i would not write in the mem where there are "abc"?

1. Because the standard says so

2. Many compilers do string pooling: All string literals with the same
value may point to the same memory. If you change one of them, you change
all.

3. Because string literals are "const", the compiler is allowed to put them
into memory that is marked as read-only. If you try to manipulate, your
program will crash.
why add one word "const", that has the only purpose of add complexity?

While "const" adds complexity, it also adds safety, performance and
correctness.

- Safety: "const" helps to propagate the information which memory is read
only and not allowed to be changed.

- Correctness: If a function takes an argument per const& or const*, it
tells you "I won't modify your variable", which is useful documentation. It
prevents accidental data modification.

- Performance: The compiler can use more optimizations on constant data,
like string pooling mentioned before.
The standardeese that that now forbids that implicit conversion
is difficult to find, because it was done by simply REMOVING the
C++03 text that allowed it,

C++03 §4.2/2
"A string literal (2.13.4) that is not a wide string literal can be
converted to an rvalue of type “pointer to char”;"

I.e., it's a case of finding the absence of text ;-);
in C++11 there is no such.

Here's an example:

Code:
#include <iostream>
using namespace std;

// Intentionally does not compile with CONFORMING compiler:
void say( char* s )
{
cout << s << endl;
}

auto main() -> int
{
say( "Uh oh..." );
}

Compiling with a MinGW Windows variant of g++ (the GNU C++
compiler) in C++11-confoming mode -- I'm not quite sure what
options I've used, because it's all in the configuration:

[example]
[D:\dev\test]
g++ foo.cpp
foo.cpp: In function 'int main()':
foo.cpp:12:21: error: deprecated conversion from
string constant to 'char*' [-Werror=write-strings]

string costant is not a string constant
it is a char array where one can write
and we resolve the problem

We're not discussing about changing the language, but about writing correct
code in C++.

If you think that C++ is too complex, then use another language. For
example an untyped or weakly type language, that has less restrictions. But
be aware, less restrictions means also less guarantees.

Tobi
 
V

Vlad from Moscow

It is simply is very silly and bad code. The memory allocated and deallocated at least two times.And this code demonstrates only one intention that the author is unable to write programs.

In this simple function that contains only two lines in the body the authormade three serious errors.

First the function has undefined behaviour because its second parameter is defined as int. So any value of type std::string::size_type that can not befit into int will result in undefined behaviour of the function.

Second, the interface of the function is inconsistent with the interface ofstd::basic_string. See the description of std::basic_string in the C++ Standard.

Third, this function allocates and deallocates memory at least two times. As a result 1) the memory will be fragmented; 2) it is possible that allocating of memory will fail for large strings.
And it is a stupidy to allocate a new memory then the original string has enough memory that to do the operation "in place".

It needs to have a great talant that to write such a dull code!:) If a programmer is unable to write even such simple functions with only two lines ofcode then what to say about his capabilities to write code in whole?!!! :)


вторник, 8 октÑÐ±Ñ€Ñ 2013 г., 9:10:24 UTC+4 пользователь Paavo Helde напиÑал:
 
V

Vlad from Moscow

No problem!

The correct definition of the function looks the following way

std::string & shift_left( std::string &s, std::string::size_type n = 1 )
{
return ( s.erase( 0, n ).append( n, ' ' ) );
}

Or if you do not like when two operations are written in one line you can rewrite it as

std::string & shift_left( std::string &s, std::string::size_type n = 1 )
{
s.erase( 0, n );

return ( s.append( n, ' ' ) );
}

This function does not make silly and unnecessary allocation of memory. So it is very efficient and robust. It has consistent interface with other functions of std::basic_string and has no side effects with undefined behaviour as the previous implementation of the function.

Enjoy!:)


вторник, 8 октÑÐ±Ñ€Ñ 2013 г., 9:12:19 UTC+4 пользователь Rosario1903 напиÑал:
 
R

Rosario1903

On Mon, 07 Oct 2013 21:29:51 +0200, Rosario1903 wrote:

who know if there are memory leak?...

#include <iostream>
#include <cstdlib>

using namespace std;

#define u32 unsigned
#define u8 unsigned char
#define i8 char
#define F for
#define R return

class mya{
public:
~mya(){free(arr);}
mya(){arr=(u8*) malloc(15);
if(arr==0) exit(-1);
s=15;
}

mya(u32 c)
{if(c>0xFFFF) exit(-1);
arr=(u8*)malloc(c);
if(arr==0) exit(-1);
s=c;
}

mya(u8* c)
{u32 v, i;
if(c==0)
{v=1;}
else {v=strlen((i8*)c)+1;}
arr=(u8*)malloc(v);
if(arr==0) exit(-1);
if(v==1) *arr=0;
else {F(i=0; i<v ; ++i)
{arr=c;
if(c==0) break;
}
F(; i<v;++i)
arr=0;
}
s=v;
}

mya& operator=(const mya& r)
{u32 i;

if(s<r.s)
{free(arr);
arr=(u8*) malloc(r.s);
if(arr==0) exit(-1);
s=r.s;
}
F(i=0; i<r.s; ++i)
arr=r.arr;
R *this;
}

mya& operator=(u8* rs)
{u32 i, len, len1;

if(rs==0) exit(-1);
len=strlen((i8*)rs);
if(len>0xFFFFF) exit(-1);
len1=len+1;

if(s<len1)
{free(arr);
arr=(u8*) malloc(len1);
if(arr==0) exit(-1);
s=len1;
}
F(i=0; i<len1; ++i)
arr=rs;
R *this;
}


/* lo chiamano costruttore di copia mha...
dicono che serve in:
mya b, a=b;
spero che la memoria nn sia gia' inizializzata
*/
mya(const mya& r)
{u32 i;
arr=(u8*)malloc(r.s);
if(arr==0) exit(-1);
s=r.s;
F(i=0; i<s; ++i)
arr=r.arr;
}

friend ostream& operator<<(ostream& os, mya& ma)
{u32 i;
if(ma.s!=0)
{F(i=0; i<ma.s-1; ++i)
{if(ma.arr==0) os<<" ";
else os<<ma.arr;
}
R os<<ma.arr;
}
else R os;
}

void shiftd(mya& ma, u32 pos)
{u32 i, j;
if(s<ma.s)
{free(arr);
arr=(u8*)malloc(ma.s);
if(arr==0) exit(-1);
s=ma.s;
}
if(pos>=ma.s)
{F(i=0;i<s; ++i) arr=0;}
else {/* 0 1 2 3 4 5 6 */
/* | shiftd 3*/
F(j=pos, i=0; j<ma.s; ++j,++i)
arr=ma.arr[j];
F(;i<s;++i)
arr=0;
}
}

void shiftm(mya& ma, u32 pos)
{u32 i, j, k;
if(s<ma.s)
{free(arr);
arr=(u8*)malloc(ma.s);
if(arr==0) exit(-1);
s=ma.s;
}
if(pos>=ma.s)
{F(i=0;i<s; ++i) arr=0;}
else {/* 0 1 2 3 4 5 6 */
/* | shiftm 3*/
F(i=0; i<pos; ++i)
arr=0;
k=ma.s-pos;
F(j=0; j<k; ++j,++i)
arr=ma.arr[j];
F(;i<s;++i)
arr=0;
}
}


friend mya* forCompilerMya(u32 a);

friend mya& operator<<(mya& ma, u32 pos)
{mya *b;
b=forCompilerMya(15);
(*b).shiftd(ma, pos);
R *b;
}

friend mya& operator>>(mya& ma, u32 pos)
{mya *b;
b=forCompilerMya(15);
(*b).shiftm(ma, pos);
R *b;
}

friend mya& operator|(mya& ma, u32 c)
{mya *b;
b=forCompilerMya(15);
*b=ma;
b->arr[0]|=c;
R *b;
}



friend mya& operator^(mya& ma, mya& mb)
{mya *b;
u32 M, m, i;
M=ma.s>mb.s? ma.s: mb.s;
m=ma.s>mb.s? mb.s: ma.s;

b=forCompilerMya(M);
F(i=0; i<m; ++i)
b->arr=ma.arr^mb.arr;
F( ; i<M; ++i)
b->arr=0;
R *b;
}


u32 s;
u8 *arr;
};

mya **bi=0;
u32 membi=0;
u32 index=0;

void fill_bi(mya* a)
{mya *r, **j;
u32 i;
if(index<=membi)
{if(membi>0xFFFFF) exit(-1);
i=membi+128; /* 128 0..127 */
j=(mya**)realloc(bi, i*sizeof *j);
if(j==0) exit(-1);
bi=j; membi=i;
}
bi[index]=a;
++index;
}

void zero_bi(void)
{mya *a;
u32 i;

if(index!=0)
{F(i=index-1; ; --i)
{a=bi;
free(a->arr);
free(a);
bi=0;
if(i==0) break;
}
}
index=0;
}

void free_bi(void)
{zero_bi(); free(bi); bi=0; membi=0;}

mya* allocMya(u32 a)
{mya *b;
if(a>0xFFFF) exit(-1);
b=(mya*)malloc(sizeof *b);
if(b==0) exit(-1);
b->arr=(u8*)malloc(a);
if(b->arr==0)exit(-1);
b->s=a;
R b;
}

mya* forCompilerMya(u32 a)
{mya *b;
b=allocMya(a);
fill_bi(b);
R b;
}

mya& f(void)
{mya a((u8*)"1 2 3 4"), *b;
b=forCompilerMya(15);
*b=a;
R *b;
}


int main(void)
{mya v((u8*)"this and that "), ma, b, c[40];
u32 i, *w;
u8 *cs="A0CDEFGHILMN";

cout<<"at start v=["<<v <<"]\n";
ma.shiftd(v, 3);
cout<<"at end ma=["<<ma<<"]\n";

F(i=0;i<12;++i)
{ma.shiftd(ma, 1);
cout<<"at end ma=["<<ma<<"]\n";
}

b=v;
cout<<"b="<<b<<"\n";
b=f(); zero_bi();
cout<<"b="<<b<<"\n";
F(i=0; i<4; ++i)
{c=cs;
++cs[0];++cs[1];++cs[2];++cs[3];
}
F(i=0; i<4; ++i)
cout<<"c["<<i<<"]=["<<c<<"]\n";
b=(((c[0]>>1)|'Q')>>1|'v');
cout<<"b=["<<b<<"]\n";
cout<<"index="<<index<<"\n";
zero_bi();
c[0]= (u8*)"";
b=c[0]^c[1] ;
cout<<"index="<<index<<"\n";

cout<<"b="<<b<<"\n";

free_bi();

R 0;
}
 
I

Ian Collins

Rosario1903 said:
On Mon, 07 Oct 2013 21:29:51 +0200, Rosario1903 wrote:

who know if there are memory leak?...

#include <iostream>
#include <cstdlib>

using namespace std;

#define u32 unsigned
#define u8 unsigned char
#define i8 char
#define F for
#define R return

Why don't you go back to trolling on c.l.c?
 
R

Rosario1903

mya **bi=0;
u32 membi=0;
u32 index=0;

void fill_bi(mya* a)
{mya *r, **j;
u32 i;
if(index<=membi) ^^^^^^^^^^^^^^^
if(index>=membi)

{if(membi>0xFFFFF) exit(-1);
i=membi+128; /* 128 0..127 */
j=(mya**)realloc(bi, i*sizeof *j);
if(j==0) exit(-1);
bi=j; membi=i;
}
bi[index]=a;
++index;
}
 
R

Rosario1903

Why don't you go back to trolling on c.l.c?

how all you are smart :)
if continue in this way there would be nobody would understand C++
code really [means can decompose it for find the micro instructions it
is composed]
 
I

Ian Collins

Rosario1903 said:
Why don't you go back to trolling on c.l.c?

how all you are smart :)
if continue in this way there would be nobody would understand C++
code really [means can decompose it for find the micro instructions it
is composed]

What have your stupid cryptic macros and C code wrapped up as C++ got to
do with understanding C++?
 
R

Rosario1903

No problem!

The correct definition of the function looks the following way

std::string & shift_left( std::string &s, std::string::size_type n = 1 )
{
return ( s.erase( 0, n ).append( n, ' ' ) );
}

Or if you do not like when two operations are written
in one line you can rewrite it as

std::string & shift_left( std::string &s, std::string::size_type n = 1 )
{
s.erase( 0, n );

return ( s.append( n, ' ' ) );
}

This function does not make silly and unnecessary allocation
of memory. So it is very efficient and robust. It has consistent
interface with other functions of std::basic_string and has no side
effects with undefined behaviour as
the previous implementation of the function.

Enjoy!:)

sorry i never had used the C++ string library so i not know...

but the allocation of mem could be in "s.append( n, ' ' )"
and in the copy the result "s" the same, that routine return with
the "=" operator in b=shift_left(s)
 
Ö

Öö Tiib

You are wrong. You omit the important characteristic of arrays that they
have fixed sizes.

Your top posting that does not even quote where you think I am wrong
(just "generally wrong"?) indicates that you are actually just trolling
and not serious. Why such low traffic Usenet group? Are you hurt? If so
stay here, we are kind, do not attempt any "epic edit wars" in Wikipedia:

http://en.wikipedia.org/wiki/Variable-length_array
They are unable to add new elements orto delete existent elements.
http://en.wikipedia.org/wiki/Dynamic_array

It is this their characteristic that is the reason that other containers
as for example std::vector are required.

Also that: http://en.wikipedia.org/wiki/Sparse_array

Arrays have been here since first programming languages. It does not
matter that one popular C++ template implementing sparse array of Ts is
named as 'boost::numeric::ublas::mapped_vector<T>' by its authors. It is
an array. Redefining the fundamental blocks of computer science takes lot
more than just saying that "you are wrong". :D
 
V

Vlad from Moscow

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.

All the stupidy you wrote here you can redirect the same dumbs as you are.

вторник, 8 октÑÐ±Ñ€Ñ 2013 г., 16:06:34 UTC+4 пользователь Öö Tiib напиÑал:
 
V

Vlad from Moscow

And please at least one time in your life read the C++ Sandard what are arrays in C++. And for example do not mix up arrays with class std::array because the last is not an array.:)


вторник, 8 октÑÐ±Ñ€Ñ 2013 г., 16:06:34 UTC+4 пользователь Öö Tiib напиÑал:
 
V

Vlad from Moscow

вторник, 8 октÑÐ±Ñ€Ñ 2013 г., 16:06:34 UTC+4 пользователь Öö Tiib напиÑал:
On Monday, 7 October 2013 21:00:26 UTC+3, Vlad from Moscow wrote:

Arrays have been here since first programming languages. It does not

matter that one popular C++ template implementing sparse array of Ts is

named as 'boost::numeric::ublas::mapped_vector<T>' by its authors. It is

an array. Redefining the fundamental blocks of computer science takes lot

more than just saying that "you are wrong". :D


You simply do not know what are arrays in C++. And template classes you arereferencing are not arrays. They are classes. Classes and array are two totally different notions in C++. Moreover for example in C there is no classes.:)

As I said you should at least one time in you life read the C++ Standard that to know at last what are arrays in C++.
 
V

Vlad from Moscow

If you do not know what is array in C/C++ it is your personal problem. But I am not going to read the next stupidy of the next dumb. Moreover you should at least know that there is no special type of arrays in C++ that differs from arrays in C. It is the same notion in the both languages.
In the C++ TSandard arrays are described in section 8.3.4 Arrays while in the C TSandard they are described in sections 6.2.5 Types and 6.5.2.1 Array subscripting.

For example in the C Standard there is written

— An array type describes a contiguously allocated nonempty set of objects with a particular member object type, called the element type

All what you imagine yourself about arrays has nothing common with notions of arrays defined in C and C++ Standards. It is only your fantasies.



вторник, 8 октÑÐ±Ñ€Ñ 2013 г., 17:10:41 UTC+4 пользователь David Brown напиÑал:
 

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,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top