Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 12th, 2005, 3:51 AM   #1
silvia
Newbie
 
Join Date: Jul 2005
Posts: 3
Rep Power: 0 silvia is on a distinguished road
Emergency: Confusing C Programming assignment questions

I get these funky programming assignment questions from my course. It is due very soon and the questions are very confusing.. PLEASE HELP!

LINE Contains
-------------------------
50 char * b, q, *r;
200 b=getbuf();
201 q = *b;
212 r= anotherfunction(b);
213-300 /* we want to use ‘q’ and ‘r’ here*/
2000 char * getbuf()
2001 {
2002 char buff[8];
2003-2050 /* unspecified, buff defined here *./
2051 return (char *) buff;
2052 }

1. What will be in variable ‘q’ after line 201 is executed? Under what conditions might this not be so?

2. Is there an alternative, but equivalent, way to write line 2000? If so, what is it?

3. Is getbuf() a reasonable function?

4. Will getbuf() execute at all?

5. Please comment on line 2051.

6. Is getbuf() good practice, and why?

7. What line not given should be provided for compilation?

8. How, exactly, could one get a second ‘char *’ to use back from this function? Be specific in terms of the exact syntax needed. (That is, provide code.) Another way to state this question is how can this function be modified to return a ‘char *’ (that is, it maintains the same return type) from the function, and an additional ‘char *’ value in one function call. Please make sure that your answer will work even if the size of the char * desired is not known in the outside calling function. Avoid (do not use) C++ syntax. Include statements in called and calling functions. Use good programming practice.

MY ANSWERS:
1
The variable q will contain unpredictable data, most likely junk.
I do not quite understand the question “Under what conditions might this not be so?” There’re 2 interpretations to this question.
1 “how to fix the program so that q will not contain junk but the actual array getbuf() returns?”. -- change line 2002 to “static char buff[8]”. So that the array space allocated inside getbuf() will still be available when the control is returned to the caller.
2 “What could happen by chance so that q will not contain junk but the actual array getbuf() returns”?” -- Luckily, when getbuf() ends, the space allocated for the array is not re-used by some other functions.

2
We can write “void * getbuf()”
Or “int * getbuf()” //should have warning for incompatible return type
Or “short * getbuf()” //same warning.
Basically, any pointers will work, since they all have the same size.

3
No it’s not a reasonable function. The function is returning pointer to a local variable, which will disappear when the program returns to the caller.

4
Yes, it will be execute, only with unexpected return result.

5
On line 2051, type casting is not neccessay. Since when we do “char buff[8]”, we have already implicitly made “buff” to be a pointer to point to the first element of the char array.

6
No, it is not a good practice. We should allocate memory in the caller, not inside the function. Because, if we allocate memory inside the function, and if we use static type, each time we call the function, it will reuse the same array and return the same pointer. i.e It will overwrite the information it returned to us last time.

7
I am not sure I understand the question. I assume I am being asked what lines to be added to make the code ready to be compiled.
1) function declaration for getbuf() – char* getbuf();
2) function declaration for anotherfunction() -- char* anotherfunction(char *);
3) void main(){
4) return statement for main
5) implementation of another function.


8
I can just pass in a pointer to char pointer to the function. And change the content of what that char pointer is pointing at.
silvia is offline   Reply With Quote
Old Jul 12th, 2005, 5:51 AM   #2
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,198
Rep Power: 5 grumpy is on a distinguished road
Well done. You've made a good attempt at answering the questions (unlike some who simply ask someone to do their homework for them, without trying).

In question 1, you've correctly picked up that the code as given yields undefined behaviour, as getbuf() returns a local (auto) variable. The answer isn't actually that the function "returns junk", but that the behaviour of the program upon assigning "q = *b;" could be anything. The program could also simply crash at that point, by trying to dereference a pointer that points at nothing valid. In other words, it is possible no value is stored in q as the program might simply terminate. The code always yields undefined behaviour, but the symptoms exhibited when it does could be anything.

Your answer to question 2 is one possibility. Another, if the intent is that the string returned by getbuf() will only be read, is to return a const pointer.

Question 3 : you are correct that the function is unreasonable, as any attempt to dereference the pointer returned by getbuf() yields undefined behaviour.

Your answers to questions 4 and 5 are essentially correct. The compiler treats any usage of "buff" as a pointer operation if required, due to equivalence of pointers and arrays. However, keep in mind that arrays and pointers are different things.

Question 6: you're right that the function is not good practice, but your reasoning needs improvement. The reason that it is not good practice is that it causes an innocent caller to exhibit undefined behaviour. Apart from the option you suggested (a static variable), another way around the problem (other than the one you suggested) is to dynamically allocate memory (using malloc(), for example) as such memory exists until explicitly released. The caveat of that alternative is that the caller needs to ensure that the memory is released, or a memory leak will occur. One more alternative is for the caller to allocate memory, and pass a pointer as an argument.

Question 8: lines 50-300 need to be within a function (eg within main()). If the body of getbuf() is below main(), or in another source file, the function needs to be declared via a prototype (eg "char *getbuf(void);") so that it can be called.

Question 9. Your answer is incorrect, as it requires the caller to know the size of the buffers to pass. What I suspect they're looking for is;
typedef struct
{
    char first_buffer[8];
    char second_buffer[10];
} Retval;

Retval getbuf()
{
     Retval value;
     /*  sample implementation */
     strcpy(value.first_buffer, "Hello");
     strcpy(value.second_buffer, "Bye");
     return value;
}
This approach also avoids all of the problems with the original getbuf(). No undefined behaviour. No need to pass a buffer as an argument. No need to use dynamic memory allocation. The only cost is that lengths of strings must be known to the programmer.
grumpy is offline   Reply With Quote
Old Jul 12th, 2005, 6:56 PM   #3
silvia
Newbie
 
Join Date: Jul 2005
Posts: 3
Rep Power: 0 silvia is on a distinguished road
Thanks thanks thanks thanks !
But then for #9, the question says "how can this function be modified to return a ‘char *’ (that is, it maintains the same return type) from the function, and an additional ‘char *’ value in one function call." It needs to return char*, not Retval..

so I am guessing..
char * getbuf(char *test){
static char temp[10];
//do something on temp;
test=temp;
}

will it work?
silvia is offline   Reply With Quote
Old Jul 13th, 2005, 3:39 AM   #4
proghelper
Newbie
 
Join Date: Jun 2005
Posts: 16
Rep Power: 0 proghelper is on a distinguished road
YEs I think you are correct on this except for a single mistake:

char * getbuf(char *test){
static char temp[10];
//do something on temp;
test=temp;
return temp;
}

Using this in main() program:

char* a;
char* b;
a = getbuf(b);

Now if you print both a and b, they will result in the same value.



-------
http://www.programminghelp4u.com - Programming (Assignment/Project) Help
proghelper is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:59 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC