Perhaps you should create an array of char[MESSAGE_MAX_SIZE] instead.
Printable View
Perhaps you should create an array of char[MESSAGE_MAX_SIZE] instead.
and why is that? can you show me how to copy a struct to that char[MESSAGE_MAX_SIZE] array?
You would be able to use memcpy() for that, but you have to be wary of structs that contain pointers.
well if I use an array of char then if I want to copy back from that array to recv, then how is that possible? I don't know where at what index it's stored at
Your idea was to allocate blocks of equal length, then use a block to store an object, possibly with some space wastage (and of course you cannot store objects larger than the block size).Quote:
Originally Posted by -EquinoX-
yes that's true that there might be some space wastage, but I don't care about that.. if it's an array of char then each block will be 8 bytes... am I seeing this right? so if I want to store a struct that is 40 bytes, then I can't... as size of message is greater than size of each block in the array... so I can't really use a char...
please correct me if I am seeing this the wrong way
in other words my code wants to behave like this:
find an empty slot in the array
if that empty slot size is greater than the size of message to be copied
then put the message to the mailbox
therefore say that index 0 is empty, then I can put the message to that index and then I can get it back from index 0 by doing a memcpy back
No, each block will be as large as you define it to be (you are probably thinking of 8 bit chars). You can set it as blocks of 40 bytes, if you wanted. Using an array of arrays of char might be a little easier. Still, I am not sure how to keep track of the types, as mentioned earlier, but matsp has hinted that there are ways to do so.Quote:
Originally Posted by -EquinoX-
how do you make so that each block will be 40 bytes? maybe some code example would help, as I still don't see it
I have tried to make since of this thread. What I gather is that you may be needing to set up a two-demensional array allocation, this can be set up to be dynamic as well possibly to allocate enough space for a string or ints.
I may be off base, but if not this should get you going in allocating enough space for your e-mails(numBlocks) and what they may contain(blockSize).Code:
int **my2Demail-array(int numBlocks, int blockSize)
{ int i;
int **t = malloc(numBlocks * sizeof (int *));
for (i = 0; i < blocks; i++)
t[i] = malloc(blockSize * sizeof(int));
return t;
}
Modifying this two work with structures may be a bit messy and then linking them may be preferred.
will something like this work?
I don't know why in the previous post before this you declared that as an intCode:void** newArray = (void**) malloc(sizeof(*newArray) * newBox->numslots);
for (i = 0; i < newBox->numslots; i++)
newArray[i] = malloc(sizeof(newBox->size));
There you go. An array of any type. Naturally you'll want to figure out what type you meant to use, but I'll leave that as an exercise to the reader.Code:union anytype {
char c;
char *pc;
int i;
int *pi;
...
struct someStructHere ss;
struct someStructHere *pss;
};
...
union anytype arrayof[ LOL ];
Quzah.