We help IT Professionals succeed at work.

Native alternative to 'size_t'.

Unimatrix_001
on
1,102 Views
Last Modified: 2012-08-14
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?
Comment
Watch Question

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
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
CERTIFIED EXPERT
Top Expert 2009

Commented:
>> 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.
CERTIFIED EXPERT
Top Expert 2009

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

size_t might be defined as unsigned long, or even something else.
Kent OlsenData Warehouse / Database Architect
CERTIFIED EXPERT

Commented:
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
CERTIFIED EXPERT
Top Expert 2009

Commented:
>> 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.
CERTIFIED EXPERT
Top Expert 2009

Commented:
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 ...
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
>>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.

Author

Commented:
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?
CERTIFIED EXPERT
Top Expert 2009

Commented:
>> >> 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

Author

Commented:
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...?
CERTIFIED EXPERT
Top Expert 2009

Commented:
>> 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 ?

Author

Commented:
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?
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> 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
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> 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.
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> 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!
CERTIFIED EXPERT
Top Expert 2009

Commented:
>> 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.
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> 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?
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> 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.
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> 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.
CERTIFIED EXPERT
Top Expert 2009

Commented:
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. :)
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> 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.
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> 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


Author

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

Thanks everybody.
Uni
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

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

Author

Commented:
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
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> 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 :)
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> 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
evilrixSenior Software Engineer (Avast)
CERTIFIED EXPERT

Commented:
>> 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.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.