C - Memcpy not working -
first, know how memcpy work: third parameter number of bytes copied, however, still have problem ...
here's struct:
#define arrondi(x, y) 1 + (x / (y + 1)) #define taille arrondi(max_length, 64) #define max_d 7 typedef unsigned long long ull; typedef struct{ ull list[taille]; ull best_solution[taille]; ull dist[taille]; int borne; int length[max_d-1]; int nb_mark; }tache_t; taille , max_d both macro defined earlier. when i've 2 tache_t, , b, , wanna copy "best_solution" array 1 another, type
#include <string.h> int main(){ int i; tache_t t; t.best_solution[0] = 52461701; t.best_solution[1] = 0; ull t[taille]; memcpy(t, t.best_solution, sizeof(ull) * taille); for(i=0; i<taille; i++) printf("%lu vs %lu\n", t[i], t.best_solution[i]); return 0; } but when checked values, both array bit different ... how possible ??
i wondering if padding problem ... isnt, right ?
aha! if run code through preprocessor (with gcc's -e option), you'll see memcpy line resolved as:
memcpy(t, b.best_solution, sizeof(ull) * 1 + (max_length / (64 + 1))); macros textual replacements. in case, can guard against erroneaous replacement putting whole expression in parentheses:
#define arrondi(x, y) (1 + (x / (y + 1))) if want macro valid in circumstances, place parentheses around arguments, too:
#define arrondi(x, y) (1 + ((x) / ((y) + 1)))
Comments
Post a Comment