Hello,
I am trying to figure out how to use memory blocks and create a block of data that contains different data types within it.

In my particular example I want a pointer to a memory block that contains a struct followed by an array of floats. This is what I'm trying:

char *memory_block;
float *array_of_floats;
 /* pretend this is filled out, with num_of_float = # of elements */ 
struct str_type *ptr_str_type = malloc(sizeof(struct str_type);
 /* pretend struct members filled out */

memcpy(memory_block,ptr_str_type,sizeof(struct str_type));
memcpy(memory_block+sizeof(struct str_type),array_of_floats,sizeof(float)*num_of_float);

Is this supposed to work(tried using this)? Is it the right way for going about it?
When looking at *memory_block in debugger, what should I be seeing?

Thanks

If you want your own memory block to act like the memory pool allocated by malloc() then you have to write some sort of memory management functions for the pool. Use malloc() only to allocate the initial memory block. After that your program has to keep track of free blocks and used blocks. AFAIK this is normally done by use of linked list. It can get a bit complicated.

Alas, no any memory blocks in your snippet. You have two uninitialized pointers (both point to nowhere) - memory_block and array_of_floats.

So you copy from nowhence to nowhere (with possible access violation, segment faults and other charms).

Possible (but, strictly speaking, not conforming to the language standard) solution:

char *memory_block = malloc(sizeof(struct str_type)+sizeof(float)*num_of_float);
float *array_of_floats = malloc(sizeof(float)*num_of_float);
 /* pretend this is filled out, with num_of_float = # of elements */ 
struct str_type *ptr_str_type = malloc(sizeof(struct str_type);
 /* pretend struct members filled out */

memcpy(memory_block,ptr_str_type,sizeof(struct str_type));
memcpy(memory_block+sizeof(struct str_type),array_of_floats,sizeof(float)*num_of_float);

Well, memory_block is a pointer to char. Probably, your debugger will try to display its contents as a C string (not as struct str_type, of course)...

Thanks for the replies!! Should have put that I malloced memory for those things as well, sry. Just wanted to make sure that this will create a message that contains only the binary copy of the data to read. Point to it with the char *, or could you use any type of pointer in this case? Then pass the pointer to another function, which then reads the raw binary data pointed to, and knowing exactly what type of data it is supposed to be, extract it into the correct type data.

Actually its not necessary to do more than one malloc

char *memory_block = malloc(sizeof(struct str_type)+sizeof(float)*num_of_float);
struct str_type *ptr_str_type = (struct str_type *)memory_block;
float *array_of_floats = (float *)(memory_block + sizeof(struct str_type));

The above array of floats can be optimized by aligning the array on the mathine's word boundry. That involves adding a bit more memory to memory_block and adjusting the start of the float array.

The last Ancient Dragon's post paragraph points to the serious defect in such nonstandard memory block using. For example, look at this structure:

struct str_type { char c1, c2, c3; };
/*
 * As usually, sizeof(struct str_type) == 3.
 * Warning: it's NOT C Standard consequence! 
 */

In that case all "right" snippets above are wrong, array_of_floats pointer points to the location with invalid (for floats) memory alignment. Of course. you may align this pointer to a proper bound but it obviously exceeds the limits of this thread discussion level...

Moral: it's not 100% correct usage of memory blocks allocation. Try to avoid it.

The C language memory model (carefully adjusted & extremelly portable) does not guarantee mutual location of independently declared variables. If you have pragmatically (or/and semantically) constrained a struct and an array, better allocate them independently. Add a pointer to an array in a new (combined) structure to reflect these relations and so on...

>>In that case all "right" snippets above are wrong, array_of_floats pointer points to the location with invalid (for floats) memory alignment

I agree, and I mentioned the alignment problem that would have to be fixed in order for the code snippet I posted to be actually useful. There are probably only a handfull of reasons to use that technique, and certain no reason at all for college-level students.

This article has been dead for over six months. Start a new discussion instead.