473,806 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Design question dealing with char* function

My question is about if I should return char* or have a char* as an
argument. My basic premise for this function is to return a char*
buffer and the size of the buffer to the caller. I know that each of
the following works but Stylistic which would be the better approach.

Here are my two examples:

char* GetBuffer(long* size);

or
void GetBuffer(char* buff, long* size);
Thanks
Danny

Nov 15 '05 #1
3 2091
fernandez....@g mail.com wrote:
My question is about if I should return char* or have a char* as an
argument. My basic premise for this function is to return a char*
buffer and the size of the buffer to the caller. I know that each of
the following works but Stylistic which would be the better approach.

Here are my two examples:

char* GetBuffer(long* size);

or
void GetBuffer(char* buff, long* size);


They are not the same, the second doesn't let you return
the buffer. Perhaps you meant "char ** buff"?

Once you are returning multiple things, my inclination would
be more like this:

int GetBuffer(char **buf_out, size_t *buf_size_out)

Where the return int is for status reporting,
such as couldn't allocate a buffer/etc. I tend to name
parameters used to return values with _out, just my style,
as it is sometimes difficult to tell at a glance
in the header file which parameters are being used for
input to the function and which for output.

A reasonable alternative, leaning more towards use of
"objects", is to return a structure containing both the
buffer and its size. Some overhead for the struct, but
then you have the buffer and its size in a nice package
you can pass around...

But this is all style, you'll get lots of opinions...

-David

Nov 15 '05 #2
On 28 Jul 2005 08:50:05 -0700, fe***********@g mail.com wrote in
comp.lang.c:
My question is about if I should return char* or have a char* as an
argument. My basic premise for this function is to return a char*
buffer and the size of the buffer to the caller. I know that each of
the following works but Stylistic which would be the better approach.

Here are my two examples:

char* GetBuffer(long* size);

or
void GetBuffer(char* buff, long* size);
Thanks
Danny


Don't use long for sizes. That's not what it is for, and it is quite
possible that on the 64-bit systems that are going to become common
soon objects can be larger than an unsigned long, let alone a signed
one.

Use size_t for the size of things, that is what it is for. There will
never be an implementation where an object will exist with a size too
large to fit into a size_t.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #3

fe***********@g mail.com wrote:
My question is about if I should return char* or have a char* as an
argument. My basic premise for this function is to return a char*
buffer and the size of the buffer to the caller. I know that each of
the following works but Stylistic which would be the better approach.

A couple of questions:

1. The caller doesn't know how big of a buffer it's requesting?

2. What determines the buffer size?
Here are my two examples:

char* GetBuffer(long* size);

or
void GetBuffer(char* buff, long* size);
Thanks
Danny


My personal preference for writing allocators is to have the thing
being allocated as the return value: i.e.,

thing *p = newThing(/* any necessary inputs here*/);

To indicate an error, I'll return NULL, and (usually) provide a second
function to get information on the cause of the error.

char *buf = newName(10);
if (!buf)
{
int errcode = newNameErr();
switch(errcode)
{
...
}
}

I *typically* do not use output parameters in the allocator function; I
try to keep those kinds of things single-valued if I can, but sometimes
an output parameter is necessary. If the multiple outputs are
*logically* connected (I'd consider a buffer and its length to be
logically connected), then I'll create a new struct type and return a
new instance of it:

typedef nameBuf {
char *buffer;
size_t length;
} nameBuf_t;

nameBuf_t *newName()
{
nameBuf_t *newBuf = malloc(sizeof *newBuf);
if (newBuf)
{
size_t newLength = ... /* however buffer length is determined
*/
newBuf->buffer = malloc(sizeof *(newBuf->buffer) * newLength);
if (newBuf->buffer)
{
newBuf->length = newLength;
/* initialize buffer contents if necessary */
}
else
{
newNameErrorSet (MALLOC_FAILURE , newLength);
free(newBuf);
newBuf = NULL;
}
}

return newBuf;
}

int main(void)
{
nameBuf_t *name;

name = newName();
if (!name)
{
fprintf(stderr, "name allocation failed: %s\n",
newNameError()) ;
...
}
return 0;
}

Nov 15 '05 #4

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

Similar topics

13
2394
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes. Two sub-classes have the relationship to communicate back and forth through this pointer. The pointer is responsible inside Top class for allocating and deallocating two sub-classes. CMemory class is responsible to allocate and deallocate memory...
16
2804
by: lovecreatesbeauty | last post by:
/* When should we worry about the unwanted chars in input stream? Can we predicate this kind of behavior and prevent it before debugging and testing? What's the guideline for dealing with it? As shown below line #21, I should remove the unwanted characters in input stream there at that time. Do I miss some other possible errors in i/o which will happen to occur sometimes in other places? And welcome your kind comments on following the...
25
2506
by: lovecreatesbeauty | last post by:
Could you talk something about the general rules on the interface (function) design in C program that recognized publically? Or introduce some good advice of yourself. How do you think about some of those advices like following? a. keep the interface clean and clear (What does clean or clear mean and how to achieve that?). b. avoid using static variables in local function if possible. c. avoid using global variables for local function...
5
7801
by: Stephen Cawood | last post by:
I'm trying to use a C++ .lib from C# (I tried the Interop group will no results). I have a working wrapper DLL (I can get back simple things like int), but I'm having issues dealing with an array of bytes. For example, the .lib contains this function: int create(int id, int scale, unsigned char *image); In the wrapper DLL I have this function:
4
1574
by: srinivasarao_moturu | last post by:
class ABC { public : virtual void operation1(); virtual void operation2(); virtual int GetValue(); virtual char GetValue(); virtual void SetValue(int); virtual void SetValue(char); }
29
2913
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
14
1939
by: Jef Driesen | last post by:
I'm writing a library (to communicate with a number of devices over a serial port) and have some questions about the design. I have now a header and source file like this: /* device.h */ typedef struct device device; int device_open (device **dev, const char *name); int device_close (device *dev); int device_read (device *dev, void *data, unsigned int size);
5
1479
by: istillshine | last post by:
Particularly for medium-sized (10,000 ~ 20,000 lines) programs, what are useful strategies to design them before coding? My strategies are: 1. Imagine what the final program would look like. Write down options. 2. Write down many structures, such as
7
1725
by: Immortal Nephi | last post by:
I have an idea how to design an object in a better way. I would like to give you my thought example. Please let me know what you think if it is best object design. Please recommend me any book which it teaches me how to design C++ OOP better as long as I know how to program OOP in C++. Think of ancient 6502 microprocessor which it was used for Commodore, Atari, and Apple II. This MPU_6502 is an object of MPU_6502 class. All member
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10364
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10370
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7649
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5545
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4328
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.