Jump to page: 1 2
Thread overview
Obsecure problem 1
Jul 30, 2022
pascal111
Jul 30, 2022
rikki cattermole
Jul 30, 2022
pascal111
Jul 30, 2022
Salih Dincer
Jul 30, 2022
pascal111
Jul 31, 2022
pascal111
Jul 31, 2022
Salih Dincer
Jul 31, 2022
FeepingCreature
Jul 31, 2022
Salih Dincer
Jul 31, 2022
pascal111
Jul 31, 2022
Salih Dincer
Jul 31, 2022
pascal111
Jul 31, 2022
frame
Jul 30, 2022
frame
Jul 30, 2022
pascal111
Jul 30, 2022
frame
Jul 30, 2022
pascal111
July 30, 2022

I've typed a code to enjoy with my library "dcollect", and found non-understandable error:

module main;

import std.stdio;
import dcollect;
import std.string;
import std.conv;

int main(string[] args)
{

    string sentence_x,
    sent_result, token2;
    string[] sentence_tokens;

    writeln(strstring(5,"*")," No tail \"s\" ",strstring(5,"*"));
    writeln;

    write("Compose a line: ");
    sentence_x=strip(readln());

    sentence_tokens=sentence_x.d_strtok(" ,;.:!?\"'");

    foreach(token; sentence_tokens){
        token2=token.strrtrim;
        if(token2[token2.length-1]=='s')
            token2=strdel(token2, token2.length-1,1);
            sent_result~=token2~" ";}

    sent_result.writeln;

	return 0;
}

dcollect library:
https://github.com/pascal111-fra/D/blob/main/dcollect.d

I changed some data types of some functions parameters to "ulong":

string strdel(const string ch, ulong x, ulong l )
{

    string l_ch=ch.strleft(x);
    string r_ch=ch.strright(ch.length-(x+l));

    return (l_ch~=r_ch);

}

string strleft(const string ch, ulong n)
{

    string ch_sub;

    ch_sub=ch[0..n];

    return ch_sub;

}

/************************************/

string strreverse(const string ch)
{

    string ch_rev;

    for(ulong i=ch.length-1; i>=0; i--)
        ch_rev~=ch[i];


    return ch_rev;

}

/*********************************************/

string strright(const string ch, ulong n)
{

    string ch_sub1,
    ch_sub2;

    ch_sub1=strreverse(ch);

    ch_sub2=strleft(ch_sub1, n);

    ch_sub1=strreverse(ch_sub2);

    return ch_sub1;

}

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

Running screen says:

https://i.postimg.cc/G3YyCmbF/Screenshot-from-2022-07-30-23-23-59.png

July 31, 2022
It is a pretty straight forward.

You tried to access memory out of bounds of the slice.

https://github.com/pascal111-fra/D/blob/main/dcollect.d#L34

That for loop is problematic in a number of ways.

You should not use int, or uint to index into memory, only size_t should be used. It is an alias to either uint or ulong based upon the size of a pointer.

```d
for(size_t i = ch.length - 1; i >= 0; i--)
```

Would be the corrected line.

However, it is still not correct, what happens when ch.length is zero? It'll wrap around and become a very large number. That is most likely what happened here.

To do this safely in D, use the foreach_reverse statement instead. There are very few reasons to use for loops in D.

https://dlang.org/spec/statement.html#foreach-range-statement

Adjusted:

```d
foreach_reverse(i; 0 .. ch.length)
```

However this is not efficient as you are reallocating constantly.

```d
char[] ch_rev;
ch_rev.length = ch.length;

size_t offset;
foreach_reverse(c; ch)
	ch_rev[offset++] = c;
```
July 30, 2022

On Saturday, 30 July 2022 at 21:24:50 UTC, pascal111 wrote:

>

I've typed a code to enjoy with my library "dcollect", and found non-understandable error:
...

Running screen says:

https://i.postimg.cc/G3YyCmbF/Screenshot-from-2022-07-30-23-23-59.png

Why you don't copy the output instead?

A range violation is the safe error (if bound checking enabled) that is thrown if you access an index of a range or array that is out of bounds. You may try to access an index that is larger than elements in the array.

July 30, 2022

On Saturday, 30 July 2022 at 21:52:42 UTC, frame wrote:

>

On Saturday, 30 July 2022 at 21:24:50 UTC, pascal111 wrote:

>

I've typed a code to enjoy with my library "dcollect", and found non-understandable error:
...

Running screen says:

https://i.postimg.cc/G3YyCmbF/Screenshot-from-2022-07-30-23-23-59.png

Why you don't copy the output instead?

Because copying the running window contents is not allowed, I couldn't do it in Code::Blocks.

>

A range violation is the safe error (if bound checking enabled) that is thrown if you access an index of a range or array that is out of bounds. You may try to access an index that is larger than elements in the array.

I think you are right.

July 30, 2022
On Saturday, 30 July 2022 at 21:48:35 UTC, rikki cattermole wrote:
> It is a pretty straight forward.
>
> You tried to access memory out of bounds of the slice.
>
> https://github.com/pascal111-fra/D/blob/main/dcollect.d#L34
>
> That for loop is problematic in a number of ways.
>
> You should not use int, or uint to index into memory, only size_t should be used. It is an alias to either uint or ulong based upon the size of a pointer.
>
> ```d
> for(size_t i = ch.length - 1; i >= 0; i--)
> ```
>
> Would be the corrected line.
>
> However, it is still not correct, what happens when ch.length is zero? It'll wrap around and become a very large number. That is most likely what happened here.
>
> To do this safely in D, use the foreach_reverse statement instead. There are very few reasons to use for loops in D.
>
> https://dlang.org/spec/statement.html#foreach-range-statement
>
> Adjusted:
>
> ```d
> foreach_reverse(i; 0 .. ch.length)
> ```
>
> However this is not efficient as you are reallocating constantly.
>
> ```d
> char[] ch_rev;
> ch_rev.length = ch.length;
>
> size_t offset;
> foreach_reverse(c; ch)
> 	ch_rev[offset++] = c;
> ```

Your suggestion works. It seems a problem with that "for" loop, but I don't know exactly from where it got a 0 or subtracted of 0.

I changed data types of some parameters and for loops, and changed that for loop as you suggested:

https://github.com/pascal111-fra/D/blob/main/dcollect.d

The program works fine now:

https://i.postimg.cc/3wkgXmVs/Screenshot-from-2022-07-31-00-04-23.png
July 30, 2022

On Saturday, 30 July 2022 at 22:13:55 UTC, pascal111 wrote:

>

Because copying the running window contents is not allowed, I couldn't do it in Code::Blocks.

Not allowed? o.O

Did you try to select the text and insert it via middle mouse button in another window? Those terminals usually copy the text by selection automatically.

July 30, 2022

On Saturday, 30 July 2022 at 22:17:10 UTC, pascal111 wrote:

>

The program works fine now:

https://i.postimg.cc/3wkgXmVs/Screenshot-from-2022-07-31-00-04-23.png

I have a suggestion for you. Use modern possibilities instead of applying functions used in the past to the present. For example, in a world with UTF, ordinary string reversing can result in erroneous results.

Also, please don't use pictures, use Markdown instead...

SDB@79

July 30, 2022

On Saturday, 30 July 2022 at 22:28:52 UTC, frame wrote:

>

On Saturday, 30 July 2022 at 22:13:55 UTC, pascal111 wrote:

>

Because copying the running window contents is not allowed, I couldn't do it in Code::Blocks.

Not allowed? o.O

Did you try to select the text and insert it via middle mouse button in another window? Those terminals usually copy the text by selection automatically.

I tried what you said, and also ctrl+c doesn't work, nothing works, I can't copy the contents of this terminal.Now you have two choices, 1) I continue using pictures 2) I stop that and be unable to describe what's happening in the running time.

July 30, 2022

On Saturday, 30 July 2022 at 22:45:14 UTC, Salih Dincer wrote:

>

On Saturday, 30 July 2022 at 22:17:10 UTC, pascal111 wrote:

>

The program works fine now:

https://i.postimg.cc/3wkgXmVs/Screenshot-from-2022-07-31-00-04-23.png

I have a suggestion for you. Use modern possibilities instead of applying functions used in the past to the present. For example, in a world with UTF, ordinary string reversing can result in erroneous results.

I'm trying, really, to understand modern D features, but I'm from old school, C pure programming and BASIC language, and such languages and styles.

D as it's obvious to all of us is a multi-paradigm language and has so advanced features, even its references, I can't understand 'em well, it's 100% new modern language, but every time I'll learn a new feature of it for that I can't surround all of its techniques at once.

>

Also, please don't use pictures, use Markdown instead...

Provide me a free solution better than code::blocks with available gdc compiler I found.

>

SDB@79

July 31, 2022
Another version of the program:

https://github.com/pascal111-fra/D/blob/main/proj04.d
« First   ‹ Prev
1 2