473,413 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

proper allocation

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,
--
-J. Leddy (a.k.a. iustitia)
Nov 13 '05 #1
3 2600
James Leddy wrote:
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


--
Artie Gold -- Austin, Texas

Nov 13 '05 #2
James Leddy wrote:
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.
Nov 13 '05 #3
Dan Pop wrote:
In <3F**************@austin.rr.com> Artie Gold <ar*******@austin.rr.com> writes:

James Leddy wrote:
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).

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

--
Artie Gold -- Austin, Texas

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Fao, Sean | last post by:
I'm reading The C++ Programming Language by Bjarne Stroustrup and I was unclear as to the proper way to catch an exception thrown by the new keyword. I was wondering if someobody here could let me...
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
9
by: Jon LaBadie | last post by:
Suppose I'm using stdio calls to write to a disk file. One possible error condition is no space on file system or even (in unix environment) a ulimit of 0 bytes. Which calls would be expected to...
6
by: brian | last post by:
I went through my implementation's (BSD) source code for calloc(), and if no more than one object is being allocated, the code will still execute the following if() (from...
48
by: Michel Rouzic | last post by:
I know it must sound like a newbie question, but I never really had to bother with that before, and I didn't even find an answer in the c.l.c FAQ I'd like to know what's the really proper way...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
171
by: Raman | last post by:
Hi All, Here is a small Code, int main(void) { char *p=(char *) malloc(100); strcpy(p,"Test1234567890"); p=p+10; free(p);
9
by: uidzer0 | last post by:
Hey everyone, Taken the following code; is there a "proper" or dynamic way to allocate the length of line? #include <stdio.h> #include <errno.h> int main(int argc, char **argv) { FILE *fp;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.