Solved

getbuf() in C

Posted on 2004-05-02
5
2,200 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
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:skypalae
Comment Utility
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
Comment Utility
Decai,

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

S.
0
 
LVL 46

Expert Comment

by:Sjef Bosman
Comment Utility
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
Comment Utility
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 125 total points
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now