Suppose I have a structure, and a pointer to a memory location p, how do I make sure that while creating an instance ‘a’ of the structure, it is placed into a memory chunk starting at p?
So I am passed a chunk of memory and I want to create a node at the beginning of it, so I need to make sure that the node is created at the beginning of it. (Note: I cannot use malloc, calloc, free, memcpy, or similar functions because I am writing code for a memory management system).
You don’t really ‘create’ instances of structures in C like that. Assuming that
ppoints to a block of usable memory, you can just treatpas a pointer to your structure type:Once
pis cast (implicitly or explicitly as above), you can initialize the contents of your structure however you wish.As an example, the following code will initialize a structure as you seem to request:
Don’t get hung up on the
mainfunction above, it’s only to illustrate how you can pass an arbitrary memory address to the function. In your real-world case, you will have the memory already allocated somehow.