0
struct media{
     uint32_t addressOfPtr; 
 };

Some error somewhere, but I am not able to see it.

printf(" > %x",  ((uint8_t*) mediaObject2->addressOfPtr)[i]);

is not print

uint8_t message[SIZE_MAX];

for (i=0;i<SIZE_MAX;i++) {
    message[i] = i+1;
    printf(" > %x",  message[i]);
}

uint8_t *msg_Ptr;
msg_Ptr = malloc(SIZE_MAX*sizeof(uint8_t));
memcpy(msg_Ptr, &message, SIZE_MAX);

printf("\n####################\n");

for (i=0; i < SIZE_MAX; i++)  // message fixed at length 10
    printf(" > %x",  msg_Ptr[i]);

printf("\n");

struct media *mediaObject2;
(mediaObject2->addressOfPtr) = malloc(SIZE_MAX*sizeof(uint8_t));
(mediaObject2->addressOfPtr) = (uint32_t) msg_Ptr;
printf("\n####################\n"); // Last stop
//printf(">>>> %x ", mediaObject2->addressOfPtr);

printf("\n");
for (i=0; i < SIZE_MAX; i++)  // message fixed at length 10
    printf(" > %x",  ((uint8_t*) mediaObject2->addressOfPtr)[i]);

printf("\n");
2
  • 1
    And should use a different name SIZE_MAX from being used in <stdint.h>. Commented Jan 27, 2014 at 1:28
  • you, are right. I will. Commented Jan 27, 2014 at 1:33

1 Answer 1

1

In this part:

struct media *mediaObject2;
(mediaObject2->addressOfPtr) = malloc(SIZE_MAX*sizeof(uint8_t));

you are using uninitialized pointer mediaObject2, which invokes an undefined behavior.

You need to either dynamically allocate memory for struct media or yet even better: use a variable with automatic storage duration here:

struct media mediaObject2;
mediaObject2.addressOfPtr = malloc(SIZE_MAX*sizeof(uint8_t));

Side note: If struct media was supposed to hold a pointer to an array of uint8_t, you should have declare this member as uint8_t *data or if the type is going to vary but it will be a pointer still, then void* dataPtr at least... anything is better than uint32_t addressOfPtr;.

Sign up to request clarification or add additional context in comments.

3 Comments

excellent. @LihO. On a further question. how do I pass it to a function, say: void functionB (void* mediaObject) and pass the struct back to uint8_t message[SIZE_MAX]?
@UrsaMajor: functionB(&mediaObject2)... you might consider spending more time reading some books or other relevant materials.
I did that, but it is not able to reload within the function to the uint8_t message[].

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.