Link to home
Get AccessLog in
Avatar of Unimatrix_001
Unimatrix_001Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Native alternative to 'size_t'.

tellg from a ifstream object returns the variable as a type of 'size_t'. What is size_t, and is there an equivalent 'native' data type without possibly truncating any data?
SOLUTION
Avatar of lucky_james
lucky_james
Flag of India image

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
it is defined somewhat as:

#ifndef __SIZE_T
#define __SIZE_T
typedef unsigned int size_t;
#endif

check out:
http://www.thescripts.com/forum/thread220206.html
>> so, u can use 'unsigned int' in place of size_t.

Usually, yes, but not on all platforms ... Using size_t is safer, and more portable.
>> so, u can use 'unsigned int' in place of size_t.

size_t might be defined as unsigned long, or even something else.
Hi Unimatrix_001,

size_t is often an integer, but you can't count on it.

At some point, the developers of C realized that the different word sizes and characteristics of different hardware architectures made moving code from one platform to another very difficult.  A 32-bit integer is a 'long long' on a 16-bit system, and an 'int' on a 32-bit system.  I've even used a 64-bit system where a 'short' was a 32-bit integer and there was no 16-bit native entity.

Your code might not need transportability to another platform today.  But who knows about tomorrow?

Go ahead and use the size_t object.  The C people got this one right.



Good Luck,
Kent
practically, i dont feel u need to use any larger alternative.
but if u really want to use then u can use unsigned form of any bigger data type.
eg. unsigned long
>> practically, i dont feel u need to use any larger alternative.
>> but if u really want to use then u can use unsigned form of any bigger data type.
>> eg. unsigned long

But why ? Why not just use size_t ... then you're sure you've got the right type.
>>. then you're sure you've got the right type.
i have already emphasized on use of size_t........thats why i wrote the sentence "practically, i dont feel..."

but for the sake of knowledge, i wanted to let the user know.
No, you said :

>> so, u can use 'unsigned int' in place of size_t.

which is not always true, and does not answer the asker's question :

>> and is there an equivalent 'native' data type without possibly truncating any data?

because when you are using unsigned int, you might be truncating ...
SOLUTION
Link to home
membership
This content is only available to members.
To access this content, you must be a member of Experts Exchange.
Get Access
>>which is not always true, and does not answer the asker's question :
i know, that why i made another comment, where i asked him to check what is the definition in the header files.
Please see all my comments in a cumulative fashion.
Avatar of Unimatrix_001

ASKER

I'm running XP 32-bit, but I'm able to operate with files greater than 2^32 bytes, thing is, if I use an unsigned int, this won't operate with files larger than 4GB, so size_t, cannot  be typedef unsigned int?
>> >> tellg from a ifstream object returns the variable as a type of 'size_t'.
>> No it doesn't! It returns a pos_type.

Heh ... somehow I missed that part of the question lol

Unimatrix_001, was the question about size_t, like I assumed, or about the type returned by tellg ?


>> i know, that why i made another comment, where i asked him to check what is the definition in the header files.
>> Please see all my comments in a cumulative fashion.

I did, and it doesn't matter ... It's still not correct. It's not because your compiler on your system uses unsigned int, that another compiler on another system will. If you want your code to be portable, just use size_t.
>>Sorry, but the knowledge you've provided is misleading!
check out my comment where i described that it is defined in a cpp file.
http://www.thescripts.com/forum/thread220206.html

you can use false and FALSE interchangeably (agree if it is not the getting on portabiliity), but that is the information only. He asked for the nearest common native data type. I i provided that. It does not seems to be misleading at all!!.

Best Regards,
James
exilrix: It seems I'm incorrect about tellg returning a size_t, it is indeed a pos_type, although my above comment still poses a problem...?
>> I'm running XP 32-bit, but I'm able to operate with files greater than 2^32 bytes, thing is, if I use an unsigned int, this won't operate with files larger than 4GB, so size_t, cannot  be typedef unsigned int?

First of all, size_t says nothing about file sizes, but about memory block sizes. So, back o my earlier question :

>> Unimatrix_001, was the question about size_t, like I assumed, or about the type returned by tellg ?
Infinity: Perhaps I should have been a bit more broad in my question - the issue is that I'm able to work with files greater than 4GB even on a 32-bit system, but without using the C types of 'long long' (see http://home.att.net/~jackklein/c/inttypes.html#long_long), how can this be accomplished? And since pos_type is as evilrix says 32-bits (on my 32-bit system), how can I even begin to get a pointer to a memory location of a file if it is greater than 4GB?
>> check out my comment where i described that it is defined in a cpp file.
Um, why?

>> you can use false and FALSE interchangeably (agree if it is not the getting on portabiliity),
Um, false and 0 (FALSE) are NOT the same thing! 0 is an integer and false is a bool - they are two different types! There is an implicit conversion between them but they are NOT the same type and some compilers will warn you if you try to use them interchangeably because it can interfere with compiler optimization!

@Unimatrix_001

This you can check on right click on SIZE_T and click on go to definition.
It is defined in
On my system (its 32-bit), its
typedef ULONG_PTR SIZE_T, *PSIZE_T;

On my systyem, i found its definition in "plateformsdk\include\basetsd.h"
That means its "unsigned long".

I told, you can always check on ur system.

James.
>>Um, false and 0 (FALSE) are NOT the same thing!
did i say they are the same??....i know its only the work around.

Best Regards,
James
>> And since pos_type is as evilrix says 32-bits
Evilrix did NOT say that at all. Read my post again! I stated size_t is a 32bit type on 32-bit system. pos_type is a opaque type that will be the max size file a ifstream can handle on your platform.
>> did i say they are the same??....i know its only the work around.
You implied they are interchangeable -- they are not! They are implicitly convertible, this is not the same this. Conversion comes with overhead!
>> how can I even begin to get a pointer to a memory location of a file if it is greater than 4GB?

Not by using the standard streams. You'll have to have large file support, which is pretty much platform dependent.
>> you can always check on ur system.
Bu the point is you shouldn't! This is what I8 and I are trying to say. The type size_t is size_t and that's it. It dosen't matter what it is defined from on your platform as it is a platform dependent type. If you need size_t use size_t and nothing else! What is there to be gained by using the wrong type apart from non-portability?
>> Not by using the standard streams. You'll have to have large file support, which is pretty much platform dependent.
Correctamundo!
>>You implied they are interchangeable
I said the CAN BE USED.

secondly,
>>Conversion comes with overhead!
Work arounds  dont guarantee to prevent any overhead.
>> Work arounds  dont guarantee to prevent any overhead.
It's not a workaround -- it's bad code! A work around is when you have to do something you can't avoid! You can avoid this, thus: -

BOOL B = FALSE;
bool b = (B == TRUE);

Now there is no implicit conversion and the compiler will optimize it's little socks off!
>>Bu the point is you shouldn't!
did the user say he has to port the software??

>>he simply asked for the nearest native type.
size_t is NOT a native type. so i just gave him a way to find out.

>>Bu the point is you shouldn't!
its upto user to decide. I have given my advice to use it as it is. but on the other way, i have given him what he asked for.
Where did the talk about FALSE and TRUE come from lol ... I thought we were discussing size_t and large file support heh ;)
>> it's bad code!
Agreed, it is!!
But try to find out the meaning of work around.
I means you are not following the straght away solution. Be that solution exists or not.

sorry, infinity..... i think we two have got distracted from the main topic. :)
>> size_t is NOT a native type
The type is defined by the C++ standard so in that respect it is considered a native type. How the type is actually define is irrelevant -- this is the whole point of this debate!
>>this is the whole point of this debate!
it is being used by typedef. there is no other dimension to evaluate it as a native type.

You provide an SDK and provide another data type using typedef.
would that be a c++ native data type.....?? lol.
its the windows SDK data type......not a native one.
>> it is being used by typedef. there is no other dimension to evaluate it as a native type
I think you care confusing native and intrinsic!

Native type: Natively belonging to C++
Intrinsic type: A pure C++ type


Hm, I'll assume we're all done here now... :) Points split 300 Infinity, 150 evilrix and 50 lucky_james.

Thanks everybody.
Uni
>> Hm, I'll assume we're all done here now... :) Points split 300 Infinity, 150 evilrix and 50 lucky_james
Yeah, I'm bored of being right :)

Cheers Uni.
Hehe, thanks for participating. :)
>> Hm, I'll assume we're all done here now... :)
Yeah, I too am bored of explaining the meanings.

Good Luck Uni
>> Yeah, I too am bored of explaining the meanings.
The English was provided for free :)
>>The English was provided for free :)
true.No points awarded for my labor to explain English :)
>> No points awarded for my labor to explain English
I have 36 years as a native English speaker and a dictionary for that!

Native: Adj. Describes something that belongs to somewhere or something from its inception
- this describes size_t as it has been part of C++ from its [size_t] inception
size_t is native to C++

Intrinsic: Adj. Describes something which is wholly independent of any other object (the opposite of extrinsic)
- this does not describe size_t as it is dependent on being a typedef of an appropriate type
size_t is not an intrinsic type in C++
>>I have 36 years as a native English speaker and a dictionary for that!
gud you have enough resources to get ahead. Congrats man !!

I can also give certain definitions (though i know its a common word in dictionary!!) :
A workaround is typically a temporary fix that implies that a genuine solution to the problem is needed.


Look if you want i can also argue to the limit you want.
Lets end this exchange of fire here.

As a fellow, I respect you. Would request you to Concentrate on what client asks, not on cutting out each others' arguments. If yours solution would be gud, each would say a thanks (including ur fellows) for enhancing their knowledge......

Enjoi ur Weekend!!

James
>> Would request you to Concentrate on what client asks, not on cutting out each others' arguments
I am not arguing. The advice you gave was misleading, it's as simple as that!

>> A workaround is typically a temporary fix that implies that a genuine solution to the problem is needed.
There was no problem, therefore the phrase 'workaround' used in the context you used it was not applicable!

>> Lets end this exchange of fire here.
There is no exchange of fire, I am just correcting false premises to ensure the OP does not accept bad advice!

>> Would request you to Concentrate on what client asks
This is exactly what I am doing -- ensuring the advice the OP accepts is good.

>> Enjoi ur Weekend!!
You too.

I will not respond to this thread again -- it is done!
>>I am not arguing.
Everybody can look into the thread, what you did.

>>I am just correcting false premises to ensure the OP does not accept bad advice!
>>The advice you gave was misleading, it's as simple as that!
That OP has to look into and decide. Would be gud if you respond to him only.

>>This is exactly what I am doing -- ensuring the advice the OP accepts is good.
I think u r really done. That why you are beating around the bush, time and again. Address to the client only. If you feel u really wanna correct somebody, then let him know in a preferably non-dominating manner.

>>I will not respond to this thread again -- it is done!
Neither do I.