Re: extern (C) function call with const char * type will sometimes generate seg fault or core dump.

2014-10-16 Thread dysmondad via Digitalmars-d-learn

On Thursday, 16 October 2014 at 07:55:24 UTC, Mike Parker wrote:

On 10/16/2014 4:54 PM, Mike Parker wrote:

On 10/16/2014 12:18 PM, dysmondad wrote:
Since I've added this call, my program will sometimes but not 
always
either generate a core dump or a seg fault. It seems that the 
issue is

with the const char * parameter.

I don't have a good grasp of the difference between the way D 
and C work

for char * types.


Strings in C are arrays of chars ending with the nul 
terminator. Strings
in D are not required to be nul terminated. String literals in 
D *are*
nul terminated. When passing a string to a C function that 
takes const

char*, use toStringz as Ali showed in his post.


Forgot to mention -- toStringz in std.string will add the nul 
terminator if it is not already there.



---
This email is free from viruses and malware because avast! 
Antivirus protection is active.

http://www.avast.com



Thank you very much. Because of your help, my application has far 
fewer seg faults and core dumps.


Now, if I can just figure out why the SDL_RenderPresent call is 
having problems. I suspect that has more to do with the way I'm 
using the library than the calling convention.


extern (C) function call with const char * type will sometimes generate seg fault or core dump.

2014-10-15 Thread dysmondad via Digitalmars-d-learn
Since I've added this call, my program will sometimes but not 
always either generate a core dump or a seg fault. It seems that 
the issue is with the const char * parameter.


I don't have a good grasp of the difference between the way D and 
C work for char * types. The call to loadTexture uses a literal 
for the file name, i.e. resources/ball.png.


// d lang bindings for C function
alias void SDL_Renderer;
alias void SDL_Texture;
extern (C) SDL_Texture * IMG_LoadTexture(SDL_Renderer * renderer, 
const char * file);


// d lang call to extern (C) function
SDL_Texture* loadTexture( SDL_Renderer * ren,  const char * file )
{
SDL_Texture * loadedImage = IMG_LoadTexture( ren, file );
return loadedImage;
}


Re: I don't get it. version(unittest) can't seem to use local variable

2014-07-12 Thread dysmondad via Digitalmars-d-learn

.


try:

unittest {
  Velocity v = new Velocity( 2.0f, 5.0f );
  v *= 5.0f;  // - line 110
  printf( v = %s\n, to!string(v) );
}

instead. Basically version is like static if, it doesn't 
indicate its a function. Instead it changes what code gets 
compiled.
Where as a unittest block, is essentially just a function that 
gets called at a special time, that is only compiled as if 
version(unittest) was also used.


Thank you very much. That was quite helpful.

So, what is the generally accepted way include unit testing? TDD 
is all the rage these days and I though I would try my hand at it 
as well as D.


Re: I don't get it. version(unittest) can't seem to use local variable

2014-07-12 Thread dysmondad via Digitalmars-d-learn

...

https://github.com/rikkimax/skeleton/blob/master/source/skeleton/syntax/download_mkdir.d#L175
Of course there will be better ones, just something I was doing 
last night however.


Thanks for the link and the information.
That's looks like what I need so I'm going to rip off your code 
example. :)


Re: DStyle: Braces on same line

2014-07-12 Thread dysmondad via Digitalmars-d-learn

On Saturday, 12 July 2014 at 19:01:56 UTC, Danyal Zia wrote:

Hi,

I noticed that in Andrei's talks and his book, he used braces 
on the same line of delcaration, however Phobos and other D 
libraries I know use braces on their own line. Now I'm in a 
position where I need to take decision on coding style of my 
library and I get accustomed to use braces on same line but I'm 
worried if that would make my library less readable to other D 
users.


Should I worry about it? Or is that's just a debatable style 
that won't really matter if it's persistent throughout library?


Thanks


Pick one and stick with it. There are countless holy wars out 
there about this sort of thing and it is really a personal choice.


Generally it's a good idea to follow the style of extant code if 
you are working on large project and I do feel that coding teams 
should pick a style all the code from that group should use the 
same style.


I don't get it. version(unittest) can't seem to use local variable

2014-07-11 Thread dysmondad via Digitalmars-d-learn
I'm new to D. I've been using C since it was a baby and C++ back 
when it was only a pre-compiler for c. So, I may just be stuck 
thinking in C land.


The issue is I am attempting to use the version(unittest) 
feature. However the compiler pukes up the error below. I'm sure 
it's something easy enough but I've not been able to find any 
help in Google land. I don't know if I have the opOpAssign 
defined correctly or if there's something wrong with the 
constructor or both.


dysmondad@Julep:~/src/D$ gdc -c velocity.d
velocity.d:110: error: no identifier for declarator v
velocity.d:110: error: semicolon expected, not '*='
velocity.d:110: error: Declaration expected, not '*='


class Velocity
{
private:

float _Y = 0.0;
float _X = 0.0;

public:
this (float x, float y )
{
_Y = y;
_X = x;
}

ref Velocity opOpAssign(string op) ( in float multiplier )
if( op == * )
{
_Y *= multiplier;
_X *= multiplier;
return this;
}
}

version(unittest)
{
Velocity v = new Velocity( 2.0f, 5.0f );
v *= 5.0f;  // - line 110
printf( v = %s\n, to!string(v) );
}