I'm going to be straight here, I'm an absolute novice when it comes to C and I'm a bit out of my depth here and need a little help. I am tweaking some source code and need to reset some static globals so that they can be used again. I've tried all sorts of things which just end in bad access errors - any help would be appreciated.
static struct option long_options[2 * countof (option_data) + 1];
static char short_options[128];
static unsigned char optmap[96];
Here's what I've tried:
memset(&long_options[0], 0, 2 * countof (option_data) + 1);
memset(&short_options[0], 0, sizeof(short_options));
memset(long_options, 0, sizeof(long_options));
memset(short_options, 0, sizeof(short_options));
memset(optmap, 0, sizeof(optmap));
Related
Documentation I find online is vague. The only forum I found that's good was https://gbatemp.net/threads/ds-programming-for-newbies.322106/page-8#post-4445495. I followed the code on screen, I made a sprite (16 x 16, 256 color palette) and wrote the code. The sprite doesn't seem to display, all I get is the top screen to be black and the bottom screen to be white on my physical DSi. Desmume throws a "The ROM header is invalid. The device size has increased for the provided file size". Am I missing something? I can't really find what I'm missing since I'm trying to code for a 15 year old console, so if anyone knows here what I'm doing wrong, please, please tell me. The file names under the nitrofiles folder are "stickman.img" and "stickman.pal". Here's my code:
#include <gl2d.h>
#include <nds.h>
#include <nf_lib.h>
#include <stdio.h>
int main(int argc, char **argv){
NF_Set2D(0, 0);
NF_SetRootFolder("NITROFS");
NF_InitSpriteBuffers();
NF_InitSpriteSys(0);
NF_LoadSpriteGfx("stickman", 0, 16, 16);
NF_LoadSpritePal("stickman", 0);
NF_VramSpriteGfx(0, 0, 0, false);
NF_VramSpritePal(0, 0, 0);
while(1) {
NF_SpriteOamSet(0);
swiWaitForVBlank();
oamUpdate(&oamMain);
}
return 0;
}
You didn't create the sprite.
Use NF_CreateSprite(), for example:
NF_CreateSprite(0, 0, 0, 0, 8, 16);
After creating the sprite, update oam.
I have the following code. The code reads a string from a PHP webpage, then converts the code using the MultiByteToWideChar-function. Then it splits the string using the comma-delimiter. It then removes the "xxx=" before the value (for example, "cid={abcd-1234-5678},tid=AAD23HKJD23KVAAAHN23") and attaches the splitted substrings to an array. Then it displays the parameters one by one in a textbox.
I had it working, however I reinstalled my system 2 days ago and since then I cannot seem to get it working again (even though I backed up the working project before reinstalling, which leaves me wondering how this could ever be possible).
So I have been trying for hours and hours and hours the past day, changing every setting in the compiler and project properties I could think of, reinstalling Visual Studio, trying other versions of Visual Studio, installing extra SDK packages... Nothing helped. Then I added some junk code to test whether the wcstok function was even working (it seems that the project died there) and then randomly the entire project worked and the array I talked about before got properly returned. However, if I remove this junkcode or the code after the splitting/returning of the string (which should have no influence on the code above it, leaving me as confused as I could possibly be) it stops working again and seems to die at the "wcstok"-function.
This is my code:
BOOL Commands(LPBYTE command, DWORD size)
{
unsigned int sizeint;
sizeint = (unsigned int)size;
wchar_t params[MAXCHAR];
MultiByteToWideChar(CP_ACP, MB_COMPOSITE, (LPCCH)command, sizeint, (LPWSTR)params, size);
MessageBoxW(0, params, 0, 0);
//wchar_t input[100] = L"A bird came down the walk";
//wchar_t* buffer;
//wchar_t* token = std::wcstok(input, L" ", &buffer);
wchar_t buf2[MAXCHAR], *ptr;
int i;
for (ptr = wcstok(params, L","); ptr != NULL; ptr = wcstok(NULL, L","))
{
CWA(lstrcpyW, kernel32, buf2, ptr);
for (i = 0; i < lstrlenW(buf2); i++)
{
if (buf2[i] == '=' )
{
wchar_t *a[1000];
wcscpy(a[0] + i, buf2 + i + 1);
MessageBoxW(0, a[0] + i, 0, 0);
}
else
{
}
}
}
//HRESULT hr;
//LPCTSTR Url = _T("http://cplusplus.com/img/cpp-logo.png"), File = _T("C:\\Users\\Public\\file.exe");
//////hr = URLDownloadToFile(0, Url, File, 0, 0);
//switch (hr)
//{
// PROCESS_INFORMATION *piinfo; //size = 0x10
// STARTUPINFO *siinfo; //size = 0x44
// CWA(CreateProcessW, kernel32, 0, File, 0, 0, 0, DETACHED_PROCESS, 0, 0, siinfo, piinfo);
//}
CWA(Sleep, kernel32, 100);
return 1;
}
It doesn't work properly when I use the code above, but when I uncomment the junk-code that is commented above, it randomly does work. The first "MessageBoxW" seems to always return the string from the PHP page properly, however once I remove the junkcode, the program gets terminated once the wcstok in the splitting function is reached.
It worked before exactly like this and I am honestly clueless, I hope anyone has an idea what could cause this because it is honestly driving me nuts..
Thanks a lot in advance!
Millie Smith was the one who gave me the place where the code was bugged.
Instead of defining "a" as a wchar_t*, I had to define it as a normal w_char.
So it became "wchar_t a[MAXCHAR]". Then I had to change the "wcscpy(a[0] + i, buf2 + i + 1);" inside the splitting function into "wcscpy(a + i, buf2 + i + 1);".
Learned something new again, undefined behaviour can be a tricky thing.
Thanks for the help and have a nice day!
I have the following native code that copies from a file into a buffer and then copies the
contents of that buffer into a jbytearray.
JNIEXPORT void JNICALL Java_com_test(JNIEnv * env, jobject){
int file_descriptor = 100;
JNIEnv * jni_env = env;
FILE* file = fdopen(file_descriptor, "r");
unsigned char* buffer;
int size_of_file = 1000000;
fread(buffer, 1, static_cast<size_t>(size_of_file), file);
imageArr = static_cast<jbyteArray>(jni_env->NewByteArray(static_cast<jsize> (size_of_file)));
jni_env->SetByteArrayRegion (imageArr, 0, static_cast<jsize>
(size_of_file ), (jbyte*)buffer);
}
As this code runs in a loop, I would like to optimize this as much as possible. Is there any way to directly read from the file to the jbyteArray? I am aware jbyteArray is a pointer to a struct. Is there any way to set the fields of this struct directly instead of using the setByteArrayRegion() function?
If not, is there any other function that I can use to read from a file to a jbytearray?
In short, no. You can probably do it, but it probably wont be much faster and if something with the implementation changed in the JVM your code would stop working. You are dealing with file I/O so I don't think SetByteArrayRegion is your real bottleneck here.
I am writing an improved Perlin noise (I don't really understand simplex noise) terrain generator for C, and I am practically finished with the alpha build. However, there is one thing holding me back: actually saving the stupid image. I recruited MagickWand to help me solve the problem of PNG creation, and it looks like a nice implementation on the whole, with tons of useful features etc., but there is very little documentation on the whole thing. No tutorials, really, just a bunch of lists of functions and some example programs. Here is my code so far, based on this:
EDIT: Cut out a bunch of irrelevant code.
#include <stdio.h>
#include <stdlib.h>
#include "mt.h"
#include "diamondsquare.h"
#include "/Library/Frameworks/libWand.framework/Versions/6.3.0/Headers/wand/MagickWand.h"
int main () {
unsigned long seed = 0, x = 0, y = 0, initial = 0, range = 0;
int smooth = 0, fail = 1, index1 = 0, index2 = 0, exception = 0;
char flagchar1 = 'n';
// Some imperative code. Not relevant.
image *ConstituteImage(x, y, "I", IntegerPixel, grid, &exception);
write("image.png", image);
}
At the very least, I know that this is linked wrong (compiling returns an error inside wand.h that it can't find one of the headers). What's the proper way to go about creating an image from an array within a program using MagickWand for C?
Too much code, it could be summarized with:
image *ConstituteImage(x, y, "I", IntegerPixel, grid, &exception);
write("image.png", image);
But reading the MagickWand link you provided:
MagickWriteImageFile
MagickWriteImageFile() writes an image to an open file descriptor. The
format of the MagickWriteImageFile method is:
MagickBooleanType MagickWriteImageFile ( MagickWand *wand, FILE *file
); A description of each parameter follows:
wand: The magick wand. file: The file descriptor.
So it is clear you have to call:
MagickBooleanType MagickWriteImageFile ( MagickWand *wand, FILE *file );
that header almost definitely tries to include other headers so you need something like:
gcc -I"/Library/Frameworks/libWand.framework/Versions/6.3.0/Headers"
or
gcc -I"/Library/Frameworks/libWand.framework/Versions/6.3.0/Headers/wand"
I have some C code to populate an OCIDate from the epoch time:
In my main program:
OCIDate ocidate;
epoch_to_ocidate(c.f, &ocidate);
And in a library:
void epoch_to_ocidate(double d, OCIDate* ocidate) {
time_t t = (time_t)d;
struct tm *ut = localtime(&t); /* convert to a Unix time */
OCIDateSetDate(ocidate, ut->tm_year + 1900, ut->tm_mon + 1, ut->tm_mday);
OCIDateSetTime(ocidate, ut->tm_hour + 1, ut->tm_min + 1, ut->tm_sec + 1);
}
I am pretty certain this is correct, because I have a check in the calling routine:
#ifdef DEBUG
char* fmt = "DD-MON-YYYY HH24:MI:SS";
ub4 dbufsize=255;
debug("testing converted OCIDate:");
OCIDateToText(h.err, (const OCIDate*)&ocidate, (text*)fmt, (ub1)strlen(fmt), (text*)0, (ub4)0, &dbufsize, (text*)dbuf);
debug(dbuf);
#endif
And I am binding it with:
OCIBindByPos(s, &bh, h.err, (ub4)p, (dvoid*)&ocidate, (sb4)sizeof(ocidate), SQLT_ODT, 0, 0, 0, 0, 0, OCI_DEFAULT);
(dbuf is already defined). And that displays exactly what I would expect. But when it arrives in Oracle it's gibberish, resulting either in a nonsensical date (e.g. 65-JULY-7896 52:69:0 or a either ORA-1858 or ORA-1801). Has anyone seen anything like this before? Thanks!
Solved it - the problem was that ocidate was stack allocated, and binding doesn't copy the value into the bindhandle, it merely sets a pointer, so when it went out of scope, that could have been pointing to anything. So I heap-allocated it instead. Now of course I have to bookkeep it, but I guess that's straightforward enough. Cheers!