User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Jul 2008
Posts: 22
Reputation: flipjoebanana is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
flipjoebanana flipjoebanana is offline Offline
Newbie Poster

Help with memory block usage - C

  #1  
Aug 7th, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,165
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 930
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Help with memory block usage - C

  #2  
Aug 7th, 2008
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
Reply With Quote  
Join Date: Jul 2008
Posts: 619
Reputation: ArkM is just really nice ArkM is just really nice ArkM is just really nice ArkM is just really nice 
Rep Power: 5
Solved Threads: 91
ArkM's Avatar
ArkM ArkM is offline Offline
Practically a Master Poster

Re: Help with memory block usage - C

  #3  
Aug 7th, 2008
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:
  1. char *memory_block = malloc(sizeof(struct str_type)+sizeof(float)*num_of_float);
  2. float *array_of_floats = malloc(sizeof(float)*num_of_float);
  3. /* pretend this is filled out, with num_of_float = # of elements */
  4. struct str_type *ptr_str_type = malloc(sizeof(struct str_type);
  5. /* pretend struct members filled out */
  6.  
  7. memcpy(memory_block,ptr_str_type,sizeof(struct str_type));
  8. 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)...
Reply With Quote  
Join Date: Jul 2008
Posts: 22
Reputation: flipjoebanana is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
flipjoebanana flipjoebanana is offline Offline
Newbie Poster

Re: Help with memory block usage - C

  #4  
Aug 7th, 2008
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.
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,165
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 930
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Help with memory block usage - C

  #5  
Aug 7th, 2008
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.
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
Reply With Quote  
Join Date: Jul 2008
Posts: 619
Reputation: ArkM is just really nice ArkM is just really nice ArkM is just really nice ArkM is just really nice 
Rep Power: 5
Solved Threads: 91
ArkM's Avatar
ArkM ArkM is offline Offline
Practically a Master Poster

Re: Help with memory block usage - C

  #6  
Aug 8th, 2008
The last Ancient Dragon's post paragraph points to the serious defect in such nonstandard memory block using. For example, look at this structure:
  1. struct str_type { char c1, c2, c3; };
  2. /*
  3.  * As usually, sizeof(struct str_type) == 3.
  4.  * Warning: it's NOT C Standard consequence!
  5.  */
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...
Reply With Quote  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,165
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 38
Solved Threads: 930
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Help with memory block usage - C

  #7  
Aug 8th, 2008
>>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 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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 8:23 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC