Ok only had a little time to code today so I fixed the lack of an initializing function I set up
Code:
void Init_person(personal_info *client, char name[NAME_SIZE],
int feet, double inches, int weight, int age);
It's a little different then Bayint Naung suggested. Only in the fact I changed the name from a string literal to a string array (thank you MK27) and broke down the height into feet and inches. So the use would look like this now.
Code:
Init_person (&client[0],"Wooley, Bill", 5,11.5,185,26);
Init_person (&client[1],"Smith, Jane",5,2.45,121,28);
Originally Posted by
Elysia
You don't necessarily have to use malloc/calloc. So long as the data is still valid when you're searching, you can just have the actual data on the stack. Pointers can point anywhere - it just has to be a valid memory location.
You only need a pointer to the actual struct. Then you implement some semantics on how the structs are actually sorted. You would only move the pointers around, so no data is copied anyway if you use pointers to the structs only.
I have been thinking about this and the debate that it brought up. The exchange has been enlightening and I will have to take some time to study all the aspects that were brought up. But in the bottom line I think that for me right now the best bet would be to learn to use malloc/free and then worry about optimization once I have a working knowledge of exactly how memory management is handled.
However my personality tends to drift toward taking risks so I am inclined to try out things that I probably shouldn't. And the idea of using the stack is tempting.
but for now I will behave myself and stick to the more basic design techniques.
Originally Posted by
MK27
In fact it is not very different than your existing code:
Code:
for (i=0;i<10;i++) {
client[i] = malloc(sizeof(personal_info));
/* error handling */
if (!client[i]) {
puts("OUT OF MEMORY!");
return -1;
}
}
Wow that is simple! Thanks for the example.
Originally Posted by
MK27
but the OP needs to understand the difference in order to exercise such a choice, which means learning how memory is allocated in C
.
Trust me I am very aware of the need to know how memory is allocated. from what I have read I have learned so far that when the program first starts that memory is allocated for all the local variables and arrays that are found in main in a static block of memory and the "heap" or dynamic memory is just above that it is used for allocated memory and grows up. Now in high memory is an area "the stack" that holds things like return pointers for functions and the functions local variables.
On the function call the return address is push onto the stack along with the arguments for the function. As the function begins to run the local variables are pushed onto the stack and when the function is complete they are popped off with the return address then the return value is pushed onto the stack and the program picks up from where it was called.
the stack grows downward toward lower memory.
I hope I didn't mess that up too bad
Well I am very tired from a long day. I know I have not replied to all of what was said so far but I have a lot to study about what was brought up. I don't want anyone to think that I have ignored them. I am listening very intently to everyone and carefully going over each point. I do thank you all for your help and patients!!