proper allocation

J

James Leddy

Hello

I'm writing a program that includes a call which returns a string, namely
getpass() in the standard c library. I was not surprised to learn that I
had improperly allocated memory to hold the return value. I don't really
know how a function returning a string works, and I have no knowledge at
compile time as to how long the string is going to be. Should I just
allocate enough memory for anything possible?

ex.
char *pass;
//pass = malloc(100) maybe 10000?
pass = getpwd("password: ");

Thanks,
 
A

Artie Gold

James said:
Hello

I'm writing a program that includes a call which returns a string, namely
getpass() in the standard c library. I was not surprised to learn that I
had improperly allocated memory to hold the return value. I don't really
know how a function returning a string works, and I have no knowledge at
compile time as to how long the string is going to be. Should I just
allocate enough memory for anything possible?

ex.
char *pass;
//pass = malloc(100) maybe 10000?
pass = getpwd("password: ");
Well, no...getpass() is _not_ in the standard C library at all!
(It's also obsolete -- and shouldn't be used -- but that's a different
matter.)

The first thing to do in a case like this is to RTFM!
If you had done so, you'd know that it returns a pointer to a static buffer
(meaning you don't need to -- and shouldn't -- allocate anything at all.

For future reference, anything prototyped in a header like <unistd.h> is
_not_ part of the standard C library (though it may be part of something
called `libc' on your implementation).

HTH,
--ag

BTW -- PLEASE read the faq, at:
http://www.eskimo.com/~scs/C-faq/top.html
 
E

E. Robert Tisdale

James said:
I'm writing a program that includes a call
which returns a string, namely getpass() in the standard library.

A UNIX standard library -- not an ANSI/ISO C standard library.
I was not surprised to learn that
I had improperly allocated memory to hold the return value.
I don't really know how a function returning a string works,

Type

man getpass

at your UNIX prompt.
and I have no knowledge at compile time
as to how long the string is going to be.
Should I just allocate enough memory for anything possible?

ex.
// char *pass;
// pass = malloc(100) maybe 10000?
const char* pass = getpass("password: ");

GETPASS(3) Linux Programmer’s Manual GETPASS(3)

NAME
getpass - get a password

SYNOPSIS
#include <unistd.h>

char *getpass( const char * prompt );

DESCRIPTION
This function is obsolete. Do not use it.

The getpass() function opens /dev/tty
(the controlling terminal of the process),
outputs the string prompt, turns off echoing,
reads one line (the "password"),
restores the terminal state and closes /dev/tty again.

RETURN VALUE
The function getpass returns a pointer
to a static buffer containing the (first PASS_MAX bytes of)
the password without the trailing newline, terminated by a NUL.
This buffer may be overwritten by a following call.
On error, the terminal state is restored,
errno is set appropriately, and NULL is returned.
 
A

Artie Gold

Dan said:
Why is it so difficult for people to judge the substance of a question,
rather than its form? Replace getpass() by getenv() and you have a
perfectly topical question dealing with exactly the same underlying issue:
how to handle library functions that return strings.

Um, I said that.
The answer is: if the function doesn't require a buffer (and usually its
size, too) among its arguments, then it is the function that allocates
space for the string, you don't have to allocate it yourself.

The only remaining issue is whether this space was dynamically
allocated and, therefore, it is your duty to release it after use or
whether it was statically allocated and reused every time the function
is called. In which case, you have to make a copy of the string, if you
still need it after calling the same function again. The answer is
provided by the function documentation: the Unix strdup() function is
dynamically allocating it, getenv() and ctime() use static allocation.

That too (though probably not as well).

--ag
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top