constructing robust constructors

F

frank

I'm not quite done with my latest dabbling with malloc. Assume that bar
and baz are constructors; that is, they allocate memory and do not free
it, but pass a pointer and the responsibility to caller to free it.

I seemed to have unlearned things about referencing and dereferencing
since I first read K&R (that was two concussions ago).

My first question is how do I see the return values of malloc in these
constructors?

Thanks for your comment and cheers,

dan@dan-desktop:~/source$ gcc -std=c99 -Wall -Wextra malloc2.c -o out
malloc2.c: In function ‘bar’:
malloc2.c:9: warning: initialization makes integer from pointer without a
cast
malloc2.c:10: warning: format ‘%d’ expects type ‘int’, but argument 2 has
type ‘char **’
malloc2.c:9: warning: unused variable ‘r’
dan@dan-desktop:~/source$ ./out
-1075468176
qwerty
0
dan@dan-desktop:~/source$ cat malloc2.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void
bar (char **s)
{
*s = malloc (13);
int r = &s;
printf ("%d\n", s);
}

char *
baz (void)
{
char *s = malloc (17);

return s;
}


int
main (void)
{
void bar (char **);
char *baz (void);
char *b;
int c;

bar (&b);
b = baz ();
#if 1
c = *b;
strcpy (b, "qwerty");
printf ("%s\n", b);
printf ("%d\n", c);
#endif


return 0;
}

// gcc -std=c99 -Wall -Wextra malloc2.c -o out
dan@dan-desktop:~/source$
 
F

frank

frank wrote:

[code elided]

Thx, pete.

dan@dan-desktop:~/source$ ./out
The value of b is 0x8f80008
The value of b is 0x8f80008
dan@dan-desktop:~/source$

H&S V p. 410 has
char * malloc (unsigned size);

So, my previous idea that malloc returned the number of characters
allocated or NULL on failure is completely wrong, right?
 
F

frank

frank said:
frank wrote:

My first question is how do I see the return values of malloc in these
constructors?


[code elided]

Thx, pete.

dan@dan-desktop:~/source$ ./out
The value of b is 0x8f80008
The value of b is 0x8f80008
dan@dan-desktop:~/source$

H&S V p. 410 has
char * malloc (unsigned size);

I'm guessing that your book is older than the "void" keyword.

That's somewhat my mistake. I took the ultimate number in the index for
malloc to be where I would find its proper declaration. I missed by 3
pages. The above is what H&S call "traditional." The current
declaration was three pages earlier.
N869
7.20.3.3 The malloc function
Synopsis
[#1]
So, my previous idea that malloc returned the number of characters
allocated or NULL on failure is completely wrong, right?

"NULL on failure" is correct.

malloc has to return a pointer value. Pointers are not arithmetic types;
you can't add two pointers together.

If not NULL, the value returned points to the allocated space.

ok, thx
 
K

Keith Thompson

frank said:
So, my previous idea that malloc returned the number of characters
allocated or NULL on failure is completely wrong, right?

Think about it. NULL is a pointer value; the number of characters
would have to be an integer. Furthermore, if malloc returned the
number of characters allocated, how would the caller know where
they are?
 
F

frank

Think about it. NULL is a pointer value; the number of characters would
have to be an integer. Furthermore, if malloc returned the number of
characters allocated, how would the caller know where they are?

Right. I think I conflated it with something like printf, where my
implementation returns the number of characters written.

The logic of it is then since only one thing is returned, it can't
simultaneously be the number you just handed to malloc and a pointer.

I struggle with levels of indirection when I cook from scratch.
 
K

Keith Thompson

frank said:
Right. I think I conflated it with something like printf, where my
implementation returns the number of characters written.

The logic of it is then since only one thing is returned, it can't
simultaneously be the number you just handed to malloc and a pointer.

I struggle with levels of indirection when I cook from scratch.

The solution to your struggle is to read the documentation. If you're
unsure of what malloc() does, "man malloc" or the equivalent on your
system (or consulting a reference work of some sort) should be a
prerequisite to attempting to use it.

The problem sometimes is when you *think* you know what it does.
Recognizing when you need to look something up can be non-trivial.
For that, experience is probably the only solution.
 
F

frank

The solution to your struggle is to read the documentation. If you're
unsure of what malloc() does, "man malloc" or the equivalent on your
system (or consulting a reference work of some sort) should be a
prerequisite to attempting to use it.

I did try that.

dan@dan-desktop:~/source$ man malloc
No manual entry for malloc
dan@dan-desktop:~/source$
The problem sometimes is when you *think* you know what it does.
Recognizing when you need to look something up can be non-trivial. For
that, experience is probably the only solution.

I've started to realize that googling once for your subject line is a
good idea before you post. The darndest things turn up. Especially
among linux users the attitude is that it's all out there, and you just
have to find it.

I spend a lot of time with my nose in C reference. What I find necessary
is referent source, like pete posted. The other nice thing about when
pete posts is that you know it's right on the money, so you can take it
to the bank. I'm still waiting for 2 titles in C:
The big book of examples for everything in C
The Essential Knuth in C

I also need places to "talk" about C. Usenet helps immensely.
 
K

Keith Thompson

frank said:
I did try that.

dan@dan-desktop:~/source$ man malloc
No manual entry for malloc
dan@dan-desktop:~/source$

<OT>
You should be able to install the man pages somehow. (On my system,
Ubuntu, the malloc man page is installed by the "manpages-dev"
package; on Red Hat, the package name is "manpages".
I've started to realize that googling once for your subject line is a
good idea before you post. The darndest things turn up. Especially
among linux users the attitude is that it's all out there, and you just
have to find it.

Unfortunately, a lot of what's out there is crud. But there are some
reliable resources (discussed already).

[...]
 
M

Michael Tsang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I'm not quite done with my latest dabbling with malloc. Assume that bar
and baz are constructors; that is, they allocate memory and do not free
it, but pass a pointer and the responsibility to caller to free it.

Sorry, C has no constructors. Maybe you need to ask in comp.lang.c++ ?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAksPxBgACgkQG6NzcAXitM8f0ACdEfH2Zv7TUTBmq0jhu3DCI/wJ
510AoIeRsGq1Cc8+HXU7RvI92pTnmCQ+
=ReLg
-----END PGP SIGNATURE-----
 
R

Richard Tobin

I'm not quite done with my latest dabbling with malloc. Assume that bar
and baz are constructors; that is, they allocate memory and do not free
it, but pass a pointer and the responsibility to caller to free it.
[/QUOTE]
Sorry, C has no constructors. Maybe you need to ask in comp.lang.c++ ?

He explained perfectly clearly what he meant by constructors. Are
people discussing C not allowed to use general computer science terms
just because C++ uses them?

-- Richard
 
K

Kenny McCormack

Are you being purposely dense or just joking?

foo and baz were constructors (ridiculous and meaningless though those
names are).

The term constructor is not limited to c++. The c++ implementation of
constructors is just one such. Try not to be so blinkered.

But you do have to admit that the line posted above *is* the required
CLC behavior. I.e., it is required behavior (i.e., a shibboleth) that
whenever anyone posts something that uses any of the "dirty words" - and
by this I mean pretty much anything that is relevant to C++ (and not C),
plus a few others, such as "stack" (OMG! OMG! OMG! - I said the "S
word" in public!!! What *will* become of me???), "heap", and probably a
few others that I can't think of at the moment - that someone come along
along and state that "C has no X". Even though anyone with a lick of
sense can see that it is perfectly proper to discuss them and that no
reference to C++ is needed.

The problem really is that C has this huge inferiority complex nowadays,
since the current implementations of C are C++, C#, and others. I.e.,
although it is a dirty secret, the fact is that no one is really using C
for new work these days - outside of some embedded areas, and that's not
going to last for long - so, naturally C (and its enthusiasts) have
developed this inferiority complex as a defense mechanism.

As I've stated in other posts, I used to be like that myself. I know
what I'm talking about. But I have grown up.
 
K

Kaz Kylheku

Sorry, C has no constructors. Maybe you need to ask in comp.lang.c++ ?

Languages other than C++ have constructors. In the history of computing,
the term has been used for ordinary functions, which create an object
or a value.

The Lips function ``cons'' is named after construction; it just allocates a
pair-like object and initializes its two fields from the two arguments
supplied.

In a Unix-like kernel, a MAKEDEV(MAJOR, MINOR) macro that turns a device major
and minor number into one device ID number can be regarded as a constructor.

The C fopen function can be called a constructor for a stream object.

Etc.
 
M

Michael Tsang

Sorry, C has no constructors. Maybe you need to ask in comp.lang.c++ ?

He explained perfectly clearly what he meant by constructors. Are
people discussing C not allowed to use general computer science terms
just because C++ uses them?

-- Richard[/QUOTE]

What I know about a constructor is a function that is called automatically
when you construct an object. C++ has constructors, Java has constructors,
PHP has constructors, but C does not.
 
R

Richard Tobin

Michael Tsang said:
What I know about a constructor is a function that is called automatically
when you construct an object. C++ has constructors, Java has constructors,
PHP has constructors, but C does not.

However, the OP's program does, and he explained what they were. If
someone has a problem with a function that does matrix inversion, do
you say "C doesn't have matrix inversion"?

-- Richard
 
F

Frank

Languages other than C++ have constructors. In the history of computing,
the term has been used for ordinary functions, which create an object
or a value.

The Lips function ``cons'' is named after construction; it just allocates a
pair-like object and initializes its two fields from the two arguments
supplied.

In a Unix-like kernel, a MAKEDEV(MAJOR, MINOR) macro that turns a device major
and minor number into one device ID number can be regarded as a constructor.

The C fopen function can be called a constructor for a stream object.

Interesting examples. I'm wading out further into unix world and find it
fascinating.

Yeah, I don't want to get all flamy regarding the obvious differences in a
syntax when borrowing a term common among the OO people. Pete got me
straightened out pretty quick. I think it's a useful term, because then
you're not leaking memory when you pass back the pointer having not
free()ed.
 
A

Antoninus Twink

However, the OP's program does, and he explained what they were. If
someone has a problem with a function that does matrix inversion, do
you say "C doesn't have matrix inversion"?

Be careful, Richard.

This sort of common sense is dangerously close to heresy in clc.
 

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

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top