Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what uses more memory? ie the best method.

Status
Not open for further replies.

jdarvwill

Technical User
May 22, 2002
12
0
0
CA
hi, i just want to know if it uses more memory to make an array of structures, or use a structure with arrays inside. thanks :)

struct inventory
{
char item_name[30];
int part_number;
int quantity;
float cost;
float total_cost;
};

struct inventory items[10];

or,

struct inventory
{
char item_name[30][20];
int part_number[20];
int quantity[20];
float cost[20];
float total_cost[20];
};

just an exmaple of what i mean :)
 
thanks for the reponse, the hardest thing about C is learning the proper style... i'll go with array of structures style now :)
 
But in either case you are wasting memory because you allocate 30 bytes for each item_name (are you sure you wanted that exact amount - it infers that the maximum length for an item-name is 29 bytes as you have to allow 1 byte for the terminating NUL) when you presumably don't have names that are all exactly 29 characters long.

If you are trying to minimise your memory usage you should allocate the memory for the item_name dynamically. There are a couple of ways you can do this.....

1) malloc enough memory to hold the item name (plus the terminating NUL) and store the pointer to the memory in the structure.

2) malloc the structure(s) and when doing so rather than requesting enough memory for the structure you could ask for enough for the structure and the item name string. This malloc would return a pointer which you would store in an array. The item name string could then be stored in the malloc'ed area after the structure elements by using the pointer and adding an offset to clear the structure elements. This method has the advantage over method 1) that you don;t have to remember to free() the memory allocated for the item name separately.

Cheers - Gavin
 
hi...

In my opinion ...you should go for array of pointers to structure. Ofcourse using the dynamic memory allocation with the care taken to free the memory allocated programatically.

Regards
Sanjay
 
Jdarvwill,
There is a serious possibility that the
struct inventory
{
char item_name[30][20];
int part_number[20];
int quantity[20];
float cost[20];
float total_cost[20];
};
structure will take much less memory than the array of structures.

The resoning is that most compilers try to optimise access to data by ensuring that it is cache alligned,or atleast word alligned (word being 4 bytes for x86) So when you declare an array of char item_name[30] the compiler will allocate 32 bytes for item_name,so that it can allocate the integer part_number at a word boundary.

cheers
amit
crazy_indian@lycos.com

to bug is human to debug devine
 
amit

The problem is that by allocating a single structure with internal arrays as per your example you allocate 600 bytes for teh item names when in all likelihood they won't use nearly that amount of storage, you would need to store an array of pointers to malloc'ed areas in your single structure.

Cheers - Gavin

PS - This thread is a bit like a blast from the past for me, I started in DP 30 years ago when memory was on real ferrite cores and memory allocation was a matter of life and death (and professional pride) due to the lack of space in systems in those days.

These days with systems with multi-megabytes of real memory and unlimited virtual memory we don't see nearly enough emphasis on efficient processing logic or efficient memory use.
 
Gavin,
I agree that using static allocation is the most horrible way to waste memory,but some embedded applications have to still do it,as they can't withstand the overhead of malloc() calls.

cheers
amit
crazy_indian@lycos.com

to bug is human to debug devine
 
thanks for the responses :)

we have never used malloc() in class (yet?) so i'll take a look into it. :) And i'll look into all the other ideas also. Good practice if nothing else :)

is memory mang. even an issue today? as newmanj pointed out, on todays systems we have a lot more access to memory than we did in the past. although i don't beleive in wasting what does not need to be wasted.

for newmanj, i'll try to figure option 2 out.. looks interesting :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top