zookeeper-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Niko Vuokko <niko.vuo...@gmail.com>
Subject Re: Storing 2d array in z node
Date Thu, 14 Aug 2014 14:15:19 GMT
Hiya,

There are multiple layers where you are invoking undefined behavior. This
has nothing to do with zookeeper, just with C pointers:

1) int** string "contains" 144 ints, that is, 144*4 = 576 bytes on 32-bit
(sort of). However the actual ints may lie wherever the compiler can point
an address to. Only the first 12*4 = 48 contiguous bytes from string[0]
contain something you've defined, the rest is rubbish.
2) You write 144 bytes to /pradesh. This contains 12*4 = 48 bytes of int*
pointers + 96 bytes of rubbish.
3) You receive 144 bytes from /pradesh. These 144 bytes are interpreted as
int pointers, meaning *int_value contains 36 valid int pointers. However
what lies at the end of these pointers is undefined.
4) You read 4*144 = 576 bytes from int_value and print them out. By sheer
luck (and I don't exactly understand why even so), the first 36 numbers
show up normal since the system never bothered to write them over with
something else. Additionally, this works only because you're using ints,
with some other type this would never work as far I see. After the first 36
numbers you once again start writing rubbish off the end.

Problem 1: you never serialize your 2D array. Higher-dim arrays are not
contiguous, they are, as their type states, pointers to an array of
pointers. Therefore your code never writes the actual ints to zookeeper,
just some arbitrary unusable pointers + rubbish.
Problem 2: you mix up * pointers with ** pointers. This generally spells
disaster, but seems to kind of work for you because your system's memory
addresses have equal size with its ints.


BR,
niko




2014-08-14 15:06 GMT+03:00 praveshjain1991 <praveshjain1991@gmail.com>:

>  am trying to store a 2d integer array in zookeeper znode, but upon
> retrieving it, the expected data is not returned.
>
> Here is the code:
>
> #include <string.h>
> #include <errno.h>
> #include <stdlib.h>
> #include <zookeeper.h>
> #include <pthread.h>
>
> static zhandle_t *zh;
> static int batchMode=0;
> static int shutdownThisThing=0;
>
> pthread_mutex_t count_mutex;
> pthread_cond_t count_threshold_cv;
>
> struct timeval startTime;
> struct Stat stat;
>
> char* buffer;
> int* buff_len;
>
> int rc = 0;
>
> int size = 12;
>
> char *foo_get_cert_once(char* id) { return 0; }
>
> void my_data_completion(int rc, const char *value, int value_len,
>     const struct Stat *stat, const void *data) {
>
>     int *int_value = (int *) value;     //data received from the z-node
>     int *int_data = (int *) data;       //data received from the caller
>     int i=0,j=0;
>     for(i=0;i<size*size;i++)
>         fprintf(stderr, "%d ",*(int_value+i) );
>     fprintf(stderr,"\n");
>     for(i=0;i<size*size;i++)
>         fprintf(stderr, "%d ",*(int_data+i) );
> }
>
> void watcher(zhandle_t *zzh, int type, int state, const char *path, void
> *watcherCtx) {
>
>     fprintf(stderr, "Inside watcher function. Here type is %d and state is
> %d\n", type, state);
>     zoo_exists(zh, "/pravesh", 1, &stat);
> }
>
> /**
> Function called in a separate thread.
> Ignite wait here.
> **/
> void *waiting(void *arg){
>
>     fprintf(stderr, "Inside waiting function\n");
>     pthread_mutex_lock(&count_mutex);
>     pthread_cond_wait(&count_threshold_cv, &count_mutex);
>     pthread_mutex_unlock(&count_mutex);
>     pthread_exit(NULL);
> }
>
> int main() {
>
>     char buffer[512];
>     char p[2048];
>     char *cert=0;
>     char appId[64];
>
>     int string[size][size];
>     int i=0,j=0,k=0;
>
>     for(i=0;i<size;i++)
>         for(j=0;j<size;j++)
>             string[i][j]=k++;
>
>     for(i=0;i<size;i++)
>         for(j=0;j<size;j++)
>             fprintf(stderr, "%d ", string[i][j]);
>     fprintf(stderr, "\n");
>
>     int end = 0;
>     int rc = 0;
>
>     zoo_set_debug_level(ZOO_LOG_LEVEL_INFO);
>
>     zh = zookeeper_init("localhost:2181", watcher, 10000, 0, 0, 0);
>     if (!zh) {
>         return errno;
>     }
>
>     int buflen= sizeof(buffer);
>
>     zoo_set(zh, "/pravesh", (const char*)string, size*size, -1);
> //sets the data in the z-node
>     zoo_aget(zh, "/pravesh", 1, my_data_completion, (const char*)string );
> //gets the data from the z-node and passes it to my_data_completion
>
> /**
>  * Trying to implement waiting functionality using threads
>  **/
>
>     fprintf(stderr, "Starting threads\n");
>     pthread_t thread;
>     pthread_mutex_init(&count_mutex, NULL);
>     pthread_cond_init (&count_threshold_cv, NULL);
>     pthread_create(&thread, NULL, &waiting, NULL);
>
>     fprintf(stderr, "Waiting function has been called\n");
>
>     pthread_mutex_destroy(&count_mutex);
>     pthread_cond_destroy(&count_threshold_cv);
>     pthread_exit(NULL);
> }
>
> The output after printing data is correct(numbers 0-143) but the output
> after printing value is unexpected (shown below):
>
>     0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
> 27 28 29 30 31 32 33 34 35 0 0 1946159936 32538 176 0 37 0 1946159744 32538
> 21 25 0 0 132017 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> 0
> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>
> Is there something wrong with the implementation?
>
>
>
> --
> View this message in context:
> http://zookeeper-user.578899.n2.nabble.com/Storing-2d-array-in-z-node-tp7580158.html
> Sent from the zookeeper-user mailing list archive at Nabble.com.
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message