0

I am trying to use the first few bytes of a section of memory on the heap to store meta-data about the section memory using C language (not C++).

The heap space is created using:

char* start_mem = (char*)malloc(10*sizeof(char)); //10 bytes of memory

Now, I'm trying to place a 'meta' struct in the first 4 bytes of allocated heap space.

typedef struct{
    int test;
}meta_t;

This is a test code I'm using to just understand how to do it before I implement it in the larger code.

test #include <stdio.h>

typedef struct{
    int test;
} meta_t;

int main(void) {

    char* start_mem = (char*)malloc(10*sizeof(char));

    meta_t meta;
    meta.test = 123;

    return 0;
}

Side note: Why does this type cast work:

int test = 123;
char c = (char) test;

but this type cast doesn't?:

meta_t meta;
meta.test = 123;
char c = (char) meta;

The main question is how can I fit the 'meta' data type (4 bytes) in to four char sized (1 byte) spaces at the start of the start_mem?

FYI - This is a small part of a larger project in a data structures class. Having said that there is no need to reply with "Why would you even bother to do this?" or "You could just use function_abc() and do the same thing." Restrictions have been set (i.e. a single use of malloc() ) and I would like to follow them.

trincot
  • 317,000
  • 35
  • 244
  • 286
Darrell
  • 139
  • 7

3 Answers3

0

You could use memcpy:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct{
    int test;
} meta_t;

int main() {

    char *start_mem = malloc(10);

    meta_t meta;
    meta.test = 123;

    memcpy(start_mem, &meta, sizeof(meta));

    printf("Saved: %d\n", ((meta_t *)(start_mem))->test);

    return 0;
}
LPs
  • 16,045
  • 8
  • 30
  • 61
0

What about this one?

memcpy(start_mem, &meta, sizeof meta);

Note that you have to pay attention to endianness.

nalzok
  • 14,965
  • 21
  • 72
  • 139
0

Even more simple, do a typecasted assignment:

#include <stdio.h>
#include <stdlib.h>

typedef struct{
  int test;
} meta_t;

int main() {

  char *start_mem = malloc(10);

  meta_t meta;
  meta_t *p;
  meta.test = 123;

  p = (meta_t *) start_mem;
  *p = meta;

  printf("Saved: %d\n", ((meta_t *)(start_mem))->test);

  return 0;
}
Henrik Carlqvist
  • 1,138
  • 5
  • 6
  • This is a strict aliasing violation and invokes undefined behavior. You can not treat an area of memory as something it's not, with the only exception being you can treat any area of memory as `[un[signed]] char`. See [**What is the strict aliasing rule?**](https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule) – Andrew Henle Aug 14 '21 at 20:16
  • Yes, this is true. In this particular case it will work in most cases as the struct only contains an int and the sizeof an int mostly is the same as the architecture word size. However, you cannot assume that an integer is 4 bytes on every architecture and the storing of the contents of a struct might give some padding. – Henrik Carlqvist Aug 15 '21 at 12:17