Hey guys. Trying to implement associative array with the help of libJudy. Here's small test, that doesn't work. Please, help me to find, where i did wrong?

#include <assert.h>
#include <Judy.h>

typedef struct {
	char *hash;
	int id;
	int views;
} Watch_t;

Pvoid_t dict = (Pvoid_t) NULL;
PWord_t rcw;

char idx[12];

void init_track(Watch_t *track) {
	assert(track);
	track->id = 0; track->views = 0;
}

int set_to_storage(const unsigned char *key, Watch_t *val) {
	PWord_t PV;

	JSLI(PV, dict, key);
	if (PV) {
		PV = (PWord_t) val;
		return 0;
	} else {
		return 1;
	}
}

Watch_t *get_from_storage(const unsigned char *key) {
	PWord_t PV;

	JSLG(PV, dict, key);

	if (PV) {
        	return ((Watch_t *) PV);
	} else {
		return NULL;
	}
}

int main(int argc, char **argv) {
	char *hash = "t8xr5nk8jczy";
	Watch_t *tr; Watch_t *track;
	int bool;

	track = (Watch_t *) malloc( sizeof(Watch_t) );
	init_track(track);

	track->hash = "t8xr5nk8jczy";
	track->id = 1; track->views = 16;

	bool = set_to_storage((unsigned char *) hash, track);

	tr = get_from_storage((unsigned char *) hash);
	assert(tr);

	fprintf(stderr, "Debug: hash=%s, track views=%d\n", tr->hash, tr->views);

    return 0;
}

Working under ubuntu 9.04 and compiler flags is:

gcc -Wall -lJudy test.c -o test

trying to run it with gdb returned no errors.
don't mention unused args. i didn't put here other voids, that are not important at the moment.

Oh, yeah. forgot to say, the wrong is that i couldn't get my data back from dict.

who or what is libJudy? (don't tell me its a library because I already guessed that :) )

commented: libJudy implements a "Judge" method ;) +31

why don't you just ask their support group? Its unlikely anyone here will know anything about that library, but then I could be wrong about that.

Because, i think, there is not the problem in libJudy. There's something with pointers or so.

Did you try using track without pointers?

Watch_t *tr; Watch_t track;
	int bool;

	init_track( &track );

	track.hash = "t8xr5nk8jczy";
	track.id = 1; track.views = 16;
// etc etc

Yes, but it doesn't change the result...

In that case its not a pointer problem because you removed all pointers. So I have no clue what's wrong.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.