-1

First, know how memcpy work: the third parameter is the number of bytes copied, however, i still have a problem ...

Here's my 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 and Max_D are both Macro defined earlier. When i've 2 tache_t, a and b, and i wanna copy the "best_solution" array from one to another, so i 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 i checked values, both array were a bit different ... How is it possible ??

I was wondering if it was a padding problem ... but obviously it isnt, right ?

Viridya
  • 640
  • 5
  • 13
  • `both array are a bit different`...clarify `a bit` – Sourav Ghosh May 11 '15 at 11:29
  • 2
    And please show the definition of the `Taille` macro. – M Oehm May 11 '15 at 11:31
  • 2
    If you are wondering something, verify it instead of assuming it. Eventually you will wonder less, and eliminate the chance that something is taken on faith is wrong. – Edwin Buck May 11 '15 at 11:32
  • The first ull is correct (both are equal), however, the second is different. I've 0 in src (which is what i want) and 4204032 in the dest o.o – Viridya May 11 '15 at 11:33
  • 2
    Try provide a SMALL but COMPLETE (in the sense that someone else can compile/link/run it) sample of code that exhibits the problem. You're leaving things out, and the code you are showing would not exhibit the problem. This means some code you have chosen to omit is the culprit. – Peter May 11 '15 at 11:36
  • 1
    Please be a bit more specific: Is the copy completely different from the source or does it differ from a certain offset on? I still suspect that your `Taille` macro is not resolved as you think it is. Please show its `#define`. – M Oehm May 11 '15 at 11:37
  • For constant `Max_Length` equal `64000` your macro `ARRONDI(Max_Length, 64)` calculates a result of `985`. Is that what you intended? If you tried to calculate the length of array of Y-bits-long items to keep X bits then your formula `1 + (X / (Y + 1))` is wrong–it should be `((X+Y-1)/Y)` instead. You should NOT divide by `(Y+1)` if you want to divide by `Y`. – CiaPan May 11 '15 at 12:10

2 Answers2

3

Aha! If you run your code through the preprocessor only (with gcc's -E option), you'll see that the memcpy line is resolved as:

memcpy(T, b.best_solution, sizeof(ull) * 1 + (Max_Length / (64 + 1)));

Macros are textual replacements. In your case, you can guard against erroneaous replacement by putting the whole expression in parentheses:

#define ARRONDI(X, Y) (1 + (X / (Y + 1)))

If you want your macro to be valid in all circumstances, place parentheses around the arguments, too:

#define ARRONDI(X, Y) (1 + ((X) / ((Y) + 1)))
M Oehm
  • 28,726
  • 3
  • 31
  • 42
2

This part of source code:

sizeof(ull) * Taille

becomes:

sizeof(ull) * ARRONDI(Max_Length, 64)

after the Taille macro expansion, which in turn becomes:

sizeof(ull) * 1 + (Max_Length / (64 + 1))

after substitution of ARRONDI.
Is that what you wanted...?

CiaPan
  • 9,381
  • 2
  • 21
  • 35
  • Right, you find a mistake ! Thanks for that :) You're such a god ;) I just add braces around the definition of "ARRONDI" and its ok .. thanks you so much, 2 days lost for that :'( – Viridya May 11 '15 at 11:44