Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more


getbuf() in C

Posted on 2004-05-02
Medium Priority
Last Modified: 2007-12-19

char *a, b, *c;
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()"?
Question by:Decai
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

Expert Comment

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.

Expert Comment

ID: 10974762

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

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.

Expert Comment

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.
LVL 46

Accepted Solution

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!

Featured Post


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

This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

647 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