0

I want to copy an array into a second array at the position index.

What I did is:

uint8_t* _data = (uint8_t *)malloc(8U*1024);
uint32_t index= 4U;
uint8_t name[] ="TEST";
memcpy(&data[index], name, sizeof(uint32_t));
index+= 4U;

When I print data using:

    for (int j =0; j<index; j++)
    {
        printf("%c \n",data[j]);
    }

it is empty. I want to find at data[3] "TEST"

5
  • The first 4 elements of data (or _data? Please edit and clarify) contain undetermined values, so what output do you expect? Commented Oct 22, 2021 at 11:50
  • Also using sizeof(uint32_t) doesn't make much sense here, you should rather use sizeof(name). Commented Oct 22, 2021 at 11:51
  • Please fix a minimal reproducible example Commented Oct 22, 2021 at 11:52
  • for (int j =0; j<offset; j++) -> for (int j = offset; j< offset + 4; j++) Commented Oct 22, 2021 at 11:53
  • 2
    If you want to find what you copied, why are you reading from a different place? Commented Oct 22, 2021 at 11:56

3 Answers 3

2

You need to read from the same place you were writing to.

You want this:

  uint8_t* data = malloc(8U * 1024);    // remove the (uint8_t*) cast, it's useless
                                        // but it doesn't do any harm
  uint32_t index = 4U;
  uint8_t name[] = "TEST";
  memcpy(&data[index], name, sizeof(name));     // use sizeof(name)

  //  index += 4U;                                << delete this line

  for (int j = index; j < index + sizeof(name); j++)  // start at j = index 
  {                                                   // and use sizeof(name)
    printf("%c \n", data[j]);
  }
Sign up to request clarification or add additional context in comments.

Comments

1

You have your data copied from index 4 but you print from index 0. So how do you want to have it printed?

To visualize this problem:

int main() 
{
  uint8_t* data = malloc(8 * 1024); 
                                    
  size_t index = 4;
  uint8_t name[] = "TEST";
  memcpy(&data[index], name, sizeof(name));     

  for (size_t j = 0; j < index + sizeof(name); j++) 
  {                                                  
    printf("data[%zu] = 0x%02hhx (%c)\n", j, data[j], isalpha(data[j]) ? data[j] : ' ');
  }
}

and the output:

data[0] = 0x00 ( )
data[1] = 0x00 ( )
data[2] = 0x00 ( )
data[3] = 0x00 ( )
data[4] = 0x54 (T)
data[5] = 0x45 (E)
data[6] = 0x53 (S)
data[7] = 0x54 (T)
data[8] = 0x00 ( )

I hope it will help you understand the problem.

Also for indexes use the correct type size_t instead of int or uint32_t.

7 Comments

You should mention that the content of data[0] to data[3] is actually undetermined.
@Oka makes no difference in this case
@Oka because it is not an UB.
I suppose UB was the wrong term, but those values are indeterminate.
@Oka And how does it affect the example if they are indeterminate? What is the danger? Will it make this program not working? Or it is just an unsuccessful pick attempt (related to wrong understanding of UB)?
|
0

This is what I am going to do so that you can copy the name at the desired index. This one works because the length of the string that you are copying is 4 and sizeof(uint32_t) is 4. Otherwise you will need to input the length of the bytes you are copying.

memcpy((uint8_t*)&_data[index], &name, sizeof(uint32_t));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.