Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2011-06

void test( void *data ) to procedure test( var data ); ? is it safe ?

9 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Skybuck Flying

I had to convert "void test( void *data)" to Delphi/Pascal.

My first conversion was:

procedure test( data : pointer );

The documentation of the test function seems to indicate that this function returns all kinds of pointers, so it's like a call by reference call.

So I am starting to wonder if I perhaps should change it to something more convenient like:

procedure test( var data );

I was trying out c/c++ to see if void test( void &data ) would be valid/legal but to my surprise it was not (visual studio 2010).

So now I am wondering if it's safe to convert:

void test( void *data )

to

procedure test( var data );

I don't want no strange stack problems or access violations or anything like that, so I have to make sure it's 100% the same and safe ?!?

The convenience would be nice to have though...

My thoughts on the c/c++ inconsistency:

Prototypes for routines:

1: void test(void *data)
2: void test(void &data)

The first one does compile in visual studio 2010 and the second does not.

They both seem conceptually the same, like untyped variable data in Delphi, yet the first one is allowed and the second is not allowed.

Isn't that strange ?! ;) :)

Seems like case 2 is not yet implemented, either in the c/c++ language rules or in the compiler ?! ;)

Bye,
Skybuck.

Reply by email

Hans-Peter Diettrich
to Skybuck Flying
Show quoted text 7 lines
So now I am wondering if it's safe to convert: void test( void *data ) to procedure test( var data );

AFAIR this can cause problems, when the C code tests "data" for NULL.

I don't want no strange stack problems or access violations or anything like that, so I have to make sure it's 100% the same and safe ?!?

The safe way is using pointer types in OPL, for C "*" parameters.

Show quoted text 10 lines
The convenience would be nice to have though... My thoughts on the c/c++ inconsistency: Prototypes for routines: 1: void test(void *data) 2: void test(void &data) The first one does compile in visual studio 2010 and the second does not.

Right. C++ "&" references never can be void or NULL, while C "*" and Delphi "var" (by-ref) parameters can be untyped and Nil. That's one of the subtle differences between C++ and OPL, which should be handled properly by every converter.

DoDi

Reply by email

Ludo Brands
to Skybuck Flying: I had to convert "void test( void *data)" to Delphi/Pascal. My first conversion was:…

An "in depth" discussion of the subject can be found here: http://www.velocityreviews.com/forums/t750137-test-void-data-vs-test-void-an d-data.html

Earlier message 25 lines
-----Message d'origine----- De : De la part de Skybuck Flying Envoyé : samedi 18 juin 2011 22:44 À : FPC developers' list Objet : [fpc-devel] void test( void *data ) to procedure test( var data );? is it safe ? Prototypes for routines: 1: void test(void *data) 2: void test(void &data) The first one does compile in visual studio 2010 and the second does not. They both seem conceptually the same, like untyped variable data in Delphi, yet the first one is allowed and the second is not allowed. Isn't that strange ?! ;) :) Seems like case 2 is not yet implemented, either in the c/c++ language rules or in the compiler ?! ;) Bye, Skybuck.

Reply by email

Skybuck Flying
to Hans-Peter Diettrich: AFAIR this can cause problems, when the C code tests "data" for NULL. The safe way is…

The difference is actually:

1. Pascal has call by value and call by reference.
2. C only has call by value.

Because of limitation 2, C programmers use the pointer trick to pass the address of a variable so it can be changed inside the routine, example:

1. C call by value (default):

void test( int a )
{

  a = 5; // after routine exists, value is lost.

}

test( b );

2. C call by value (reference trick):

void test( int *a ) <- pointer to variable of integer trick. {

  *a = 5;

}

test( &b ); // <- address of variable trick.

For noobies/novice programmers this makes C a bad language since noobies/novice programmers are still learning to design software codes and will often need to change call by value to call by reference or vice versa and then it becomes a hurdle to change all these call sites, not mention confusion.

For experienced programmers it's also a reall burden to use * everywhere and & everywhere, everywhere reference comes into play.

Passing real pointers and working with them becomes even more tricky and requires even more stars/asterixes.

A very bad problem which was easily solved in pascal:

procedure test( var a : integer );
begin
  a := 5;
end;
test( a );

^ No stupid symbols needed ! ;) Much user friendly and pretty much does the same thing.

Why make things more difficult then absolutely necessary huh ?! ;) :) =D

Only thing I can imagine is very very very maybe it's easier to write a "call by value compiler" than a "call by reference compiler" ?!? But does sound a bit like bullshit to me ?! ;) :)

Bye,
Skybuck =D

Reply by email

Hans-Peter Diettrich
to Michael Schnell: IMHO a "&" in a function prototype definition in C is conceptional nonsense. -Michael
Show quoted text 7 lines
On 06/20/2011 09:55 AM, Ludo Brands wrote: > Prototypes for routines: > > 1: void test(void *data) > 2: void test(void&data) IMHO a "&" in a function prototype definition in C is conceptional nonsense.

A C compiler should report an syntax error, but Microsoft C/C++ is something different from C and C++ standards :-(

DoDi

Reply by email

Michael Schnell
to Hans-Peter Diettrich
Hans-Peter Diettrich wrote
A C compiler should report an syntax error, but Microsoft C/C++ is something different from C and C++ standards :-(

Typical for M$ :(

I just did a small java script program and this did now work correctly in InternetExplorer. I found that according to the JavaScript docs I found date.Year is based on 1900 and Firefox does work this way, while with IE date.Year is based on 0.

-Michael

Reply by email

Hans-Peter Diettrich
to Skybuck Flying: The difference is actually: 1. Pascal has call by value and call by reference. 2. C only…
Show quoted text 16 lines
Passing real pointers and working with them becomes even more tricky and requires even more stars/asterixes. A very bad problem which was easily solved in pascal: procedure test( var a : integer ); begin a := 5; end; test( a ); ^ No stupid symbols needed ! ;) Much user friendly and pretty much does the same thing. Why make things more difficult then absolutely necessary huh ?! ;) :) =D

What does above code when called with Nil?

While
   test(nil);
may result in a compiler error, the only cure would be to rewrite test as
   procedure test(a: ^integer);

But how would you do that, when the converter decided to use

   procedure test(var a: integer);

instead?

You'll have to *instruct* the converter to use the pointer form, and that for *every single* subroutine in the C code.

A converter also cannot determine from a header file, whether the implementation does something like this:

int test(int* a)
{

   return (a)?++*a:42;

}

DoDi

Reply by email