Solved

getbuf() in C

Posted on 2004-05-02
5
2,229 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 125 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 how to create, access, and change arrays in the C programming language.

688 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