简体   繁体   中英

All elements of array overwritten instead of only one?

I've got some C code running inside my pebble watch. It is receiving some data, each time as a key-value pair. It is receiving 5 pieces of data, each with the correct key and value, as follows:

Key: 5 Value: '0'
Key: 6 Value: '10'
Key: 7 Value: '20'
Key: 8 Value: '30'
Key: 9 Value: '40'

The pebble receives one of these pairs at a time, and each time it does the following function gets called (previous line: SimpleMenuItem chats[5]; )

void in_received_handler(DictionaryIterator *received, void *context) {
    dataReceived = dict_read_first(received);
    APP_LOG(APP_LOG_LEVEL_DEBUG, "read first");
    while (dataReceived != NULL){

        APP_LOG(APP_LOG_LEVEL_DEBUG, dataReceived->value->cstring);
        char keystr[10];
        snprintf(keystr, 10, "Key: %d", (int)dataReceived->key);
        APP_LOG(APP_LOG_LEVEL_DEBUG, keystr);

        snprintf(keystr, 10, "Index: %d", (int)dataReceived->key -5);
        APP_LOG(APP_LOG_LEVEL_DEBUG, keystr);
        chats[dataReceived->key - 5] = (SimpleMenuItem){
                    // You should give each menu item a title and callback
                    .title = dataReceived->value->cstring,
                    .callback = selected_chat,
                };

        dataReceived = dict_read_next(received);
        APP_LOG(APP_LOG_LEVEL_DEBUG, "read again");
    }

    layer_mark_dirty((Layer *)instant_chats);
}

Which then outputs the following to the pebble logs (this is what I believe to be correct):

[DEBUG] sr.c:195: read first
[DEBUG] sr.c:197: 0
[DEBUG] sr.c:200: Key: 5
[DEBUG] sr.c:219: Index: 0
[DEBUG] sr.c:243: read again
[DEBUG] sr.c:195: read first
[DEBUG] sr.c:197: 10
[DEBUG] sr.c:200: Key: 6
[DEBUG] sr.c:219: Index: 1
[DEBUG] sr.c:243: read again
[DEBUG] sr.c:195: read first
[DEBUG] sr.c:197: 20
[DEBUG] sr.c:200: Key: 7
[DEBUG] sr.c:219: Index: 2
[DEBUG] sr.c:243: read again
[DEBUG] sr.c:195: read first
[DEBUG] sr.c:197: 30
[DEBUG] sr.c:200: Key: 8
[DEBUG] sr.c:219: Index: 3
[DEBUG] sr.c:243: read again
[DEBUG] sr.c:195: read first
[DEBUG] sr.c:197: 40
[DEBUG] sr.c:200: Key: 9
[DEBUG] sr.c:219: Index: 4
[DEBUG] sr.c:243: read again

So, while everything appears to be correct (to me), there is unexpected behavior. Instead of having chats be an array of SimpleMenuItem with different values for each of the elements, the same piece of data (that is, the newest) overrides all of the values, even though it (probably) should only be writing over a specified element. Thus, at the end of the 5 pieces of data being sent, the entire chats array ends up being filled with SimpleMenuItem of value 40 . I feel as though this is more of a C problem than a pebble problem specifically - but if anyone could address this I would much appreciate it.

Thanks!

As answered in the comments, you need to duplicate the strings, otherwise they all point to the same address in memory and are being overwritten.

See my reply to a related question: Converting char array to string [and Pebble] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM