OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 2:01 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Equivalent to the "Variant" Data Type of Visual Basic in C
PostPosted: Tue Jul 04, 2017 10:02 pm 
Offline
Member
Member
User avatar

Joined: Tue Mar 06, 2007 11:17 am
Posts: 1225
I was thinking, why not use massive type casting for variables in C and declare the variables with a generic data type which is as big as to contain a pointer or the full register width for the target machine?

For example, define a variable
Code:
uintmax_t some_variant_variable;




And then use them for many data types with type casting as if it could hold any data type (actually it seems it can):
Code:
//Pairs of variable update and printing it
//in its raw form:
///
(double)some_variant_variable=(double)0;
printf("%d",(uint16_t)some_variant_variable);

(double)some_variant_variable++;
printf("%d",(uint16_t)some_variant_variable);

(double)some_variant_variable++;
printf("%d",(uint16_t)some_variant_variable);




I think that it could be done more than we could think in production code (declaring a variable that is big enough and using it as a variable that could hold any data type, via massively explicit type casting), and probably that's why there are so many complex warnings when compiling the different open source projects.

_________________
Live PC 1: Image Live PC 2: Image

YouTube:
http://youtube.com/@AltComp126/streams
http://youtube.com/@proyectos/streams

http://master.dl.sourceforge.net/projec ... 7z?viasf=1


Top
 Profile  
 
 Post subject: Re: Equivalent to the "Variant" Data Type of Visual Basic in
PostPosted: Tue Jul 04, 2017 11:27 pm 
Offline
Member
Member

Joined: Tue Mar 04, 2014 5:27 am
Posts: 1108
There are several problems here.

Casting between an integer type and a floating-point type won't work the way you want (or think it works). You'll have to do memcpy() from one to another.

The compiler will not always be able to eliminate the introduced overhead.

The compiler will not be able to tell you about something silly in such code, e.g. using a float as an int or a pointer.

Is there a real problem you're trying to solve or you think you have a magic solution to all problems?

Btw, in C there's a limited way of attaching information to types (much like C++ type traits), which can be examined. See Type Traits In C (there's no demo code to play with, but the doc gives you bits from which you can construct yours).


Top
 Profile  
 
 Post subject: Re: Equivalent to the "Variant" Data Type of Visual Basic in
PostPosted: Wed Jul 05, 2017 1:25 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7612
Location: Germany
alexfru wrote:
Is there a real problem you're trying to solve or you think you have a magic solution to all problems?


This is the core issue here. Having a "magic" variant type is very seldom really required, and brings a lot of problems of its own.

Perhaps having a look at the introduction to Boost.Any will be enlightening. Judging from your code snippet, it seems you are looking for...

Quote:
Indiscriminate types that can refer to anything but are oblivious to the actual underlying type, entrusting all forms of access and interpretation to the programmer. This niche is dominated by void *, which offers plenty of scope for surprising, undefined behavior.

_________________
Every good solution is obvious once you've found it.


Top
 Profile  
 
 Post subject: Re: Equivalent to the "Variant" Data Type of Visual Basic in
PostPosted: Wed Jul 05, 2017 1:39 pm 
Offline
Member
Member
User avatar

Joined: Thu Jul 12, 2012 7:29 am
Posts: 723
Location: Tallinn, Estonia
Visual Basic's VARIANT is a discriminated type = you know what's inside.

The closest analogue is a discriminated union in C.

_________________
Learn to read.


Top
 Profile  
 
 Post subject: Re: Equivalent to the "Variant" Data Type of Visual Basic in
PostPosted: Wed Jul 05, 2017 4:36 pm 
Offline
Member
Member
User avatar

Joined: Tue Mar 06, 2007 11:17 am
Posts: 1225
But an union would need to contain all possible C types for it to be a truly raw variant data type.

It would probably be better to ensure to declare a variable that just contains untyped binary data and has the maximum register width, that then can be interpreted directly in any way possible using verbose type casting every time we want to use it as certain data type.

In this way we could do things like calculating floating point values and then inspecting the binary pattern of the value, using only type casts on the very same variable data.

_________________
Live PC 1: Image Live PC 2: Image

YouTube:
http://youtube.com/@AltComp126/streams
http://youtube.com/@proyectos/streams

http://master.dl.sourceforge.net/projec ... 7z?viasf=1


Top
 Profile  
 
 Post subject: Re: Equivalent to the "Variant" Data Type of Visual Basic in
PostPosted: Wed Jul 05, 2017 11:51 pm 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7612
Location: Germany
Only the conversion to / from an array of unsigned char is well-defined.

That means, you might manage to get a C implementation going for a specific platform / compiler, but nothing portable.

And you still haven't answered the core question:

What for?

_________________
Every good solution is obvious once you've found it.


Top
 Profile  
 
 Post subject: Re: Equivalent to the "Variant" Data Type of Visual Basic in
PostPosted: Thu Jul 06, 2017 1:01 am 
Offline
Member
Member
User avatar

Joined: Thu Jul 12, 2012 7:29 am
Posts: 723
Location: Tallinn, Estonia
~ wrote:
But an union would need to contain all possible C types for it to be a truly raw variant data type.


Yes, and with a limitation that if you initialise the union with

Code:
u.type = INT;
u.intVal = 5;

you cannot portably do later

Code:
myDoubleVar = u.doubleVal;

Same with std::variant in c++ - if you put an int in, you cannot take a double out.

_________________
Learn to read.


Top
 Profile  
 
 Post subject: Re: Equivalent to the "Variant" Data Type of Visual Basic in
PostPosted: Thu Jul 06, 2017 3:50 am 
Offline
Member
Member
User avatar

Joined: Tue Mar 06, 2007 11:17 am
Posts: 1225
dozniak wrote:
~ wrote:
But an union would need to contain all possible C types for it to be a truly raw variant data type.


Yes, and with a limitation that if you initialise the union with

Code:
u.type = INT;
u.intVal = 5;

you cannot portably do later

Code:
myDoubleVar = u.doubleVal;

Same with std::variant in c++ - if you put an int in, you cannot take a double out.
It's good to know. With this, I think that now it's clear that the portability of C is really limited. But we could make a list of cases where we state that a way of doing things is portable to which architectures. That would probably the best if we are to handle such low level details. At least I think that if it works in an x86 CPU, it will work in any other of the same kind or probably all of them, and if so, we can use the same code, and if it's a detail that works the same in any CPU, then we can point out how portable it is.


Solar wrote:
Only the conversion to / from an array of unsigned char is well-defined.

That means, you might manage to get a C implementation going for a specific platform / compiler, but nothing portable.

And you still haven't answered the core question:

What for?
To have a variant type that can be used as any data type solely by using type casts so that it allows an unrestricted low level access of the byte data for variables for easy manipulation and display, like assembly but without having to use it.

_________________
Live PC 1: Image Live PC 2: Image

YouTube:
http://youtube.com/@AltComp126/streams
http://youtube.com/@proyectos/streams

http://master.dl.sourceforge.net/projec ... 7z?viasf=1


Top
 Profile  
 
 Post subject: Re: Equivalent to the "Variant" Data Type of Visual Basic in
PostPosted: Thu Jul 06, 2017 4:02 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7612
Location: Germany
Yes, but what for? What is your use case, other than "doing things more like VB or some scripting language than C, while still pretending to do C"?

"Besides, the determined Real Programmer can write Fortran programs in any language." -- Ed Post, Tektronix, 1983

_________________
Every good solution is obvious once you've found it.


Top
 Profile  
 
 Post subject: Re: Equivalent to the "Variant" Data Type of Visual Basic in
PostPosted: Thu Jul 06, 2017 4:11 am 
Offline
Member
Member
User avatar

Joined: Thu Jul 12, 2012 7:29 am
Posts: 723
Location: Tallinn, Estonia
~ wrote:
It's good to know. With this, I think that now it's clear that the portability of C is really limited.


Only to those who do not know C.

With that, I'll return to ignoring you - that's been more productive in the past three years.

_________________
Learn to read.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 30 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group