<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body><div>A pointer to a char (*char) is not the same size as a char. A pointer to a char is the size of a pointer on your machine (8 on a 64-bit machine).<br></div>
<div> </div>
<div>But when you increment a char pointer it gets incremented by the size of a char (1) and when you increment a float pointer it gets<br></div>
<div>incremented by the size of a float (4). Remember here that we're incrementing the <i>value</i> of the pointer, so we're incrementing the location of memory that the pointer points at.<br></div>
<div> </div>
<div>This is so that if you have a bunch of data packed into an array, and you have a pointer to an element in that array, when you increment the pointer it is pointing at the next element in the array.<br></div>
<div> </div>
<div>The reason that the (char*) cast happens is that the onset variable is defined to be measured in bytes, so if we want to increment the pointer by a number of bytes we have to make sure the compiler treats it as a char pointer. That doesn't seem to jive with your later email though, which says the onset is getting multiplied by sizeof(t_word). In that context it sounds like the onset is measured in words. Are you sure those are the same onset?<br></div>
<div> </div>
<div>(sorry I haven't dug into the code itself, I'm going off of your emails).<br></div>
<div> </div>
<div>-s<br></div>
<div> </div>
<div> </div>
<div> </div>
<div>On Mon, Aug 17, 2015, at 01:54 PM, Forrest Curo wrote:<br></div>
<blockquote type="cite"><div dir="ltr">*char is guaranteed to be the same size as a 'char', 8 bits -- but if you're treating t_word as an array of char's, you can get into t_word and process it in 8 bit chunks.<br></div>
<div><div> </div>
<div><div>On Mon, Aug 17, 2015 at 9:53 AM, Jonathan Wilkes via Pd-list <span dir="ltr"><<a href="mailto:pd-list@lists.iem.at">pd-list@lists.iem.at</a>></span> wrote:<br></div>
<blockquote style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0.8ex;border-left-width:1px;border-left-color:rgb(204, 204, 204);border-left-style:solid;padding-left:1ex;"><div><div style="color:rgb(0, 0, 0);background-color:rgb(255, 255, 255);font-family:HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif;font-size:16px;"><div dir="ltr">Ah, ok-- I forgot my pointer arithmetic rules.<br></div>
<div dir="ltr">Once I remembered how it works, I still wasn't understanding how you could be doing pointer arithmetic with byte granularity when t_word is wider than 1 byte.  But then I see from template_find_field you are multiplying the onset by sizeof(t_word).<br></div>
<div dir="ltr"> </div>
<div dir="ltr">So now, further down the rabbit hole of knowledge, I'm wondering why you multiply by sizeof(t_word) at all.  If you didn't, couldn't the (char *) cast go away?<span><span class="colour" style="color:rgb(136, 136, 136)"></span></span><br></div>
<div> </div>
<div dir="ltr"> </div>
<div dir="ltr"><span><span class="colour" style="color:rgb(136, 136, 136)">-Jonathan</span></span><br></div>
<div> </div>
<div><div><div> </div>
<div> </div>
<div><div> </div>
</div>
<div style="display:block;"><div style="font-family:HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif;font-size:16px;"><div style="font-family:HelveticaNeue, 'Helvetica Neue', Helvetica, Arial, 'Lucida Grande', sans-serif;font-size:16px;"><div dir="ltr"><span class="font" style="font-family:Arial"><span class="size" style="font-size:small">On Monday, August 17, 2015 12:24 PM, Miller Puckette <<a href="mailto:msp@ucsd.edu">msp@ucsd.edu</a>> wrote:</span></span><br></div>
<div> </div>
<div> </div>
<div><div>I don't thing the width of (char *) enters into it (it's a pointer, 8 chars in <br></div>
<div>64 bit addr space)  - the direct contrast to be made is (char) vs (t_word).<br></div>
<div> </div>
<div>Not sure if that answers the question though...<br></div>
<div> </div>
<div>cheers<br></div>
<div>M<br></div>
<div><div> </div>
<div>On Mon, Aug 17, 2015 at 03:52:27PM +0000, Jonathan Wilkes via Pd-list wrote:<br></div>
<div>> Thanks, I think I'm getting it.<br></div>
<div>> So is char* guaranteed to be the same width as sizeof(t_word)?  If so, are you just using it as a shorthand?<br></div>
<div>> <br></div>
<div>> Thanks,<br></div>
<div>> Jonathan<br></div>
<div>> <br></div>
<div>> <br></div>
<div>>  <br></div>
<div>> <br></div>
<div>> <br></div>
<div>>      On Monday, August 17, 2015 11:31 AM, Miller Puckette <<a href="mailto:msp@ucsd.edu" shape="rect">msp@ucsd.edu</a>> wrote:<br></div>
<div>>    <br></div>
<div>> <br></div>
<div>>  Here's an example...<br></div>
<div>> <br></div>
<div>> #include <stdio.h><br></div>
<div>> <br></div>
<div>> float foo[2];<br></div>
<div>> <br></div>
<div>> main()<br></div>
<div>> {<br></div>
<div>>     printf("foo = %p\n", foo); <br></div>
<div>>     printf("incremented as float: %p\n", foo+1);<br></div>
<div>>     printf("incremented as (char *): %p\n", ((char *)foo)+1);<br></div>
<div>> }<br></div>
<div>> <br></div>
<div>> ---><br></div>
<div>> <br></div>
<div>> foo = 0x601038<br></div>
<div>> incremented as float: 0x60103c<br></div>
<div>> incremented as (char *): 0x601039<br></div>
<div>> <br></div>
<div>> Adding an integer to a pointer "increments" it - the effect depends on the type<br></div>
<div>> of pointer.  Another way to think of it is that foo[1], say, is semantically<br></div>
<div>> identical to *(foo+1).<br></div>
<div>> <br></div>
<div>> cheers<br></div>
<div>> Miller<br></div>
<div>> <br></div>
<div>> On Mon, Aug 17, 2015 at 03:10:35PM +0000, Jonathan Wilkes via Pd-list wrote:<br></div>
<div>> > But we're dealing with an array of t_words, so onset*sizeof(t_word) is what we want anyway, no?<br></div>
<div>> > -Jonathan <br></div>
<div>> > <br></div>
<div>> > <br></div>
<div>> >      On Monday, August 17, 2015 10:55 AM, Claude Heiland-Allen <<a href="mailto:claude@mathr.co.uk" shape="rect">claude@mathr.co.uk</a>> wrote:<br></div>
<div>> >    <br></div>
<div>> > <br></div>
<div>> >  On 17/08/15 15:36, Jonathan Wilkes via Pd-list wrote:<br></div>
<div>> > > Hi list,Wondering if someone will give me a free lesson in C programming.<br></div>
<div>> > > In g_traversal.c, there's some code to retrieve a float from a t_word* vec.  It looks like this:<br></div>
<div>> > > *(t_float *)(((char *)vec) + onset));<br></div>
<div>> > > Why does vec need to be cast to char*?  t_word has to be as big as the largest member of the union, and the largest member has to be the same size as char*, right?  (Otherwise we'd have big problems...)<br></div>
<div>> > <br></div>
<div>> > aiui pointer arithmetic is in increments of sizeof(pointee)<br></div>
<div>> > if onset is measured in bytes (I don't know if it is in this case, but <br></div>
<div>> > it looks likely), then you need to have a pointer to bytes for the <br></div>
<div>> > addition to be meaningful.  vec is already a pointer, but adding onset <br></div>
<div>> > to a t_word* would offset the address by onset*sizeof(t_word) bytes<br></div>
<div>> > <br></div>
<div>> > <br></div>
<div>> > _______________________________________________<br></div>
<div>> > <a href="mailto:Pd-list@lists.iem.at" shape="rect">Pd-list@lists.iem.at</a> mailing list<br></div>
<div>> > UNSUBSCRIBE and account-management -> <a href="http://lists.puredata.info/listinfo/pd-list" shape="rect">http://lists.puredata.info/listinfo/pd-list</a><br></div>
<div>> > <br></div>
<div>> > <br></div>
<div>> >  <br></div>
<div>> <br></div>
<div>> > _______________________________________________<br></div>
<div>> > <a href="mailto:Pd-list@lists.iem.at" shape="rect">Pd-list@lists.iem.at</a> mailing list<br></div>
<div>> > UNSUBSCRIBE and account-management -> <a href="http://lists.puredata.info/listinfo/pd-list" shape="rect">http://lists.puredata.info/listinfo/pd-list</a><br></div>
<div>> <br></div>
<div>> <br></div>
<div>> <br></div>
<div>>   <br></div>
<div> </div>
<div>> _______________________________________________<br></div>
<div>> <a href="mailto:Pd-list@lists.iem.at" shape="rect">Pd-list@lists.iem.at</a> mailing list<br></div>
<div>> UNSUBSCRIBE and account-management -> <a href="http://lists.puredata.info/listinfo/pd-list" shape="rect">http://lists.puredata.info/listinfo/pd-list</a><br></div>
</div>
<div> </div>
<div> </div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div> </div>
<div>_______________________________________________<br></div>
<div><a href="mailto:Pd-list@lists.iem.at">Pd-list@lists.iem.at</a> mailing list<br></div>
<div>UNSUBSCRIBE and account-management -> <a href="http://lists.puredata.info/listinfo/pd-list">http://lists.puredata.info/listinfo/pd-list</a><br></div>
<div> </div>
</blockquote></div>
</div>
<div><u>_______________________________________________</u><br></div>
<div><a href="mailto:Pd-list@lists.iem.at">Pd-list@lists.iem.at</a> mailing list<br></div>
<div>UNSUBSCRIBE and account-management -> <a href="http://lists.puredata.info/listinfo/pd-list">http://lists.puredata.info/listinfo/pd-list</a><br></div>
</blockquote><div> </div>
</body>
</html>