POV-Ray : Newsgroups : povray.off-topic : Error mesage : Re: Error mesage Server Time
7 Sep 2024 19:15:11 EDT (-0400)
  Re: Error mesage  
From: Warp
Date: 2 May 2008 05:42:39
Message: <481ae20e@news.povray.org>
Invisible <voi### [at] devnull> wrote:
> I don't really know C++, but from what little I've seen printing *looks* 
> safer...

  It's more modular too. For example, assume you have abstracted away
a type like this:

typedef int MyType;

  And somewhere else in the code you have something like:

MyType value = something;
printf("The value is: %i\n", value);

  That 'printf' assumes the type is an integer. What happens if you later
change the type? For example like:

typedef double MyType;

  What happens is that the printf breaks horribly and will print gibberish.

  That won't happen using the C++ style:

MyType value = something;
std::cout << "The value is: " << value << "\n";

  Now if the value type changes the output will still be correct.

> OOC, how does C do argument passing? [I'm puzzled as to why it's 
> possible for a function with variable arguments to exist.]

  The C standard (and consequently the C++ standard) defines a special
mechanism for passing a variable amount of arguments to a function.
Simply put, you can declare a function like:

void foo(...);

and then you can call it like:

foo(any, amount, of, arguments, you, want);

  Of course C being C, there's no way for the function to know how many
arguments were passed and what was their type. printf() solves this with
the format string given as first parameter. (And its problem is, of course,
that it simply trusts that the amount and type of the arguments corresponds
to the given format string.)

  The C standard defines special macros which can be used inside foo() to
retrieve the given arguments.

  From the compiler's point of view the implementation of this is rather
simple: All the function parameters are pushed onto the stack, and then
the foo() function simply pops them from the stack. But as I said, there's
no type checking nor anything. It's all just "raw". (But it's not like
there wouldn't be *any* advantage at all in this: The advantage is, of
course, that it's quite fast and memory-efficient to do it this way.)

-- 
                                                          - Warp


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.