?
Solved

getbuf() in C

Posted on 2004-05-02
5
Medium Priority
?
2,239 Views
Last Modified: 2007-12-19

char *a, b, *c;
a=getbuff();
b=*a;
c=otherfunction(a);
...
char *getbuff()
{
     char buff[8];
     ...
     return (char*)buff;
}

After executed, what will be in variable "b"? And under what conditions might this not be so?
Are there any way to wirte an alternative, but equivalent, as "char *getbuff()"?
0
Comment
Question by:Decai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:skypalae
ID: 10974746
Hi Decai,

since buff[8] is statically allocated inside the getbuff() the pointer becomes non-valid after return. Accessing the memory under that pointer (the b=*a line) will most probably cause the program crash (also passing a non-valid pointer to otherfunction() will most probably cause the crash)

if you want to make the pointer valid as long as you wish use malloc() and free() functions instead.

char *getbuff () {
    char *buff = (char *) malloc (sizeof(char) * 8) ;
    ...
    return buff ;
}

but don't forget to free() the memory at the end of program or you'll get memory leaks.

Cheers! S.
0
 
LVL 4

Expert Comment

by:skypalae
ID: 10974762
Decai,

and to answer you completely .. the 'b' will contain a[0] element.

S.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 10975926
In my humble opinion
1) the content of variable b can be predicted, but it is higly unsafe!
2) this might not be predictable when you have one or more functions that catch a signal() in Linux;
  if you use *a in a later stage, after having called one or more other functions, *a might have a totally different value
3) for an alternative, see above (malloc()) but don't forget to free() the memory once unnecessary (there is no garbage collection in C)

Unlike skypalae says, the buff[8] isn't statically allocated, it is on the stack. There are 3 types of memory: main, stack and heap. Static variables are in main memory. Your buff is on the stack, and when the function getbuff returns, stack content gets lost. The stack's memory probably still exists, so you can still "use" it. It's memory isn't it, and it's yours to use. Fiddling with the stack is very dangerous though, your program might crash! The function malloc() dynamically allocates space (from the heap). It is very good practice to free() the memory allocated by malloc() once done with it, to keep your program from growing out of bounds.
0
 

Expert Comment

by:CodeDeamon
ID: 10989661
This is a test question if I have ever seen it.  I love it when "programmers" try to get actual programmers to do their work for them.
0
 
LVL 46

Accepted Solution

by:
Sjef Bosman earned 375 total points
ID: 10989864
I think we'd better make it good practice to never supply the full answer, or throw in some pitfalls for the cut/paste student. I've seen lots of complete code passing by, to be called "homework" as I've noticed, but this was an inconspicuous one, so "I plead guilty". I think it's not illegal EE behaviour though, or is it?

I suppose we're all too happy to be of help. I hope the others will be more hesitant to give very detailed answers to questions like these.

Thanks for the warning!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question