Hi Rich!
First of all thanks for offer your knowledge to help me in this matter.
The question I threw to David was the simplest question about pointers that
I was able to extract from the problem I'm facing. The question isn't that
simple like that.

First, we are dealing with a template class that is used to store an image.
So we have a pointer to a generic class T and two values of the size of the
image w and y. The code stores using the 'new' statement an allocation of
memory of w*h*sizeof(T). For some working cases, the value of that product
is higher than the maximum value of an int type. In the code it is needed
to access the data in steps of bytes (that later are type casted to T*) so
I need to do the pointer arithmetics in bytes that cannot be done using
'int' types obviously for the reason above. The T* pointer is casted first
to char* before doing the arithmetics and then casted back again to T*
afterwards.

So my initial question is: what type should I use on the variables that are
used on the char* arithmetics so they don't overflow like 'int' does?

The code of the template is here:

https://github.com/synfig/synfig/blob/master/ETL/ETL/_pen.h

I'm sorry for not having a simpler example.

Thank you very much for taking time on it!
Greetings
PS: I CC this email to synfig-devl mailing list


2012/9/13 Dave Jeffery <david.richard.jeff...@gmail.com>

> Hi Carlos! Hi Rich!
>
> You're both into C++, where as my brain melts with Python so I think it
> would probably be best if I get you both chatting to each other!
>
> Many thanks for your help Rich - and as soon as you need anything for
> Blurp (or any other game) let me know! I'll reply to you properly later.
>
> All the best,
>
> Dave
>
> ---------- Forwarded message ----------
> From: Rich Talbot-Watkins <rich...@gmail.com>
> Date: 13 September 2012 11:15
> Subject: Re: Big arrays in C++
> To: Dave Jeffery <david.richard.jeff...@gmail.com>
>
>
>  Hi Dave,
>
> Yeah, happy to help!  I had to go and check it with the C++ standard
> though.
>
> In C++, it's best to index an array with a value of type size_t.  This is
> an implementation-defined unsigned integer type, which for x86 would
> normally be 32-bit, and for x64 64-bit, although this is not *necessarily*
> guaranteed.
>
> The important point is that if you declare:
>
> int array[SIZE];
>
> then SIZE * sizeof(int) must fit into a size_t, because it's also
> perfectly valid to write sizeof(array), which should return exactly that.
> He definitely shouldn't be using a signed int, as that just won't address
> enough elements.
>
> These days in C++ I wouldn't really recommend using static arrays like
> this, when there is std::vector<T> as a safer replacement.  This too takes
> size_t as an array index type.
>
> Feel free to put Carlos in touch with me, and I can probably give him a
> hand with any snippets of code he wants to send my way.
>
> How's everything going?  I don't have enough hours in the week any more,
> my little girl's growing up fast!  But at least I have plenty of work,
> unlike a good 25% of people around here :(
>
> Cheers,
> Rich
>
>
>
> On 13/09/2012 09:21, Dave Jeffery wrote:
>
> Hi Rich!
>
> I hope you are well. I'm sorry to bother you but a friend of mine, Carlos
> López González had a question about C++ (a language that's a mystery to
> me), so I wondered if you might know that answer to it. You're just about
> the only person I know who'd know the answer! Carlos is working on Synfig
> Studio, the open source animation program I use for a lot of my animation
> work and he's got a bit stuck.
>
> He has an array of a huge size (bigger than the maximum signed int value).
> He wondered what type should be the variable used to index the array? long?
> ptrdiff_t?
>
> If you're busy then please feel free to tell me to get lost, but I thought
> I'd try asking as Carlos is desperate for some help.
>
> All the best,
>
> Dave
> --
> Dave Jeffery
> Kiskunhalas
> Eötvös u. 2. II. em. 4.
> 6400 Hungary
>
> tel: +36 77 460 742
>
> email: david.richard.jeff...@gmail.com
> www: http://kecskebak.blogspot.com
>
>
>
>
>
> --
> Dave Jeffery
> Kiskunhalas
> Eötvös u. 2. II. em. 4.
> 6400 Hungary
>
> tel: +36 77 460 742
>
> email: david.richard.jeff...@gmail.com
> www: http://kecskebak.blogspot.com
>



-- 
Carlos
http://synfig.org
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Synfig-devl mailing list
Synfig-devl@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/synfig-devl

Reply via email to