does any one know how to solve?


 
Thread Tools Search this Thread
Top Forums Programming does any one know how to solve?
# 8  
Old 08-21-2008
IMHO that kind of solution fits best in const cases because in this case the original poster might want to add more entries in employee array struct from say, user input?

I thought I should bring this up because mlhazan doesn't seem to be a very experienced C programmer. Anyhow, I might be wrong and shamrock's solution could be exactly what mlhazan is looking for.
# 9  
Old 08-22-2008
Quote:
Originally Posted by redoubtable
IMHO that kind of solution fits best in const cases because in this case the original poster might want to add more entries in employee array struct from say, user input?

I thought I should bring this up because mlhazan doesn't seem to be a very experienced C programmer. Anyhow, I might be wrong and shamrock's solution could be exactly what mlhazan is looking for.
Imho that solution should be used for both static and dynamic cases. It's not advisable to create a 1000 element array on the stack and end up using only a small portion of it. Knowledge of sizeof operator is even more important if the code was accepting user input from the keyboard because in that scenario calls to malloc() printf() and fgets() have to be interleaved together in order to fill up a single empRec structure though its size should be known beforehand.
# 10  
Old 08-22-2008
Quote:
Originally Posted by shamrock
Imho that solution should be used for both static and dynamic cases. It's not advisable to create a 1000 element array on the stack and end up using only a small portion of it. Knowledge of sizeof operator is even more important if the code was accepting user input from the keyboard because in that scenario calls to malloc() printf() and fgets() have to be interleaved together in order to fill up a single empRec structure though its size should be known beforehand.
I agree with static cases when the total number of structs inside employee is known and sizeof() returns the whole size of the array so if we want to know the number of entries we just have to divide sizeof(employee) for sizeof(struct empRec). But in dynamic cases when the user wishes to add more entries in employee array struct, if the array (employee) is bigger (1000 structs) than the number of entries (struct empRec) it has, sizeof() will not help to determine the number of entries because it will give us the size of those 1000 structs and we just want to know those we're using. Also if pointers/malloc() are used, sizeof() won't be of any help I guess.
Code:
    static struct empRec employee[10] =
    {
        {"Peter North","4B-208",35400,{10,11,1983}},
        {"John Musa","2B-118",25400,{07,10,1993}},
        {"Paula Jeminova","1A-506",18700,{02,1,1990}},
        {"Patricia Silver","6C-123",52100,{14,21,2000}},
        {"Robert mill","4D-318",42100,{01,19,2008}}
    }

In this case, if we would want to add a few more entries dynamically we could not determine the number of existing entries (5) using sizeof(), because sizeof(employee) is the same as saying sizeof(struct empRec)*10.

This is why I was advising the original poster to use a NULL/empty struct reference as the final entry so the number of entries could be calculated by counting structs until an empty one was found.

I also agree that having 1000 entries is far too much, but I was not arguing about that. Anyway, how do you use sizeof() in dynamic cases?
# 11  
Old 08-25-2008
Quote:
Originally Posted by redoubtable
I agree with static cases when the total number of structs inside employee is known and sizeof() returns the whole size of the array so if we want to know the number of entries we just have to divide sizeof(employee) for sizeof(struct empRec). But in dynamic cases when the user wishes to add more entries in employee array struct, if the array (employee) is bigger (1000 structs) than the number of entries (struct empRec) it has, sizeof() will not help to determine the number of entries because it will give us the size of those 1000 structs and we just want to know those we're using. Also if pointers/malloc() are used, sizeof() won't be of any help I guess.
The problem with static cases is that you are restricted in the size of the database that can be created. Declaring a 1000 element array implies that the database can't have more than 1000 records in it atleast there is no easy way to alter the number of records it can have short of modifying the array definition in the source code and re-compiling.
Quote:
Originally Posted by redoubtable
Code:
    static struct empRec employee[10] =
    {
        {"Peter North","4B-208",35400,{10,11,1983}},
        {"John Musa","2B-118",25400,{07,10,1993}},
        {"Paula Jeminova","1A-506",18700,{02,1,1990}},
        {"Patricia Silver","6C-123",52100,{14,21,2000}},
        {"Robert mill","4D-318",42100,{01,19,2008}}
    }

In this case, if we would want to add a few more entries dynamically we could not determine the number of existing entries (5) using sizeof(), because sizeof(employee) is the same as saying sizeof(struct empRec)*10.
the sizeof operator returns the allocated size instead of the used size for any object...so in the above case it will return the "sizeof one empRec structure times 10".

Quote:
Originally Posted by redoubtable
This is why I was advising the original poster to use a NULL/empty struct reference as the final entry so the number of entries could be calculated by counting structs until an empty one was found.
That would work only for static cases and in scenario where the last entry was NULL it implies wasting memory since only 5 out of 10 structs can be used.

Quote:
Originally Posted by redoubtable
I also agree that having 1000 entries is far too much, but I was not arguing about that. Anyway, how do you use sizeof() in dynamic cases?
Determine the size of one empRec structure and malloc'ate memory for it before adding a new record. This way the database size is not limited by the number of elements in the array.
The code snippet below can be put inside a loop to allocate space for a block of at least empRec size bytes followed by prompting the user for input and storing it in one of the elements of the empRec structure members.

Code:
pointer_to_empRec_object = malloc(sizeof(struct empRec));

while () {    /* while loop to capture user input */
    printf("get emp name ");
    fgets()   /* store emp name input above */
    printf("get emp room ");
    fgets()   /* store emp room */
    printf("get emp start date ");
    fgets()   /* store emp start date */
}   /* end of while loop */

# 12  
Old 08-25-2008
Quote:
Originally Posted by shamrock
The problem with static cases is that you are restricted in the size of the database that can be created. Declaring a 1000 element array implies that the database can't have more than 1000 records in it atleast there is no easy way to alter the number of records it can have short of modifying the array definition in the source code and re-compiling.
Sure. For the situation at hand static cases should only be used if performance really matters. If there is a need to expand the number of entries to an unknown value we should use dynamic algorithms (linked list implementation for example)

Quote:
the sizeof operator returns the allocated size instead of the used size for any object...so in the above case it will return the "sizeof one empRec structure times 10".
Thus confirming that your initial solution can't be used in both static/dynamic cases like I said. If you have a static case that needs expanding sizeof() will not help to determine the number of entries and if you have a dynamic case sizeof() will not help too.

Quote:
Determine the size of one empRec structure and malloc'ate memory for it before adding a new record. This way the database size is not limited by the number of elements in the array.
The code snippet below can be put inside a loop to allocate space for a block of at least empRec size bytes followed by prompting the user for input and storing it in one of the elements of the empRec structure members.

Code:
pointer_to_empRec_object = malloc(sizeof(struct empRec));

while () {    /* while loop to capture user input */
    printf("get emp name ");
    fgets()   /* store emp name input above */
    printf("get emp room ");
    fgets()   /* store emp room */
    printf("get emp start date ");
    fgets()   /* store emp start date */
}   /* end of while loop */

Remember that we're talking about an array of structs so either we use linked lists or we would have to malloc() a pointer of pointers to struct empRec.

Anyway I was just saying that the initial solution you posted will fit best in static cases which don't need expansion because you used sizeof() to calculate the number of entries and that wouldn't be possible if there were more entries in the array struct than those in use.
# 13  
Old 08-26-2008
Beg to Differ

Quote:
Originally Posted by redoubtable
Sure. For the situation at hand static cases should only be used if performance really matters.
On the contrary if performance really matters one would go with a dynamic solution in order to minimize the overhead of allocating a very large memory segment and using only a small portion of it. A static solution is not optimized in terms of memory management.

Quote:
Originally Posted by redoubtable
If there is a need to expand the number of entries to an unknown value we should use dynamic algorithms (linked list implementation for example)
Yes a linked list should be used for dynamic cases as it allows searching the database and removing entries from it whereas a sequential list like the one used by the o/p allows only insertions in the form of appends.

Quote:
Originally Posted by redoubtable
Thus confirming that your initial solution can't be used in both static/dynamic cases like I said. If you have a static case that needs expanding sizeof() will not help to determine the number of entries and if you have a dynamic case sizeof() will not help too.
Why would you need to expand a static array. Don't you think that's an oxymoron? If you need to expand an array it becomes dynamic by default. The only case where that solution won't work is the gray scenario you suggested where the number of allocated entries is more than the used ones; as in the case below where the array has 10 entries allocated and only 5 are used. Smilie

Code:
static struct empRec employee[10] =
{
     {"Peter North","4B-208",35400,{10,11,1983}},
     {"John Musa","2B-118",25400,{07,10,1993}},
     {"Paula Jeminova","1A-506",18700,{02,1,1990}},
     {"Patricia Silver","6C-123",52100,{14,21,2000}},
     {"Robert mill","4D-318",42100,{01,19,2008}}
}

Quote:
Originally Posted by redoubtable
Remember that we're talking about an array of structs so either we use linked lists or we would have to malloc() a pointer of pointers to struct empRec.
You won't need double level of indirection...the memory returned by malloc() will be assigned to a pointer to an object of type struct empRec that is a single level of indirection. My earlier post showed that scenario.

Quote:
Originally Posted by redoubtable
Anyway I was just saying that the initial solution you posted will fit best in static cases which don't need expansion because you used sizeof() to calculate the number of entries and that wouldn't be possible if there were more entries in the array struct than those in use.
Imho the only case which it doesn't fit is the gray case you proposed where the number of allocated entries is more than the ones used.
# 14  
Old 08-26-2008
I think we're failing to communicate due to my poor English.

Quote:
Originally Posted by shamrock
On the contrary if performance really matters one would go with a dynamic solution in order to minimize the overhead of allocating a very large memory segment and using only a small portion of it. A static solution is not optimized in terms of memory management.
This is a question of circumstance, memory access speed is generally the same for both static and dynamic memory but if you're creating a program where (malloc()ing) and free()ing entries happens too often a static implementation (with proper algorithms to avoid overflows) should be prefered.


Quote:
Why would you need to expand a static array. Don't you think that's an oxymoron? If you need to expand an array it becomes dynamic by default. The only case where that solution won't work is the gray scenario you suggested where the number of allocated entries is more than the used ones; as in the case below where the array has 10 entries allocated and only 5 are used. Smilie
I was not referring about expanding the array itself! I was talking about expanding the number of entries inside the array (so called "gray scenario"). The gray situation has some indications. For instance, if the proper algorithm (avoid bound overflows) is used and we're going to add and remove many entries at a time (for example, buffer purposes).

Quote:
You won't need double level of indirection...the memory returned by malloc() will be assigned to a pointer to an object of type struct empRec that is a single level of indirection. My earlier post showed that scenario.
Yes I never said it was imperative I just said that you where proposing a one-level solution when the user was having a two-leveled problem. I just said that pointer you refer would have to be linked inside a list (linked lists) to fulfill the user's problem or be inside a double level pointer.


IMHO the best solution is not generally applied to every case hence there is no point saying "dynamic is the best way" or "static is the best way". The best solution is that which fits the requirements and has best performance.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. UNIX Desktop Questions & Answers

please help me to solve it

i thought about to use the commands : wc and sort cut and mybe more .. i need to write a bash script that recive a list of varuables kaka pele ronaldo beckham zidane messi rivaldo gerrard platini i need the program to print the longest word of the list. word in the output appears on a... (0 Replies)
Discussion started by: yairpg
0 Replies

2. Homework & Coursework Questions

help me to solve it thank you

i thought about to use the commands : wc and sort and mybe more .. i need to write a bash script that recive a list of varuables kaka pele ronaldo beckham zidane messi rivaldo gerrard platini i need the program to print the longest word of the list. word in the output appears on a separate... (1 Reply)
Discussion started by: yairpg
1 Replies

3. UNIX for Dummies Questions & Answers

Can somebody solve this

I have to find the files older than 200 days from a path and copy them to some other directory with the current date stamp attached to it. i have written like follows: #!/bin/ksh DSTAMP=$(date +"%y%m%d%H%M") rm $CA_OUT_PATH/ftp_logs/temp touch $CA_OUT_PATH/ftp_logs/temp chmod 777... (13 Replies)
Discussion started by: sreenusola
13 Replies

4. UNIX for Advanced & Expert Users

Can somebody solve this

I have to find the files older than 200 days from a path and copy them to some other directory with the current date stamp attached to it. i have written like follows: #!/bin/ksh DSTAMP=$(date +"%y%m%d%H%M") rm $CA_OUT_PATH/ftp_logs/temp touch $CA_OUT_PATH/ftp_logs/temp chmod 777... (1 Reply)
Discussion started by: sreenusola
1 Replies

5. Shell Programming and Scripting

Can somebody solve this please

I have to find the files older than 200 days from a path and copy them to some other directory with the current date stamp attached to it. i have written like follows: #!/bin/ksh DSTAMP=$(date +"%y%m%d%H%M") rm $CA_OUT_PATH/ftp_logs/temp touch $CA_OUT_PATH/ftp_logs/temp chmod 777... (1 Reply)
Discussion started by: sreenusola
1 Replies

6. Shell Programming and Scripting

How to solve this

I have to write an script for.. CUST: 123 trans: some contents CUST: 1234 trans: some contents Now wat i have to do is this: CUST:123 akash trans: some contents CUST:1234 akash1 trans: I have been able to add... (3 Replies)
Discussion started by: akashag22
3 Replies
Login or Register to Ask a Question