•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 425,918 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,680 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 318 | Replies: 6
![]() |
•
•
Join Date: Jul 2008
Posts: 22
Reputation:
Rep Power: 1
Solved Threads: 0
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:
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
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
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,165
Reputation:
Rep Power: 38
Solved Threads: 930
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.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
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:
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)...
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:
c Syntax (Toggle Plain Text)
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)...
•
•
Join Date: Jul 2008
Posts: 22
Reputation:
Rep Power: 1
Solved Threads: 0
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.
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,165
Reputation:
Rep Power: 38
Solved Threads: 930
Actually its not necessary to do more than one malloc
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.
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));
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
The last Ancient Dragon's post paragraph points to the serious defect in such nonstandard memory block using. For example, look at this structure:
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...
c Syntax (Toggle Plain Text)
struct str_type { char c1, c2, c3; }; /* * As usually, sizeof(struct str_type) == 3. * Warning: it's NOT C Standard consequence! */
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...
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,165
Reputation:
Rep Power: 38
Solved Threads: 930
>>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.
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.
I think it's about time we voted for senators with breasts. After all, we've been voting for boobs long enough. ~Clarie Sargent, Arizona senatorial candidate
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
Those who are too smart to engage in politics are punished by being governed by those who are dumber. ~Plato
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
age amd avatar blue gene chips database development dos economy energy enterprise firefox flash gecko handsets hardware ibm ibm. news intel ibm it laptop leak linux medicine memory memory cards microsoft mozilla news open source openoffice pc ps3 recession red hat russia sandisk sun supercomputer supercomputing technology trends ubuntu wireless working x86
- Write Your First Application in Win32 Using Assembly (Assembly)
- memory management in wndows 2000 (Windows NT / 2000 / XP / 2003)
- reasons why malloc fails? (C)
- Crazy Leak...plz help (C++)
- HiJaCk ThIs notepad! (Viruses, Spyware and other Nasties)
- Pointers (C++)
- Pointers (Part II) (C)
Other Threads in the C Forum
- Previous Thread: how to detect arrow keys??
- Next Thread: FORCE MQGET call to fail



Linear Mode