Jump to page: 1 2
Thread overview
Using "strcpy" to assign value to dynamic char array
Nov 01, 2021
pascal111
Nov 01, 2021
pascal111
Nov 01, 2021
Ali Çehreli
Nov 01, 2021
pascal111
Nov 01, 2021
Ali Çehreli
Nov 02, 2021
pascal111
Nov 01, 2021
Ali Çehreli
Nov 02, 2021
pascal111
Nov 03, 2021
pascal111
Nov 03, 2021
Imperatorn
Nov 03, 2021
tsbockman
Nov 03, 2021
Ali Çehreli
Nov 04, 2021
jfondren
Nov 05, 2021
pascal111
November 01, 2021

I know that I can use the next syntax to assign new value to char dynamic array, and the new value isn't in same length of the current value of the array:

{

char[] s="xyz".dup;

s="Hello World!".dup;

writeln(s);
}

Result:

Hello World!

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

But what if I want to use "strcpy" function to assign that new value to the array that the problem is that the array won't take more than its first initializing value length:

{

char[] s="xyz".dup;

strcpy(&s[0], "Hello World!");

writeln(s);

}

Result:

Hel

November 01, 2021

On 11/1/21 3:56 PM, pascal111 wrote:

>

But what if I want to use "strcpy" function to assign that new value to the array that the problem is that the array won't take more than its first initializing value length:

{

char[] s="xyz".dup;

strcpy(&s[0], "Hello World!");

writeln(s);

}

Result:

Hel

Don't do this, you just corrupted memory! You wrote 13 bytes into a memory location that contains 3.

Use .dup, it does the equivalent of strcpy.

Can you share why you want to use strcpy here?

-Steve

November 01, 2021

On Monday, 1 November 2021 at 20:15:14 UTC, Steven Schveighoffer wrote:

>

On 11/1/21 3:56 PM, pascal111 wrote:

>

But what if I want to use "strcpy" function to assign that new value to the array that the problem is that the array won't take more than its first initializing value length:

{

char[] s="xyz".dup;

strcpy(&s[0], "Hello World!");

writeln(s);

}

Result:

Hel

Don't do this, you just corrupted memory! You wrote 13 bytes into a memory location that contains 3.

Use .dup, it does the equivalent of strcpy.

Can you share why you want to use strcpy here?

-Steve

Yes, I'm practicing doing things in low level style like standard C.

November 01, 2021
On 11/1/21 1:49 PM, pascal111 wrote:

> Yes, I'm practicing doing things in low level style like standard C.

All you needed extra was to let the slice know about the new length:

import std.stdio;
import core.stdc.string;

void main() {
  char[] s="xyz".dup;
  strcpy(&s[0], "Hello World!");
  s = s.ptr[0..12];           // <-- Here
  writeln(s);
}

Now, the result is "correct" without dup:

Hello World!

The program is as incorrect as its C equivalent would be. ;)

Ali

November 01, 2021
On Monday, 1 November 2021 at 21:01:31 UTC, Ali Çehreli wrote:
> On 11/1/21 1:49 PM, pascal111 wrote:
>
> > Yes, I'm practicing doing things in low level style like
> standard C.
>
> All you needed extra was to let the slice know about the new length:
>
> import std.stdio;
> import core.stdc.string;
>
> void main() {
>   char[] s="xyz".dup;
>   strcpy(&s[0], "Hello World!");
>   s = s.ptr[0..12];           // <-- Here
>   writeln(s);
> }
>
> Now, the result is "correct" without dup:
>
> Hello World!
>
> The program is as incorrect as its C equivalent would be. ;)
>
> Ali

This can serve the style I want. It uses OOP style like C++ by putting a pointer as a property, but pointers themselves are low level.
November 01, 2021
On 11/1/21 2:01 PM, Ali Çehreli wrote:

> The program is as incorrect as its C equivalent would be. ;)

I wrote a cool function to make it easy to disregard memory safety:

import std.stdio;

auto assumedLength(S)(ref S slice) {
  struct LengthSetter {
    void opAssign(size_t length) {
      // Nooo! :)
      *cast(size_t*)(&slice) = length;
    }
  }

  return LengthSetter();
}

void main() {
  auto arr = [ 1, 2, 3 ];
  arr.assumedLength = 10;
  writeln(arr);
}

Joking aside, I liked the nested struct and its opAssign to mimic internal `arr.length = 42` syntax. (I know it involves a potentially expensive delegate but still...)

Ali


November 01, 2021
On 11/1/21 2:28 PM, pascal111 wrote:

> This can serve the style I want.

I am feeling funny right now and showing incorrect code. It's impossible to fit "Hello World!" in "xyz". As Steve said, don't do that. :)

> It uses OOP style like C++ by putting a
> pointer as a property,

D's slices are the equivalent of the following struct (showing for int):

struct __Slice__ {
  size_t length;
  int * ptr;
}

So, .ptr is simply a member variable. (Assigning to .length does some magic like allocating more memory in some cases; otherwise it is a member variable as well.)

> but pointers themselves are low level.

arr.ptr is as low as it gets: It is a pointer.

Ali

November 02, 2021
On Monday, 1 November 2021 at 21:32:21 UTC, Ali Çehreli wrote:
> On 11/1/21 2:01 PM, Ali Çehreli wrote:
>
> > The program is as incorrect as its C equivalent would be. ;)
>
> I wrote a cool function to make it easy to disregard memory safety:
>
> import std.stdio;
>
> auto assumedLength(S)(ref S slice) {
>   struct LengthSetter {
>     void opAssign(size_t length) {
>       // Nooo! :)
>       *cast(size_t*)(&slice) = length;
>     }
>   }
>
>   return LengthSetter();
> }
>
> void main() {
>   auto arr = [ 1, 2, 3 ];
>   arr.assumedLength = 10;
>   writeln(arr);
> }
>
> Joking aside, I liked the nested struct and its opAssign to mimic internal `arr.length = 42` syntax. (I know it involves a potentially expensive delegate but still...)
>
> Ali

This function seems smart and flexible and higher than my current level, I'll study it.
November 02, 2021
On Monday, 1 November 2021 at 21:37:59 UTC, Ali Çehreli wrote:
> On 11/1/21 2:28 PM, pascal111 wrote:
>
> > This can serve the style I want.
>
> I am feeling funny right now and showing incorrect code. It's impossible to fit "Hello World!" in "xyz". As Steve said, don't do that. :)
>
> > It uses OOP style like C++ by putting a
> > pointer as a property,
>
> D's slices are the equivalent of the following struct (showing for int):
>
> struct __Slice__ {
>   size_t length;
>   int * ptr;
> }
>
> So, .ptr is simply a member variable. (Assigning to .length does some magic like allocating more memory in some cases; otherwise it is a member variable as well.)
>
> > but pointers themselves are low level.
>
> arr.ptr is as low as it gets: It is a pointer.
>
> Ali

Anyway, I'm a beginner and any information is useful to me.
November 02, 2021

On 11/1/21 9:03 PM, pascal111 wrote:

>

On Monday, 1 November 2021 at 21:32:21 UTC, Ali Çehreli wrote:

> >

Joking aside
...

This function seems smart and flexible and higher than my current level, I'll study it.

Please please, do NOT study this code. It is bad all around. Ali should know better ;)

He is joking with the code posted, it should not be used in any real code ever. Not only is it corrupting memory, it may not work as expected in all cases (and will randomly fail).

Please learn first how memory works before writing the low-level bits of arrays!

As a beginner, I suggest studying what a buffer overrun is, and why it is bad.

-Steve

« First   ‹ Prev
1 2