-1

Trying to copy A into B....

char *A;
double *B;
unsigned int size = 1024;

A = malloc (size*size * sizeof (char));
B = malloc (size*size * sizeof (double));
//fill A here

memcpy (B, &A[0], (size*size * sizeof (char)));

Printing values in B don't match whats in A.

What's going wrong?

Thanks for any help!

Edit: The point of this is to test the memcpy function's speed in relation to the size of the L2 cache. I'm just wanting to make sure the code above is actually copying all of A into B. Sorry about leaving out this info: I just try to make it as simple as possible (and went too far this time)

RHok
  • 152
  • 1
  • 2
  • 11
  • Are you trying to convert `char`s to `double`s? – japreiss Oct 09 '12 at 23:15
  • 5
    Nothing is going wrong. The only thing wrong may be your expectations. – Kerrek SB Oct 09 '12 at 23:18
  • 1
    What is it that you're putting in A, what are you expecting to see in B, and what how are you viewing it? Also, notice that you are not filling B unless sizeof(char) == sizeof(double), which is unlikely. – Variable Length Coder Oct 09 '12 at 23:18
  • 2
    Why are you trying to do such a ridiculous thing? – Jim Balter Oct 09 '12 at 23:19
  • 1
    What is it that you want to achieve? – Daniel Fischer Oct 09 '12 at 23:21
  • You're only copying 1/8 of the memory location B and moving it into A. – Suroot Oct 09 '12 at 23:22
  • It's a small part to get a real figure for L2 cache speed transfer. Thank you Kerrek, that's one of things I was afraid of haha. Variable Length coder: Why is it not filling? How do I make it fill? – RHok Oct 09 '12 at 23:23
  • This seems kinda dumb, but I'll play along. If you have: int char_size = some_int; then: int double_size = (char_size * sizeof(char))/ sizeof(double); Although, `sizeof(char)` is always `1`, as far as I know. – Geoff Montee Oct 09 '12 at 23:26

1 Answers1

1

It's hard to tell exactly what you are trying to do.

How are you printing values? Print routines like printf also depend on the type.

It sounds like you just want to get float input values. This can be done using the scanf family.

int num_floats = 10;

double* B = malloc (num_floats * sizeof (double));

int count;

for (count = 0; count < num_floats; count++)
{
    printf("Insert float %d: ", count);
    scanf("%f", &B[num_floats]);
}

for (count = 0; count < num_floats; count++)
{
    printf("Float %d: %f", B[num_floats]);
}

free(B);

If you are trying to convert C-strings from char * to floating point numbers and don't want to use sscanf, you can also use atof.

const char* num_str = "1.01";
double num = atof(num_str);
Geoff Montee
  • 2,587
  • 13
  • 14