void *malloc(size_t num_bytes);

C

CBFalconer

forayer said:
mate, i apologized once, and apologized to everyone in that message.
now what do you guys want me to do? say sorry for each and every
post i made in which i have used the abbreviations??? come on...

Pointing up the fact that various readers don't see the same
articles or sequence that you or other readers see. Apologies are
not needed one by one, just don't do it in future. And please also
use an uppercase I for the first person singular, it is also much
easier to read.

Chris Torek has covered the other points in his usual admirable
fashion.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
C

CBFalconer

forayer said:
all i meant to say was that a 'void *' can _sometimes_ be a
pointer to nothing, but not always as Tim said it to be. :)

You will very rarely declare anything of type void* (apart from
function parameters and possibly return type). In fact I can't
think of any such need for now.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
F

forayer

CBFalconer said:
Pointing up the fact that various readers don't see the same
articles or sequence that you or other readers see. Apologies are
not needed one by one, just don't do it in future. And please also
use an uppercase I for the first person singular, it is also much
easier to read.

I will keep that in mind too. Please ignore the posts I just made with
the lowercase 'i'. I did it before I read your post. Sorry.

Cheers,
forayer
 
J

john_bode

Alawna said:
so let us suppose that malloc returns an integer then its prototype
would be int void *malloc(-----); . is this accurate?

No. malloc() returns pointer to void -- period. It does not return an
integer type. The resulting pointer value can be *cast* to an integer
value, although whether that resulting integer is useful or not is an
open issue.
 
C

Chris Dollin

Alawna said:
so let us suppose that malloc returns an integer then its prototype
would be int void *malloc(-----); . is this accurate?

It's syntactically illegal; accuracy isn't applicable.
 
L

Lawrence Kirby

forayer said:
Tim said:
[snip]

A "void *" means roughly "a pointer to something".

So "pointer to nothing" means roughly "a pointer to something"?!??

no, 'void *' means a pointer to any kind of thing! but it definitely
points to something, though this "something" can even be NULL!!!

A NULL pointer isn't a pointer to anything.

Yes, a null pointer is a pointer that doesn't point.
A 'void *' is a pointer to nothing - just dereference it
to get that void value. :)

A void * pointer can point at an object, you just don't know what type of
object it is pointing at. Whether a non-null void * pointer value is valid
or not depends on whether it is pointing at an object or one place past
the end of an array.
Speaking of which, why isn't

void
foo(){
void *p = ...whatever...;

return *p;
}

allowed?

It's all just humor, don't take it too seriously....

Sense prevailed.

Lawrence
 
K

Keith Thompson

forayer said:
mate, i apologized once, and apologized to everyone in that message.
now what do you guys want me to do? say sorry for each and every post i
made in which i have used the abbreviations??? come on...

Usenet is an asynchronous medium. Articles do not necessarily appear
in the order in which they were posted (assuming they appear at all),
and individuals may read articles in any arbitrary order. (That's why
it's so important to quote properly (a general comment, not meant to
imply that you haven't.))

I'll take your word for it that you've apologized elsewhere in this
thread, but I haven't seen that article yet, and I presume Flash
Gordon hadn't seen it either. (It's probably on my server, I just
haven't gotten to it yet.)

You should see what happens in comp.lang.c.moderated. Articles don't
appear until they've been cleared by the moderator. A single question
can be followed days or weeks later by a dozen similar responses, all
written by people who've seen only the original question.

[...]
I agree. seg faults are specific to some OSs. But if p=NULL, and then
you do a p->func(), no matter what the OS, it is a runtime error, dont
you think?

Any attempt to dereference a null pointer invokes undefined behavior.
There is no requirement for the implementation to detect the error.
In many cases, undefined behavior results in the program behaving just
as you expect it to, failing only at a critical moment during a
demonstration for an important customer. The old joke around here is
that one of the allowed consequences of undefined behavior is that it
causes demons to fly out of your nose.
 
K

Keith Thompson

CBFalconer said:
Pointing up the fact that various readers don't see the same
articles or sequence that you or other readers see. Apologies are
not needed one by one, just don't do it in future.

The fact that I made the same point was just my clever way of
demonstrating the phenomenon, not an indication that I was too lazy to
read the entire thread before posting redundant followups. Yeah,
that's it.
 
F

forayer

Keith said:
Usenet is an asynchronous medium. Articles do not necessarily appear
in the order in which they were posted (assuming they appear at all),
and individuals may read articles in any arbitrary order. (That's why
it's so important to quote properly (a general comment, not meant to
imply that you haven't.))

I'll take your word for it that you've apologized elsewhere in this
thread, but I haven't seen that article yet, and I presume Flash
Gordon hadn't seen it either. (It's probably on my server, I just
haven't gotten to it yet.)
That explains a lot. My apology letter is in another thread, and you
participated in it only recently (Flash Gordon has not yet participated
in it). Anyway, I'm sorry you guys.
Any attempt to dereference a null pointer invokes undefined behavior.
There is no requirement for the implementation to detect the error.
In many cases, undefined behavior results in the program behaving just
as you expect it to, failing only at a critical moment during a
demonstration for an important customer. The old joke around here is
that one of the allowed consequences of undefined behavior is that it
causes demons to fly out of your nose.
Ouch!
Can you tell me which implementation uses such function calls as
recursive main() calls (Chris Torek said so) ?

cheers,
forayer
 
K

Keith Thompson

forayer said:
Can you tell me which implementation uses such function calls as
recursive main() calls (Chris Torek said so) ?

It's not a matter of "which implementation". C allows main to be
called recursively. It hardly ever makes sense to do so.
 
C

Chris Torek

Chris said:
#include <stdio.h>
struct S { char a[1]; };
int main(void) {
struct T { struct S { char a[1000]; } a; };
printf("sizeof(struct S) = %lu\n", (unsigned long)sizeof(struct S));
return 0;
}
(Exercise: what is the most likely output of the above, as both
C and C++? Why is it different?)

1000 using C, and 1 using C++. My money is on scope-resolution
differences between the languages. Please correct me if I am wrong.

Got it in one. Of course, neither 1 nor 1000 are guaranteed
(because the structures may include padding), but they are at
least extremely likely to be different. :)
... There are machines on which [(*f)(), where f==NULL,] turns
out to be a recursive call to main().
I really didn't know that, thank you. :)

I saw in another thread that you were wondering where this was the
case. The case I was thinking of in particular occurred on the
VAX running 4.xBSD, but it could occur on any similar machine.
The most basic requirement is that the NULL function pointer must
point to executable code. This was true since NULL on that machine
was 32-zero-bits, and 4BSD had the program starting on page zero
(VMS, by contrast, made page-zero off limits by default, so that
*(char *)0 got a runtime fault, and attempts to call through NULL
also faulted).

The second tricky bit was that location 0 began with a 16-bit
register mask, even though the kernel launched executables by
pointing the program counter to the startup code rather than
executing the VAX's normal subroutine-call instruction, "CALLS".
(The startup code was thus at address 2. As I recall, 0x00 is a
VAX "HALT" instruction, so the two zero bytes at location zero
would have caused trouble if the binary were launched at address
0. However, having the register-save-mask there enables one to
use the VAX's CALLS instruction.)

The final tricky bit was that this recursive call to main()
would only work if the registers were set up properly. I have
forgotten what the register setup was supposed to be, and just
how tolerant this all was (whether by design or coincidence).
I do, however, remember having one program blow up with a stack
overflow, and in the debugger, seeing a seemingly-infinite
sequence of calls starting with the startup code calling main()
which called some functions which eventually called through the
NULL function-pointer which called main(), etc.

One could get the same effect on other machines, for the same
reasons.
 
R

Rajan

Alawna,
though void does not return anything , void* meaning it returns an
address, which can then be cast to any data-type as you may know.
 
F

forayer

Keith said:
It's not a matter of "which implementation". C allows main to be
called recursively. It hardly ever makes sense to do so.
I was not exactly asking if main() could be called recursively. What I
was asking for was where calls to NULL function pointers would be
treated as recursive main() calls. Chris Torek has given a very good
explantion for this (it has appeared in my news reader, but not yet on
google groups!). As far as making sense for recursive main() calls go,
you should take a look at the winning entry for International
Obfuscated C Code Contest (IOCCC).

P.S.: You're completely right, it makes no sense to use it in any
useful program, but hell, its fun to do this kind of stuff. (I haven't
qouted the code here, not sure if it has already been posted or not. I
will post it if need be)

cheers,
forayer
 
B

Ben Bacarisse

Chris said:
... There are machines on which [(*f)(), where f==NULL,] turns
out to be a recursive call to main().
I really didn't know that, thank you. :)

I saw in another thread that you were wondering where this was the case.
The case I was thinking of in particular occurred on the VAX running
4.xBSD

If IIRC something similar happened on the ill-fated Perq (a lovely
personal workstation from Three Rivers later aquired by ICL in the UK).
The microcode designers borrowed an idea from ICL mainframes in which each
function was placed in its own page so the top 16 bits of a code address
identified the function and the bottom 16 the offset within it. This made
decoding register dumps very easy.

<aside>It also had the curious property of being word addressed (though,
for compiling C there were, of course, byte access instructions). This
put it in the now very small category of machines in which the cast in
code like:

int x;
char *cp = (char *)&x;

was not just "to keep the compiler happy" (as some people still think) but
was essential since it generated code to convert from one pointer type to
another. I particularly remember the sort program. It was littered with
un-cast pointer conversions and was a pain to port.</aside>
 
L

Lawrence Kirby

On Thu, 23 Jun 2005 14:29:22 +0100, Ben Bacarisse wrote:

....
int x;
char *cp = (char *)&x;

was not just "to keep the compiler happy" (as some people still think) but
was essential since it generated code to convert from one pointer type to
another. I particularly remember the sort program. It was littered with
un-cast pointer conversions and was a pain to port.</aside>

What do you mean by an "un-cast pointer conversion"? Most pointer
conversions in C like the one above require a cast, the compiler is
required to issue a diagnostic if the cast is missing. For those
conversions where a cast is not necessary the effect is identical to using
a cast. For example

void *vp = &x;

and

int *a = malloc(100 * sizeof *a);

work fine when int * and void * have different representations.

Or are you referring to some sort of representation punning using pointers
to pointers?

Lawrence
 

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,166
Messages
2,570,902
Members
47,442
Latest member
KevinLocki

Latest Threads

Top