[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

test(void *data) vs test(void &data

Skybuck Flying

6/18/2011 8:34:00 PM

Hello,

I just came across something strange.

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.

94 Answers

Skybuck Flying

6/18/2011 8:40:00 PM

0

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

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 );

This also explains my first/original posting.

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

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...

Bye,
Skybuck.

Paul

6/18/2011 9:31:00 PM

0


"Skybuck Flying" <Windows7IsOK@DreamPC2006.com> wrote in message
news:6b599$4dfd0bc8$5419acc3$4652@cache2.tilbu1.nb.home.nl...
> Hello,
>
> I just came across something strange.
>
> 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 ?! ;)
>

A reference must always refer to an object, and you cannot have an object of
type void.
You can have a reference to a void pointer though.

void test(void* &data){ data = new double[12];}

void* p=0;
test(p);

p now points to 12x doubles .







Skybuck Flying

6/18/2011 10:11:00 PM

0

Does this mean C is more strongly typed than Pascal/Delphi ???

In other words:

Delphi is more weakly typed than C ?!?

Many people believe it's the other way around.

Yet the following example proves Delphi is more relaxed/weaker typed:

Unless something else is going on underneath ?!? Therefore I worry the
generated code/stack/parameter calling, might not be compatible ?!?

Pascal/Delphi and C/C++ are supposed to be binary compatible to a certain
extent.

So I suspect Delphi might do something different than C/C++ to make it work,
then again perhaps C/C++ is more limited.

So I am not sure what is going on underneath... I guess I will have to start
inspecting assembler instructions to see what's going on.

Even if the reference as you call/mention it points to an int in C it's
still not possible.

Delphi can do the following:

// *** Begin of Test Program ***:

program TestProgram;

{$APPTYPE CONSOLE}

uses
SysUtils;

const
value : integer = 666;
c : pointer = @value;

procedure test( var data );
begin
pointer(data) := c;
end;

procedure Main;
var
p : ^integer;
begin
test( p );
writeln( 'p^: ', p^ );
end;

begin
try
Main;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
ReadLn;
end.

// *** End of Test Program ***

Bye,
Skybuck.

Skybuck Flying

6/18/2011 10:17:00 PM

0

Equivalent C program does not compile, please check if it does not in your
compiler:

int value = 666;
void *c = &value;

void test(void *data)
{
(void *)(*data) = c;
}

int main()
{
int *p;

test( &p );

printf("*p: %d \n", *p );
}


How strange...

Bye,
Skybuck.

Skybuck Flying

6/18/2011 10:26:00 PM

0



"Paul" wrote in message news:6O8Lp.11966$w86.9277@newsfe22.ams2...


"Skybuck Flying" <Windows7IsOK@DreamPC2006.com> wrote in message
news:6b599$4dfd0bc8$5419acc3$4652@cache2.tilbu1.nb.home.nl...
> Hello,
>
> I just came across something strange.
>
> 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 ?! ;)
>

"
A reference must always refer to an object, and you cannot have an object of
type void.
You can have a reference to a void pointer though.

void test(void* &data){ data = new double[12];}

void* p=0;
test(p);

p now points to 12x doubles .
"

You should test it first, I doubt your code works.

Better yet, look at the C test program provided and try to get it working,
whatever way you can without reasonable scope of the problem/goal/delphi
equivalent.

Bye,
Skybuck.




Skybuck Flying

6/18/2011 10:26:00 PM

0



"Skybuck Flying" wrote in message
news:7c42e$4dfd25d6$5419acc3$7548@cache4.tilbu1.nb.home.nl...



"Paul" wrote in message news:6O8Lp.11966$w86.9277@newsfe22.ams2...


"Skybuck Flying" <Windows7IsOK@DreamPC2006.com> wrote in message
news:6b599$4dfd0bc8$5419acc3$4652@cache2.tilbu1.nb.home.nl...
> Hello,
>
> I just came across something strange.
>
> 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 ?! ;)
>

* correction within

"
A reference must always refer to an object, and you cannot have an object of
type void.
You can have a reference to a void pointer though.

void test(void* &data){ data = new double[12];}

void* p=0;
test(p);

p now points to 12x doubles .
"

You should test it first, I doubt your code works.

Better yet, look at the C test program provided and try to get it working,
whatever way you can within (*) reasonable scope of the problem/goal/delphi
equivalent.

Bye,
Skybuck.



Paul

6/18/2011 10:31:00 PM

0


"Skybuck Flying" <Windows7IsOK@DreamPC2006.com> wrote in message
news:79378$4dfd23e7$5419acc3$4089@cache4.tilbu1.nb.home.nl...
> Equivalent C program does not compile, please check if it does not in your
> compiler:
>
> int value = 666;
> void *c = &value;
>
> void test(void *data)
> {
> (void *)(*data) = c;
> }
This function body should be :
data = c;
You cannot dereference a void pointer.

But data would be a copy of the original pointer, to pass the pointer by
reference change it to:
void test(void* &data){
data=c;
}

>
> int main()
> {
> int *p;
>
> test( &p );
This passes the address of the pointer p, which has the type int** (pointer
to a pointer) and is not compatable with the function parameter.
Change it to:
test(p);

>
> printf("*p: %d \n", *p );
> }
>
>
> How strange...
>
HTH

Paul

6/18/2011 10:35:00 PM

0


"Skybuck Flying" <Windows7IsOK@DreamPC2006.com> wrote in message
news:d1ebe$4dfd260f$5419acc3$8020@cache4.tilbu1.nb.home.nl...
> "Skybuck Flying" <Windows7IsOK@DreamPC2006.com> wrote in message
> news:6b599$4dfd0bc8$5419acc3$4652@cache2.tilbu1.nb.home.nl...
>> Hello,
>>
>> I just came across something strange.
>>
>> 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 ?! ;)
>>
>
> * correction within
>
> "
> A reference must always refer to an object, and you cannot have an object
> of
> type void.
> You can have a reference to a void pointer though.
>
> void test(void* &data){ data = new double[12];}
>
> void* p=0;
> test(p);
>
> p now points to 12x doubles .
> "
>
> You should test it first, I doubt your code works.
>
It works ok.

Skybuck Flying

6/18/2011 10:36:00 PM

0

Ok,

Now I understand sort of... but it's a bit confusing... I guess this pretty
much proves C is a bad language.

It's all about where the space is placed.

I guess the function should be written and interpreted as follows:

1: void test( void* data )

instead of

2: void test( void *data )

Interpretation of 1: call by value passing a void pointer.

Interpretation of 2: call by reference passing a void (which is not
allowed).

One could also argue that C programmer who write 2: test is a bad programmer
;)

I shall point this out to the programmer in question ;)

Bye,
Skybuck.

Paul

6/18/2011 10:39:00 PM

0


"Skybuck Flying" <Windows7IsOK@DreamPC2006.com> wrote in message
news:8a9fa$4dfd284c$5419acc3$11814@cache4.tilbu1.nb.home.nl...
> Ok,
>
> Now I understand sort of... but it's a bit confusing... I guess this
> pretty much proves C is a bad language.
>
> It's all about where the space is placed.
>
> I guess the function should be written and interpreted as follows:
>
> 1: void test( void* data )
>
> instead of
>
> 2: void test( void *data )
>
> Interpretation of 1: call by value passing a void pointer.
>
> Interpretation of 2: call by reference passing a void (which is not
> allowed).
>
> One could also argue that C programmer who write 2: test is a bad
> programmer ;)
>
> I shall point this out to the programmer in question ;)
>
A pointer can be declared as :

int* p;
or
int *p;

Both are the same its just a case of programming style.