dmd and gdc compiling code differently? - d

I am currently trying out DerelictSDL2 (a binding to the SDL2 library for D) and I've written a code that successfully loads a JPG image and displays it in a window. That is, when it's compiled with dmd. When I try with gdc (and no code modification), it does compile but it won't load the image at runtime.
I believe I did things right :
SDL_Init(SDL_INIT_VIDEO)
then
IMG_Init(IMG_INIT_JPG)
and somewhere after that
this.data = IMG_LoadTexture(Window.renderer, name.ptr)
where Window.renderer is (obviously) the SDL_Renderer* and name.ptr is a char* pointing to the name of the image to load. But when compiling with gdc, IMG_Load and IMG_LoadTexture both return null, while with dmd they return a pointer to the newly created texture...
Did I forget something else (after all, with dmd it worked even without IMG_Init) ? Does Derelict only works with dmd (even if it's only interfacing to C functions) ?
dmd : 2.065
gdc : 4.9.1
EDIT :
Turns out the problem is completely different. IMG_LoadTexture takes a pointer to data for its second argument but name.ptr seems to only work with dmd. However If I try with a hard-coded argument like this :
IMG_LoadTexture(renderer, "../test/res/image.jpg")
it works with both dmd and gdc.

there is no guarantee that D string will be 0-terminated. it just happens by chance with dmd. the correct way is to use toStringz() function from std.string module.
p.s. please note that string literals are 0-terminated, that's why hardcoded arguments works.

Related

After recompiling our FORTRAN-code and using it in C++ our system() or c_str() command don`t work properly

Hi I have a big problem: We created a program in C++/Qt 4.8.4 /Qt Creator 2.8.1 years ago that while executing runs another executable (written and compiled in FORTRAN). Everything worked well.
We recompiled our Fortran-Code with the new version of Visual studio and now suddenly it doesn`t work any more. I looked into my C++-Code and found the position where the program crashes:
std::string Executable = ApplicationName.toStdString();
bool RunOK= system((Executable+" > "+"X.out2").c_str());
QString ExeName = (Executable+" > "+"X.out2").c_str();
QString tf = QString::number(qweee);
if(system((Executable+" > "+"X.out2").c_str()))
{
msg.showMessage("msg.showMessage("An XXX error occured during calculation......((Executable+ > +X.out2).c_str(): "+ExeName +"......(system((Executable+ > +X.out2).c_str()): "+ QString::number(RunOK));
if(QFile(OutputFiles[0]).exists())
QFile(OutputFiles[0]).remove();
}
Somehow system((Executable+" > "+"X.out2").c_str()) gets to be true which didn`t happen before.
This seems to happen either in the c_str-command or in the system()-command.
We had some missing dll-issues before. Is this another dll-problem and if so which?
Can anybody help us on this?
Thank you
The return value of system is an integer, not a boolean. Its value is only defined for one very special case, system(nullptr). That's not the case here. So whether you get a zero or non-zero result depends on your particular C++ implementation, which has indeed changed. ("New visual studio version"). You can't rely on non-zero means error
c_str() is not a suspect at all.

Migrating flex 2.5.4a to 2.6 (lexical analyser generator)

I have a file that generates cc code using flex. When I use the version 2.5.4a-10 the codes works as expected.
If I use bit more recent version 2.5.37 or even newer like 2.6 the generated code seems not to allocate anything. It uses some pointers defined with nullptr and crashes.
I think the syntax has changed in between these versions. I find it also strange that Debian/Ubuntu have a package called flex-old saying:
flex is a tool for generating scanners: programs which recognize lexical
patterns in text. This is the old 2.5.4a version, which is no longer
being developed. You should normally choose flex, unless you have
legacy lexer files that do not work with a modern flex.
This product includes software developed by the University of California,
Berkeley and its contributors. The upstream source code can be found at
http://flex.sourceforge.net/
(Editor's note: Flex has moved to Github but v2.5.4a is not there.)
That version seems to be a big deal for others I suspect. Getting to my question:
Is there any manual or guide of what I have to do in order to port that code to generate some c++ code that works in more recent versions of flex?
EDIT: Here is my simple example taken from something larger:
int num_lines = 0, num_chars = 0;
%%
\n ++num_lines; ++num_chars;
. ++num_chars;
%%
int main()
{
yy_init=1;
yylex();
printf( "# of lines = %d, # of chars = %d\n",
num_lines, num_chars );
return 0;
}
flex it with flex file.l and build it with gcc lex.yy.c -lfl. Now, if you used version 2.5.4 it will work. With later versions it translates and compiles just fine, but when you run the program you will get segmentation fault.
I found the problem myself. The variable yy_init can be explicitly set in that old version. In newer versions it is not allowed. I'm not sure if that is intended, maybe someone can explain why this behavior is observed. I find it a bit strange.
If someone has a similar problem, you might want to take a look at the yy_init variable. Other than that I had no issues.

Unique character code not working when ran in linux but fine in windows (C++)

When I run the code in windows, it works as expected. But when I run it in linux, the program doesn't run as expected and produces a completely different output.
I'm assuming its a problem in the all_unique_letters method, but I can't seem to figure it out.
In bool all_unique_letters(const string &s), unsigned int v; is used without being initialized. If it contains something else that 0, the function reports a letter as a duplicate when it is not.

Code completion for CodeBlocks c++

So i wanted to ask how to get "proper" Code Completion working for CodeBlocks 13.12 for c++, im running mint rosa at the moment.
By this i mean
string buf;
buf. <--This doesnt work - no suggestions
I would like the "." to show me relevant function/attribute names. It autocompletes variables and shows relevant type information, but nothing of the type mentioned above that originates from a library.
For example I see that this works for some cases
MyClass obj;
obj. <--This works

Running the executable of hdl_simple_viewer.cpp from Point Cloud Library

The Point Cloud library comes with an executable pcl_hdl_viewer_simple that I can run (./pcl_hdl_viewer_simple) without any extra arguments to get live data from a Velodyne LIDAR HDL32.
The source code for this program is supposed to be hdl_viewer_simple.cpp. A simplified version of the code is given on this page which cannot be compiled readily and requires a tiny bit of tweaking to make it compile.
My problem is that the executable that I build myself for both the versions are not able to run. I always get the smart pointer error "Assertion px!=0" error. I am not sure if I am not executing the program in the correct way or what. The executable is supposed to be executed like
./hdl_viewer_simple -calibrationFile hdl32calib.xml -pcapFile file.pcap
in case of playing from previously recorded PCAP files or just ./hdl_viewer_simple if wanting to get live data from the real sensor. However, I always get the assertion failed error.
Has anyone been able to run the executables? I do not want to use the ROS drivers
"Assertion px!=0" is occurring because your pointer is not initialized.
Now that being said, you could initialize it inside your routines, in case the pointer is NULL, especially for data input.
in here, you can try updating the line 83 like this :
CloudConstPtr cloud(new Cloud); //initializing your pointer
and hopefully, it will work.
Cheers,