Solved

getbuf() in C

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

773 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