Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-17 Thread rempas via Digitalmars-d-learn
On Sunday, 15 October 2023 at 07:22:53 UTC, Imperatorn wrote: You already got a lot of good answers, I thought I'd just share this for anyone searching for nogc string formatting compatible with betterC: https://code.dlang.org/packages/bc-string You thought, well, thank you very much and

Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-14 Thread rempas via Digitalmars-d-learn
On Friday, 13 October 2023 at 10:11:33 UTC, Nick Treleaven wrote: You can also do it using a string mixin: mixin(create_fn!(mixin("`", i, "`"))); I think that's equivalent to `i.stringof` anyway. Thank you for the info!

Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-14 Thread rempas via Digitalmars-d-learn
On Tuesday, 10 October 2023 at 16:11:57 UTC, bachmeier wrote: Which part uses Phobos? The linked function compiles without importing anything. Actually, you are right. I didn't give a lot of thought to it, as there is the line `char[] ret = new char[](length);` but I can replace it with an

Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-10 Thread rempas via Digitalmars-d-learn
On Tuesday, 10 October 2023 at 11:46:38 UTC, Hipreme wrote: My engine has its own implementation of toString(long), which does not have dependency with the C runtime: https://github.com/MrcSnm/HipremeEngine/blob/master/modules/util/source/hip/util/conv.d#L180C1-L208C2 I have reimplemented the

Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-10 Thread rempas via Digitalmars-d-learn
On Tuesday, 10 October 2023 at 11:45:25 UTC, Dennis wrote: The result of `.stringof` is implementation defined, it can be used for debugging but don't make your program's semantics depend on the output of it. ... ... ...That being said, this trick can be used to convert an integer to

Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-10 Thread rempas via Digitalmars-d-learn
On Tuesday, 10 October 2023 at 05:32:52 UTC, Imperatorn wrote: If count < 10 then why not just ```d import std; static foreach(c; "0123456789") {  mixin(create_fn!(c)); } enum create_fn(char num) = ` auto function_`~ num ~`() => "Hello from function `~ num

Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-10 Thread rempas via Digitalmars-d-learn
On Monday, 9 October 2023 at 22:49:11 UTC, Salih Dincer wrote: Great masters generally warn to stay away from stringof. Please do not use it as much as possible. The following code snippet will be useful to you: ```d alias CN = __traits(allMembers, CardinalNumbers); static foreach(i; CN) {

Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-09 Thread rempas via Digitalmars-d-learn
On Monday, 9 October 2023 at 17:52:58 UTC, Jonathan M Davis wrote: The closest to a built-in solution would be toString on classes and structs, but structs don't necessarily have a toString. Rather, in many cases, Phobos does introspection on the struct to figure out how to convert the

Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-09 Thread rempas via Digitalmars-d-learn
On Monday, 9 October 2023 at 17:42:48 UTC, Imperatorn wrote: You could just add your own int to string I guess? That will be a good idea! I'll do it in the future if that is the case, as it's not important, and I want to finish my job. Thank you and have a great day!

Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-09 Thread rempas via Digitalmars-d-learn
On Monday, 9 October 2023 at 16:53:55 UTC, mw wrote: but you `import std.stdio;`? Or copy the std/conv.d over to your build, or copy / write a toString(int) function yourself, which is compile-time callable. I do on that example just to use "write". It wouldn't be necessary, but I just

Re: How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-09 Thread rempas via Digitalmars-d-learn
On Monday, 9 October 2023 at 16:42:38 UTC, mw wrote: use: import std.conv; [...] Damn, sorry, forgot to mention. I cannot use Phobos.

How to use ".stringof" to get the value of a variable and not the name of the variable (identifier) itself?

2023-10-09 Thread rempas via Digitalmars-d-learn
Let's see the following example: ```d import std.stdio; static foreach(i; 0 .. 10) { mixin(create_fn!(i.stringof)); } enum create_fn(string num) = ` void function_`~ num ~`() { writeln("Hello from function `~ num ~`!"); } `; void main() { function10(); } ``` I'm trying to create a

Re: malloc error when trying to assign the returned pointer to a struct field

2023-09-09 Thread rempas via Digitalmars-d-learn
On Saturday, 9 September 2023 at 10:54:34 UTC, bachmeier wrote: Hate to be that guy, but I posted a link to a stackoverflow question with the exact error message you were getting, and the solution. And I told you I had experienced the same error and that question fixed it. No reason to not

Re: malloc error when trying to assign the returned pointer to a struct field

2023-09-09 Thread rempas via Digitalmars-d-learn
On Saturday, 9 September 2023 at 09:56:59 UTC, H. S. Teoh wrote: libc doesn't know what you intended. All it knows is that you asked it for 20 bytes (even though you actually needed 40), then later on its internal structures are corrupted (because you thought you got 40 bytes; storing data

Re: malloc error when trying to assign the returned pointer to a struct field

2023-09-09 Thread rempas via Digitalmars-d-learn
On Saturday, 9 September 2023 at 09:47:14 UTC, Steven Schveighoffer wrote: You are focusing on the wrong problem. You asked for size bytes, and malloc gave you size bytes. It doesn't "know" anything special. Then you proceeded at some point to write *past* the size bytes. What did you

Re: malloc error when trying to assign the returned pointer to a struct field

2023-09-09 Thread rempas via Digitalmars-d-learn
On Saturday, 9 September 2023 at 09:04:18 UTC, Steven Schveighoffer wrote: This is not ideal. Why? Because 99% of the time, a poster has come here with a problem they don't know how to solve, and have focused in on where they *think* the problem is. However, the problem isn't there. But us

Re: malloc error when trying to assign the returned pointer to a struct field

2023-09-09 Thread rempas via Digitalmars-d-learn
On Saturday, 9 September 2023 at 08:54:14 UTC, Brad Roberts wrote: I'm pretty sure this is your problem. You're allocating size bytes which is only going to work where sizeof(T) == 1. Changing to malloc(size * sizeof(T)) is likely going to work better. Oh man That was it! I had forget

Re: malloc error when trying to assign the returned pointer to a struct field

2023-09-09 Thread rempas via Digitalmars-d-learn
On Friday, 8 September 2023 at 19:48:33 UTC, Basile B. wrote: My idea was that if you dont have defined a copy constructor and if an instance is assigned to another, then that other instance share the same pointer, which can cause memory errors. To eliminate that risk and to detect where

Re: malloc error when trying to assign the returned pointer to a struct field

2023-09-09 Thread rempas via Digitalmars-d-learn
On Friday, 8 September 2023 at 19:14:47 UTC, H. S. Teoh wrote: The error message looks to me like a corruption of the malloc heap. These kinds of bugs are very hard to trace, because they may go undetected and only show up in specific circumstances, so small perturbations of completely

Re: malloc error when trying to assign the returned pointer to a struct field

2023-09-09 Thread rempas via Digitalmars-d-learn
On Friday, 8 September 2023 at 19:14:47 UTC, H. S. Teoh wrote: My guess is that you have a double-free somewhere, or there's a buffer overrun. Or maybe some bad interaction with the GC, e.g. if you tried to free a pointer from the GC heap. (Note that this may not immediately show up; free()

Re: malloc error when trying to assign the returned pointer to a struct field

2023-09-09 Thread rempas via Digitalmars-d-learn
On Friday, 8 September 2023 at 16:17:15 UTC, Richard (Rikki) Andrew Cattermole wrote: I would strongly suggest that you log all memory sizes that are allocated, and double check that you do free. Also turn on ASAN in ldc.

Re: malloc error when trying to assign the returned pointer to a struct field

2023-09-08 Thread rempas via Digitalmars-d-learn
On Friday, 8 September 2023 at 16:02:36 UTC, Basile B. wrote: Could this be a problem of copy construction ? I don't think so. The assertion seems to be violated when `malloc` is used. And when I assert the result in the `_ptr` field. Really weird...

Re: I don't understand betterC

2023-09-08 Thread rempas via Digitalmars-d-learn
On Monday, 4 September 2023 at 07:39:21 UTC, confused wrote: So then I guess I'd still like to know how I'm expected to store and access an array of characters without the C runtime as I tried in my original post. You are going to allocate memory using the system call of your operation

Re: malloc error when trying to assign the returned pointer to a struct field

2023-09-08 Thread rempas via Digitalmars-d-learn
On Friday, 8 September 2023 at 07:59:37 UTC, rempas wrote: [I do have ... I do use ldc2 and `betterC`!] For anyone who is still following, first of all thanks! Second, I have bad news and good news! The bad news is that I tested in an Linux Mint system (mine is an Arch Linux) and the it

Re: malloc error when trying to assign the returned pointer to a struct field

2023-09-08 Thread rempas via Digitalmars-d-learn
On Friday, 8 September 2023 at 14:40:13 UTC, Richard (Rikki) Andrew Cattermole wrote: No, for this you need ModuleInfo. The order is sequential on what it sees first. Personally I test using full D rather than -betterC. For dub: ```json "configurations": [ { "name":

Re: malloc error when trying to assign the returned pointer to a struct field

2023-09-08 Thread rempas via Digitalmars-d-learn
On Friday, 8 September 2023 at 14:50:17 UTC, Kagamin wrote: Did you run this example program above? Does it crash? I didn't as I suppose that it would had no problems as it works for you. Either that, or it will be indeed a problem with my Glibc. I did run it now after your reply and it

Re: malloc error when trying to assign the returned pointer to a struct field

2023-09-08 Thread rempas via Digitalmars-d-learn
On Friday, 8 September 2023 at 13:34:42 UTC, Richard (Rikki) Andrew Cattermole wrote: In case you didn't know, all you need to get unittests working in -betterC is: ```d foreach (module_; allModules) { foreach (unitTest;

Re: malloc error when trying to assign the returned pointer to a struct field

2023-09-08 Thread rempas via Digitalmars-d-learn
On Friday, 8 September 2023 at 13:05:47 UTC, evilrat wrote: You run with -unittest compiler flag? Well, that does nothing for me with betterc (without it is ok). I did stupid and unsafe things like malloc(0) and writing out of bounds but still no crash, it works fine. I guess it depends

Re: malloc error when trying to assign the returned pointer to a struct field

2023-09-08 Thread rempas via Digitalmars-d-learn
On Friday, 8 September 2023 at 09:25:59 UTC, Hipreme wrote: Hello, not completely unrelated to your problem, I have also done something like that, and when you're in D, don't use simply a pointer and length like that, use the `slice` operator. See references:

Re: malloc error when trying to assign the returned pointer to a struct field

2023-09-08 Thread rempas via Digitalmars-d-learn
On Friday, 8 September 2023 at 09:07:12 UTC, evilrat wrote: Hard to tell from that code but it is quite unlikely there is a compiler bug in such simple use case. I assume you already tried debugging your program, right? Yep! I have spent days and it's these kinds of bugs that burn me off

malloc error when trying to assign the returned pointer to a struct field

2023-09-08 Thread rempas via Digitalmars-d-learn
I do have the following struct: ```d struct Vec(T) { private: T* _ptr = null; // The pointer to the data u64 _cap = 0; // Total amount of elements (not bytes) we can store public: /* Create a vector by just allocating memory for it. The null terminator is not set for strings as,

Re: How does D’s ‘import’ work?

2023-06-19 Thread rempas via Digitalmars-d-learn
On Monday, 19 June 2023 at 12:48:26 UTC, Cecil Ward wrote: If I have sources to all the library routines, not libraries or .obj files. I am simply completely ignorant about the D tools including DUB, so off to do some reading. I’ve just been seeing how good LDC and GDC are, and the answer is

Re: How does D’s ‘import’ work?

2023-06-19 Thread rempas via Digitalmars-d-learn
On Sunday, 18 June 2023 at 20:17:50 UTC, Cecil Ward wrote: target.obj: target.c include1.h include2.h cc.exe cc target.c and you either have to pray that you have kept the list of .h files that are mentioned inside target.c and other .h files referenced recursively from within these .h

Re: How does D’s ‘import’ work?

2023-06-19 Thread rempas via Digitalmars-d-learn
On Sunday, 18 June 2023 at 20:24:10 UTC, Cecil Ward wrote: I wasn’t intending to use DMD, rather ldc if possible or GDC because of their excellent optimisation, in which DMD seems lacking, is that fair? (Have only briefly looked at dmd+x86 and haven’t given DMD’s back end a fair trial.)

Re: need `this` for `this` of type `ref @safe Test(string reg_arg)

2023-06-18 Thread rempas via Digitalmars-d-learn
On Sunday, 18 June 2023 at 19:22:45 UTC, Paul Backus wrote: No, there is no way to pass template arguments to a constructor without using `__ctor`. My recommendation is to use a free function or a `static` method instead. For example: ```d import std.stdio; struct Test {} Test

Re: need `this` for `this` of type `ref @safe Test(string reg_arg)

2023-06-18 Thread rempas via Digitalmars-d-learn
On Sunday, 18 June 2023 at 18:17:16 UTC, Paul Backus wrote: `__ctor` doesn't create a new object, it initializes an existing object. You need to create the object first, then call `__ctor` on it: ```d void main() { Test test; test.__ctor!("non_def")("Hello"); } ``` Thank you! Do you

need `this` for `this` of type `ref @safe Test(string reg_arg)

2023-06-18 Thread rempas via Digitalmars-d-learn
Ok, so I'm having a struct that has a constructor that takes a template parameter. I suppose this question could also be named `how to initialize constructors with template parameters` but whatever! The funny thing is, I think that I may have already found how to do it in the past but I

Re: How does D’s ‘import’ work?

2023-06-18 Thread rempas via Digitalmars-d-learn
On Thursday, 8 June 2023 at 04:17:20 UTC, Cecil Ward wrote: I was thinking about the situation in C where I have a rule in a make file that lists the .h files as well as the .c all as dependencies in creating an object file. I don't think I'm aware of what you mean with "lists .h and .c

Re: How does D’s ‘import’ work?

2023-06-18 Thread rempas via Digitalmars-d-learn
On Wednesday, 31 May 2023 at 18:43:52 UTC, Cecil Ward wrote: Is there an explanation of how D’s ‘import’ works somewhere? I’m trying to understand the comparison with the inclusion of .h files, similarities if any and differences with the process. I do wonder why no-one have linked the

Re: How does D’s ‘import’ work?

2023-06-02 Thread rempas via Digitalmars-d-learn
On Friday, 2 June 2023 at 11:27:31 UTC, Andrew wrote: Others can correct me if I'm wrong, but I don't think that it is a priority for D to be specially compatible with makefiles in any way beyond the whole separation of compilation and linking. If he was talking about using GNU Make as a

Re: How does D’s ‘import’ work?

2023-06-02 Thread rempas via Digitalmars-d-learn
On Thursday, 1 June 2023 at 03:47:00 UTC, Cecil Ward wrote: I have another question if I may, what do we do about getting makefiles right given that we have imports ? What do you mean with that? Give some more info please!

Re: How does D’s ‘import’ work?

2023-06-02 Thread rempas via Digitalmars-d-learn
On Wednesday, 31 May 2023 at 18:43:52 UTC, Cecil Ward wrote: Is there an explanation of how D’s ‘import’ works somewhere? I’m trying to understand the comparison with the inclusion of .h files, similarities if any and differences with the process. I do wonder why no-one have linked the

Re: How does D’s ‘import’ work?

2023-06-02 Thread rempas via Digitalmars-d-learn
On Wednesday, 31 May 2023 at 18:43:52 UTC, Cecil Ward wrote: Is there an explanation of how D’s ‘import’ works somewhere? I’m trying to understand the comparison with the inclusion of .h files, similarities if any and differences with the process. I do wonder why no-one have linked the

Re: How can a function pointer required to be extern(C)?

2023-04-13 Thread rempas via Digitalmars-d-learn
On Wednesday, 12 April 2023 at 21:00:04 UTC, John Chapman wrote: You can also express it like this: ```d extern(C) alias FuncPtr = void* function(void*); ``` Thank you! This is how I was planning to do anyway because other that the fact that I like the syntax of that a little bit more,

Re: How can a function pointer required to be extern(C)?

2023-04-13 Thread rempas via Digitalmars-d-learn
On Wednesday, 12 April 2023 at 20:36:59 UTC, H. S. Teoh wrote: IMO this is a bug either in D's syntax or in the parser. I'd file an enhancement request. In the meantime, you can use alias as a workaround: ---snip--- extern(C) void* abc(void*) {return null;} alias FuncPtr =

How can a function pointer required to be extern(C)?

2023-04-12 Thread rempas via Digitalmars-d-learn
Sorry if the title doesn't make any sense, let me explain. So, I do have the following code that does not compile: ```d import core.sys.posix.pthread; /* The library */ struct Thread { private: pthread_t thread_id; public: this(void* function(void*) func, void* arg = null, scope

Re: @nogc and Phobos

2023-04-12 Thread rempas via Digitalmars-d-learn
On Tuesday, 14 March 2023 at 20:40:39 UTC, bomat wrote: Sounds intriguing. Anything I can look at yet? Or still all top secret? :) Oh, amazing! I let you on waiting for almost a month and I wouldn't had see it if I didn't came to post another question. I'm so so sorry for the waiting,

Re: @nogc and Phobos

2023-03-11 Thread rempas via Digitalmars-d-learn
On Saturday, 11 March 2023 at 16:21:40 UTC, bomat wrote: first: I didn't want to sound aggressive or like I was trying to bash D, sorry if it came across like that. Oh don't worry! People have openly criticized (and still doing) the language before. It's nothing to worry, criticism help us

Re: How can I get the scalar type of a pointer to pointer (and in even deeper levels)

2023-03-11 Thread rempas via Digitalmars-d-learn
On Saturday, 11 March 2023 at 13:37:26 UTC, ag0aep6g wrote: On 11.03.23 14:22, rempas wrote: On Saturday, 11 March 2023 at 12:59:59 UTC, ag0aep6g wrote: alias Foo(T : U*, U) = Foo!U; alias Foo(T) = T; static assert(is(Foo!(int*) == int)); static assert(is(Foo!(int**) == int)); static

Re: How can I get the scalar type of a pointer to pointer (and in even deeper levels)

2023-03-11 Thread rempas via Digitalmars-d-learn
On Saturday, 11 March 2023 at 12:59:59 UTC, ag0aep6g wrote: alias Foo(T : U*, U) = Foo!U; alias Foo(T) = T; static assert(is(Foo!(int*) == int)); static assert(is(Foo!(int**) == int)); static assert(is(Foo!(int***) == int)); static assert(is(Foo!(int) == int)); Wait, but "Foo" is defined

Re: @nogc and Phobos

2023-03-11 Thread rempas via Digitalmars-d-learn
On Saturday, 11 March 2023 at 12:04:25 UTC, bomat wrote: Hello! First of all, let's start by making clear of what `@nogc` means. It it an attribute that is used one function signatures and it annotates that the garbage collector will not be used inside this function. However, in the scenario

How can I get the scalar type of a pointer to pointer (and in even deeper levels)

2023-03-11 Thread rempas via Digitalmars-d-learn
Let's see the following code: ```d void test_fn(T)(T val) { pragma(msg, "The type of the pointer is: " ~ typeof(*val).stringof); } extern (C) void main() { int* int_ptr = cast(int*)0x1037; char* char_ptr = cast(char*)0x1037; test_fn(int_ptr); test_fn(char_ptr); } ``` This function

Re: Is there a way to get the name of the current function?

2023-03-07 Thread rempas via Digitalmars-d-learn
On Tuesday, 7 March 2023 at 22:28:41 UTC, ryuukk_ wrote: ``` import std.stdio; void main() { writeln("file:", __FILE__); writeln("function is: ", __FUNCTION__); writeln("function is: ", __PRETTY_FUNCTION__ ); } $ dmd -run tester.d file:tester.d function is:

Re: Is there a way to get the name of the current function?

2023-03-07 Thread rempas via Digitalmars-d-learn
On Tuesday, 7 March 2023 at 22:19:22 UTC, JG wrote: Yes, see here: https://dlang.org/spec/expression.html#specialkeywords OMG, so that's when they are located! I was trying to search the spec and find them in different pages but had no luck! Thanks a lot for the help, have a great day!

Is there a way to get the name of the current function?

2023-03-07 Thread rempas via Digitalmars-d-learn
For example, in the given code: ```d void my_function() { import std.stdio; writeln("The name of the function is: ", ); } ``` Is there something to put in the place of `` to get the name of the function?

Re: How would the equivalent C type be in D?

2023-03-07 Thread rempas via Digitalmars-d-learn
On Tuesday, 7 March 2023 at 09:05:45 UTC, FeepingCreature wrote: Yay! Yes, that is a bit weird. First of all, the actual signal is 11 [...] Thank you for the info!

Re: How would the equivalent C type be in D?

2023-03-01 Thread rempas via Digitalmars-d-learn
On Wednesday, 1 March 2023 at 08:26:07 UTC, FeepingCreature wrote: 11 is SIGSEGV. A segfault, or access violation, happens when you try to access unallocated memory. In this case, let me annotate your code so it's easier to see what's happening: ```d // null is the default value for a

How would the equivalent C type be in D?

2023-03-01 Thread rempas via Digitalmars-d-learn
I'm looking into [this](https://www.x.org/releases/X11R7.7/doc/libxcb/tutorial/index.html) tutorial to learn XCB and I'm trying to write the code in D with betterC. In the section 9.1 (sorry, I cannot give a section link, the article does not give us this ability), I'm facing a problem and my

Re: How ptr arithmitic works??? It doesn't make any sense....

2022-12-06 Thread rempas via Digitalmars-d-learn
On Monday, 5 December 2022 at 22:21:06 UTC, Salih Dincer wrote: Yeah, there is such a thing! I'm sure you'll all like this example: [...] Great example! Thank you my friend!

Re: How ptr arithmitic works??? It doesn't make any sense....

2022-12-05 Thread rempas via Digitalmars-d-learn
On Monday, 5 December 2022 at 18:01:47 UTC, ag0aep6g wrote: You can use bracket notation with pointers. You just need to move your closing parenthesis a bit. Assuming that `ptr` is a `void*`, these are all equivalent: ```d (cast(int*) ptr)[i] = whatever; *((cast(int*) ptr) + i) = whatever;

Re: How ptr arithmitic works??? It doesn't make any sense....

2022-12-05 Thread rempas via Digitalmars-d-learn
On Monday, 5 December 2022 at 08:21:44 UTC, bauss wrote: Because it's much easier to work with. Ex. if you have an array of 4 signed 32 bit integers that you're pointing to then you can simply just increment the pointer by 1. If it was raw bytes then you'd have to increment the pointer by

Re: How ptr arithmitic works??? It doesn't make any sense....

2022-12-05 Thread rempas via Digitalmars-d-learn
On Sunday, 4 December 2022 at 19:00:15 UTC, H. S. Teoh wrote: This is true only if you're talking about pointers in the sense of pointers in assembly language. Languages like C and D add another layer of abstraction over this. Another thing with pointers is that it doesn't have "types".

Re: How ptr arithmitic works??? It doesn't make any sense....

2022-12-04 Thread rempas via Digitalmars-d-learn
On Sunday, 4 December 2022 at 17:27:39 UTC, Nick Treleaven wrote: On Sunday, 4 December 2022 at 16:33:35 UTC, rempas wrote: (MemoryBlock.sizeof is 16 on my 64-bit system). The above adds 16 bytes to ptr. The above adds 16 * MemoryBlock.sizeof bytes (16 * 16) to ptr, because ptr is cast

Re: How ptr arithmitic works??? It doesn't make any sense....

2022-12-04 Thread rempas via Digitalmars-d-learn
On Sunday, 4 December 2022 at 16:40:17 UTC, ag0aep6g wrote: Not quite. Adding 10 to a T* means adding 10 * T.sizeof. Oh! I thought it was addition. Is there a specific reasoning for that if you are aware of?

How ptr arithmitic works??? It doesn't make any sense....

2022-12-04 Thread rempas via Digitalmars-d-learn
First a little bit of theory. A pointer just points to a memory address which is a number. So when I add "10" to this pointer, it will point ten bytes after the place it was pointing to, right? Another thing with pointers is that it doesn't have "types". A pointer always just points to a

Re: How do I initialize a templated constructor?

2022-08-08 Thread rempas via Digitalmars-d-learn
Thank you all for the info! I'll try to find another way to do it as it is not possible to match the exact behavior I want! Have a great day everyone!

Re: How do I initialize a templated constructor?

2022-08-08 Thread rempas via Digitalmars-d-learn
On Monday, 8 August 2022 at 11:03:21 UTC, Dom Disc wrote: But if you only want to know the type of the parameter, you can do this: ```D struct TestArray(ulong element_n) { int[element_n] elements; this(type)(type number) { pragma(msg, "The type is: " ~ type.stringof); } } ```

Re: How do I initialize a templated constructor?

2022-08-08 Thread rempas via Digitalmars-d-learn
On Monday, 8 August 2022 at 08:27:49 UTC, bauss wrote: Yeah I think the only template argument you can have for constructors are `this` which will refer to things like the class that inherited the current class etc. not sure what else, but you can't really pass anything to it yourself

Re: How do I initialize a templated constructor?

2022-08-08 Thread rempas via Digitalmars-d-learn
On Monday, 8 August 2022 at 06:58:42 UTC, bauss wrote: ``` this(string type)(ulong number) { ``` You cannot do this. Instead your type should look like this: First let's change it up a little bit. ``` struct TestArray(ulong element_n, string type) { int[element_n] elements; this(ulong

Re: Hacking C code vs D code

2022-08-08 Thread rempas via Digitalmars-d-learn
On Thursday, 4 August 2022 at 23:11:36 UTC, pascal111 wrote: One of problems faced me in C programming is hacking data with C code that some hackers do with C code which make me needs more tools to protect my C code, but I don't have good resources in my current time, while I noticed that D

Re: CMake and D

2022-08-07 Thread rempas via Digitalmars-d-learn
On Saturday, 6 August 2022 at 18:22:45 UTC, Jan Allersma wrote: I figured out a strategy to solve te problem: 1) Create a C++ function which will be called in D. 2) Build a static C++ library with CMake and add dependencies (In my case: SDL libraries) 3) Create a new project (`dub init`). 4)

How do I initialize a templated constructor?

2022-08-07 Thread rempas via Digitalmars-d-learn
In the following struct (as an example, not real code): ``` struct TestArray(ulong element_n) { int[element_n] elements; this(string type)(ulong number) { pragma(msg, "The type is: " ~ typeof(type).stringof); } } ``` I want to create it and be able to successfully initialize the

Re: How can I match every instance of a template type (struct)?

2022-07-12 Thread rempas via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 13:34:42 UTC, rempas wrote: [...] Thank you all for your help! @Ali Çehreli That makes things much much easier! I'll look at the source code in "traits.d" and I'll copy-paste it into my library ;)

Re: How can I match every instance of a template type (struct)?

2022-07-12 Thread rempas via Digitalmars-d-learn
On Tuesday, 12 July 2022 at 13:37:49 UTC, ag0aep6g wrote: static if (is(typeof(obj) == Test!T, T)) { printf("YES!!!\n"); } Haah? Ok, what does this work anyway? I thought you needed parenthesis for more than 1 templated arguments...

How can I match every instance of a template type (struct)?

2022-07-12 Thread rempas via Digitalmars-d-learn
I want to do something like the following: ```d import core.stdc.stdio; Test!(T, "mode1") make_test(T)(T data) { Test!(T, "mode1") t = { data }; return t; } struct Test(T, string mode = "ref") { T data; } extern (C) void main() { auto obj = make_test(20); static if (is(typeof(obj) ==

Re: How can I check if a type is a pointer?

2022-06-25 Thread rempas via Digitalmars-d-learn
On Saturday, 25 June 2022 at 14:51:49 UTC, Paul Backus wrote: Use an [`is()` expression:][1] ```d if (is(typeof(accepted_type) == T*, T)) { // it's a pointer } ``` In English, you read this as "if `typeof(accepted_type)` matches the pattern `T*`, where `T` is a type." If you want to

Re: How can I check if a type is a pointer?

2022-06-25 Thread rempas via Digitalmars-d-learn
On Saturday, 25 June 2022 at 14:32:27 UTC, Ola Fosheim Grøstad wrote: I guess you can look at the source code for https://dlang.org/phobos/std_traits.html#isPointer Thank you! Nice and quick ;) For anyone interested, here's the source code: ```d enum bool isPointer(T) = is(T == U*, U) &&

How can I check if a type is a pointer?

2022-06-25 Thread rempas via Digitalmars-d-learn
For example, something like the following: ```d void main() { char accepted_type; char* non_accepted_type; if (__traits(isPointer, typeof(accepted_type))) { // The type is not accepted } else { /* The type is not a pointer so its accepted */ } if (__traits(isPointer,

Re: How to map machine instctions in memory and execute them? (Aka, how to create a loader)

2022-06-08 Thread rempas via Digitalmars-d-learn
On Monday, 6 June 2022 at 15:13:45 UTC, rempas wrote: In case someone is wondering, I found an answer in another forum. The code is the following: ```d import core.stdc.stdio; import core.stdc.string; import core.stdc.stdlib; import core.sys.posix.sys.mman; void putbytes(char **code, const

Re: How to map machine instctions in memory and execute them? (Aka, how to create a loader)

2022-06-06 Thread rempas via Digitalmars-d-learn
On Monday, 6 June 2022 at 18:05:23 UTC, Johan wrote: This instruction is wrong. Note that you are writing twice to RDX, but also that you are using `mov sign_extend imm32, reg64` instead of `mov imm64, reg64` (`0x48 0xBA`?). Third, why append an extra zero (`*cast(char*)(code + 32) = 0x00;`)?

Re: How to map machine instctions in memory and execute them? (Aka, how to create a loader)

2022-06-06 Thread rempas via Digitalmars-d-learn
On Monday, 6 June 2022 at 16:24:58 UTC, Guillaume Piolat wrote: See: https://github.com/GhostRain0/xbyak https://github.com/MrSmith33/vox/blob/master/source/vox/utils/mem.d Thank you! And I just noticed that the second source is from Vox

Re: How to map machine instctions in memory and execute them? (Aka, how to create a loader)

2022-06-06 Thread rempas via Digitalmars-d-learn
On Monday, 6 June 2022 at 16:08:28 UTC, Adam D Ruppe wrote: On a lot of systems, it can't be executable and writable at the same time, it is a security measure. see https://en.wikipedia.org/wiki/W%5EX so you might have to mprotect it to remove the write permission before trying to execute

Re: How to map machine instctions in memory and execute them? (Aka, how to create a loader)

2022-06-06 Thread rempas via Digitalmars-d-learn
On Monday, 6 June 2022 at 15:27:12 UTC, Alain De Vos wrote: Note , it is also possible to do inline assembly with asm{...} or __asm(T) {..}. Thank you for the info! I am aware of that, I don't want to practically do this. I just want to learn how it works. It will be useful when I'll built

How to map machine instctions in memory and execute them? (Aka, how to create a loader)

2022-06-06 Thread rempas via Digitalmars-d-learn
I tried to find anything that will show code but I wasn't able to find anything expect for an answer on stackoverflow. I would find a lot of theory but no practical code that works. What I want to do is allocate memory (with execution mapping), add the machine instructions and then allocate

Re: Why is the compiled file size so huge?

2022-05-28 Thread rempas via Digitalmars-d-learn
On Friday, 27 May 2022 at 13:40:25 UTC, Alexander Zhirov wrote: I'm trying to compile a file that weighs 3 kilobytes. I'm also linking a self-written dynamic library. I don't understand why the resulting executable file is so huge? After all, all libraries are present: [...] I did a similar

Re: Why isn't my dynamic array method doesn't work for this type?

2022-05-06 Thread rempas via Digitalmars-d-learn
On Friday, 6 May 2022 at 13:35:13 UTC, Steven Schveighoffer wrote: You don't need to declare an assign operator, the default for structs is to member-wise copy all the values. However, if you do provide one, it will be used. Note that a simple default can be done like: ```d ref My_File

Re: Why isn't my dynamic array method doesn't work for this type?

2022-05-06 Thread rempas via Digitalmars-d-learn
On Thursday, 5 May 2022 at 21:07:22 UTC, colleen camacho wrote: can i use method of Dynamic array in C using malloc library function. Program example will create an integer array of any length dynamically by asking the array size and array elements from user and display on the screen.how

Re: Why isn't my dynamic array method doesn't work for this type?

2022-05-05 Thread rempas via Digitalmars-d-learn
On Thursday, 5 May 2022 at 14:20:49 UTC, Steven Schveighoffer wrote: Your assignment operator does nothing. -Steve Thanks! That was indeed the problem! In the other data structures, it worked without needing explicitly provide one so I didn't thought about it. Thanks a lot, you're saving

Re: Why isn't my dynamic array method doesn't work for this type?

2022-05-05 Thread rempas via Digitalmars-d-learn
On Thursday, 5 May 2022 at 11:49:29 UTC, vit wrote: ```d this.capacity += DEF_VEC_REALLOC_SIZE; //realloc(this.ptr, T.sizeof * DEF_VEC_REALLOC_SIZE); this.ptr = realloc(this.ptr, T.sizeof * this.capacity); //<<-- ``` Oh, right! I forgot to say it. I'm using my own

Why isn't my dynamic array method doesn't work for this type?

2022-05-05 Thread rempas via Digitalmars-d-learn
I have created a structure that is a actually an array that allocates memory and growths. It is a template and it has worked with a couple of types that I have tried with. It doesn't work with one tho and I cannot understand why. I will list the smallest possible code I could. Keep in mind

Re: Create an associative array with function pointers as the value

2022-04-20 Thread rempas via Digitalmars-d-learn
On Wednesday, 20 April 2022 at 14:29:33 UTC, rikki cattermole wrote: On 21/04/2022 2:15 AM, rempas wrote: Unfortunately, this will not work for me as it uses "TypeInfo" and it is not available with "-betterC". Thank you for trying to help regardless! You can't use AA's in -betterC. The

Re: Create an associative array with function pointers as the value

2022-04-20 Thread rempas via Digitalmars-d-learn
On Wednesday, 20 April 2022 at 11:10:18 UTC, vit wrote: You need shared static this for initializing immutable AA: ```d immutable void function(ref file_struct)[string] common_identifiers; shared static this(){ common_identifiers = [ "let" : _let, // "macro" :

Create an associative array with function pointers as the value

2022-04-20 Thread rempas via Digitalmars-d-learn
I'm trying to create an associative array where the keys will be a "string" type and the values will be function pointers. I'm using a custom type is called "file_struct" and for anyone that wants to try specifically with this type, the definition is the following: ```d struct file_struct {

Re: Is it safe to read to memory after it has been allocated with `pureMalloc` and `pureRealloc`?

2022-04-04 Thread rempas via Digitalmars-d-learn
On Monday, 4 April 2022 at 09:26:13 UTC, Stanislav Blinov wrote: No. Neither `malloc` nor `realloc` (for which D's `pure...` variants are mere wrappers) are specified to initialize allocated memory. `calloc`, however, is - it initializes allocated block with zeroes. Thanks, that's what I was

Re: Is it safe to read to memory after it has been allocated with `pureMalloc` and `pureRealloc`?

2022-04-04 Thread rempas via Digitalmars-d-learn
On Monday, 4 April 2022 at 07:39:08 UTC, Salih Dincer wrote: On Monday, 4 April 2022 at 07:32:00 UTC, rempas wrote: Does anyone knows what's going on here? Source code? Why does it matter? ``` import core.memory; import core.stdc.stdio; import core.stdc.stdlib; extern (C) void main() {

Is it safe to read to memory after it has been allocated with `pureMalloc` and `pureRealloc`?

2022-04-04 Thread rempas via Digitalmars-d-learn
In other terms, do these functions auto-initialize memory to be ready for use? I test it out using `printf` to print a string. "%s" expects for a null terminated string and it seems to work so I suppose that these functions auto-initialize the bytes to `\0`. However, memory is tricky and I

Re: Will it be possible to write a GCC frontend in D?

2022-02-23 Thread rempas via Digitalmars-d-learn
On Wednesday, 23 February 2022 at 21:33:00 UTC, Adam D Ruppe wrote: There is no branch, it is just part of the upstream mainline. see: https://gcc.gnu.org/git/?p=gcc.git;a=tree;f=gcc/d/dmd;h=454baa71a0d270fb891acdda6fd0215a3d6cb588;hb=HEAD Oh, this is what I mean by saying "branch" so my

Re: Will it be possible to write a GCC frontend in D?

2022-02-23 Thread rempas via Digitalmars-d-learn
On Wednesday, 23 February 2022 at 20:19:04 UTC, Adam D Ruppe wrote: I should hope so, otherwise gdc wouldn't exist, yet it does. [extern (C++)](https://github.com/D-Programming-GDC/gdc/tree/master/gcc/d/dmd) and manually creating the decorations. At least that's my understanding of browsing

Re: Will it be possible to write a GCC frontend in D?

2022-02-23 Thread rempas via Digitalmars-d-learn
On Wednesday, 23 February 2022 at 20:06:58 UTC, bachmeier wrote: Not sure if this is the same thing (a link would have helped) but [this is done in C](https://www.cs.usfca.edu/~galles/compilerdesign/C/csupport.html) Thank you! However, one things that I didn't mentioned is that GCC was

Will it be possible to write a GCC frontend in D?

2022-02-23 Thread rempas via Digitalmars-d-learn
I'm using a book called "modern compiler design (version 2)" to learn how to create compiler and I thought about learning and applying this knowledge on writing a GCC frontend just for fun to see where this gets me. However, I've seen some tutorials and I've seen people doing it in C++. Now, I

  1   2   3   >