![]() |
Pointer row addresses have unexpected numbers
I can see that in the pasted results of the code snippet (also pasted
below), there are 48 decimal bytes between rows of an array. The coded subtraction of row addresses is showing 12 instead of 48. I cannot figure out why. I see the same result on my Windows cygwin and my Linux CentOS boxes. Any idea what is going on? //----------- Paste code snippet #include <stdio.h> #include <stdlib.h> int main(void) { int nrows = 5; /* Both nrows and ncols could be evaluated */ int ncols = 10; /* or read in at run time */ int row; int **rowptr; rowptr = malloc(nrows * sizeof(int *)); if (rowptr == NULL) { puts("\nFailure to allocate room for row pointers.\n"); exit(0); } printf("\nIndex &rowptr[row] &rowptr[row][0] Ptr(hex) Ptr(dec) Diff(dec)"); for (row = 0; row < nrows; row++) { rowptr[row] = malloc(ncols * sizeof(int)); if (rowptr[row] == NULL) { printf("\nFailure to allocate for row[%d]\n",row); exit(0); } printf("\n %d %u %u %p %d", row, &rowptr[row], &rowptr[row][0], rowptr[row], rowptr[row] ); if (row > 0) printf(" %d",(int)(rowptr[row] - rowptr[row-1])); } return 0; } //----------------------------------------- // Results // // Index &rowptr[row] &rowptr[row][0] Ptr(hex) Ptr(dec) Diff(dec) // 0 6685048 6750616 0x670198 6750616 // 1 6685052 6750664 0x6701c8 6750664 12 // 2 6685056 6750712 0x6701f8 6750712 12 // 3 6685060 6750760 0x670228 6750760 12 // 4 6685064 6750808 0x670258 6750808 12 |
Re: Pointer row addresses have unexpected numbers
On Sat, 11 Dec 2010 08:24:37 -0800, Malcolm Nooning wrote:
> I can see that in the pasted results of the code snippet (also pasted > below), there are 48 decimal bytes between rows of an array. The > coded subtraction of row addresses is showing 12 instead of 48. I > cannot figure out why. I see the same result on my Windows cygwin and > my Linux CentOS boxes. Any idea what is going on? The result of subtracting two pointers is the number of *elements* between the pointers, not the number of bytes. 12 integers is 48 bytes. |
Re: Pointer row addresses have unexpected numbers
On Sat, 11 Dec 2010 11:24:37 -0500, Malcolm Nooning
<mhnpittsburgh@gmail.com> wrote: > I can see that in the pasted results of the code snippet (also pasted > below), there are 48 decimal bytes between rows of an array. The > coded subtraction of row addresses is showing 12 instead of 48. And without looking at any of your code, I can tell you that you're subtracting pointers, and that sizeof(whatever type the pointers point to) == 4. -- Morris Keesan -- mkeesan@post.harvard.edu |
Re: Pointer row addresses have unexpected numbers
On Dec 11, 11:35*am, Nobody <nob...@nowhere.com> wrote:
> On Sat, 11 Dec 2010 08:24:37 -0800, Malcolm Nooning wrote: > > I can see that in the pasted results of the code snippet (also pasted > > below), there are 48 decimal bytes between rows of an array. *The > > coded subtraction of row addresses is showing 12 instead of 48. *I > > cannot figure out why. *I see the same result on my Windows cygwin and > > my Linux CentOS boxes. *Any idea what is going on? > > The result of subtracting two pointers is the number of *elements* between > the pointers, not the number of bytes. 12 integers is 48 bytes. But, there are 10 integers (int ncols = 10) per row, not 12. Where do the extra two come from? |
Re: Pointer row addresses have unexpected numbers
On 12/12/10 06:28 AM, Malcolm Nooning wrote:
> On Dec 11, 11:35 am, Nobody<nob...@nowhere.com> wrote: >> On Sat, 11 Dec 2010 08:24:37 -0800, Malcolm Nooning wrote: >>> I can see that in the pasted results of the code snippet (also pasted >>> below), there are 48 decimal bytes between rows of an array. The >>> coded subtraction of row addresses is showing 12 instead of 48. I >>> cannot figure out why. I see the same result on my Windows cygwin and >>> my Linux CentOS boxes. Any idea what is going on? >> >> The result of subtracting two pointers is the number of *elements* between >> the pointers, not the number of bytes. 12 integers is 48 bytes. > > But, there are 10 integers (int ncols = 10) per row, not 12. > Where do the extra two come from? Probably housekeeping space used by the allocator. -- Ian Collins |
Re: Pointer row addresses have unexpected numbers
On Dec 11, 2:52*pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 12/12/10 06:28 AM, Malcolm Nooning wrote: > > > On Dec 11, 11:35 am, Nobody<nob...@nowhere.com> *wrote: > >> On Sat, 11 Dec 2010 08:24:37 -0800, Malcolm Nooning wrote: > >>> I can see that in the pasted results of the code snippet (also pasted > >>> below), there are 48 decimal bytes between rows of an array. *The > >>> coded subtraction of row addresses is showing 12 instead of 48. *I > >>> cannot figure out why. *I see the same result on my Windows cygwin and > >>> my Linux CentOS boxes. *Any idea what is going on? > > >> The result of subtracting two pointers is the number of *elements* between > >> the pointers, not the number of bytes. 12 integers is 48 bytes. > > > But, there are 10 integers (int ncols = 10) per row, not 12. > > Where do the extra two come from? > > Probably housekeeping space used by the allocator. > > -- > Ian Collins No, that cannot be. The C books tell us that whenever two pointers are subtracted we get the number of the elements between what they point to. This is the gist of what "Nobody", and Morris Keesan, were stating above. However, just going by that, the answers should all be 10, not 12. A programmer would need to rely on the results of pointer subtraction being well defined. There is something else going on, and I would really like to get a solid understanding of what it is. |
Re: Pointer row addresses have unexpected numbers
On 12/12/10 09:37 AM, Malcolm Nooning wrote:
> On Dec 11, 2:52 pm, Ian Collins<ian-n...@hotmail.com> wrote: >> On 12/12/10 06:28 AM, Malcolm Nooning wrote: >> >>> On Dec 11, 11:35 am, Nobody<nob...@nowhere.com> wrote: >>>> On Sat, 11 Dec 2010 08:24:37 -0800, Malcolm Nooning wrote: >>>>> I can see that in the pasted results of the code snippet (also pasted >>>>> below), there are 48 decimal bytes between rows of an array. The >>>>> coded subtraction of row addresses is showing 12 instead of 48. I >>>>> cannot figure out why. I see the same result on my Windows cygwin and >>>>> my Linux CentOS boxes. Any idea what is going on? >> >>>> The result of subtracting two pointers is the number of *elements* between >>>> the pointers, not the number of bytes. 12 integers is 48 bytes. >> >>> But, there are 10 integers (int ncols = 10) per row, not 12. >>> Where do the extra two come from? >> >> Probably housekeeping space used by the allocator. > > No, that cannot be. Oh yes it can! > The C books tell us that whenever two pointers are subtracted we get > the number of the elements between what they point to. To pointers within the same object. Your rowptr[row] and rowptr[row-1] are the results of different allocations. > A > programmer would need to rely on the results of pointer subtraction > being well defined. It is.... > There is something else going on, and I would > really like to get a solid understanding of what it is. ....but not between pointers in different blocks. -- Ian Collins |
Re: Pointer row addresses have unexpected numbers
Malcolm Nooning wrote:
) On Dec 11, 2:52?pm, Ian Collins <ian-n...@hotmail.com> wrote: )> Probably housekeeping space used by the allocator. ) ) No, that cannot be. ) The C books tell us that whenever two pointers are subtracted we get ) the number of the elements between what they point to. This is the ) gist of what "Nobody", and Morris Keesan, were stating above. ) However, just going by that, the answers should all be 10, not 12. A ) programmer would need to rely on the results of pointer subtraction ) being well defined. There is something else going on, and I would ) really like to get a solid understanding of what it is. It's really very easy. You're comparing the results of *different calls* to malloc(). The books are talking about *one* array or block. You're comparing apples and oranges. The standard sais that comparing pointers that point into two different objects (i.e. two different calls to malloc(), or two static arrays, or whatever), gives undefined results. SaSW, Willem -- Disclaimer: I am in no way responsible for any of the statements made in the above text. For all I know I might be drugged or something.. No I'm not paranoid. You all think I'm paranoid, don't you ! #EOT |
Re: Pointer row addresses have unexpected numbers
On Dec 11, 3:41*pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 12/12/10 09:37 AM, Malcolm Nooning wrote: > > > > > On Dec 11, 2:52 pm, Ian Collins<ian-n...@hotmail.com> *wrote: > >> On 12/12/10 06:28 AM, Malcolm Nooning wrote: > > >>> On Dec 11, 11:35 am, Nobody<nob...@nowhere.com> * *wrote: > >>>> On Sat, 11 Dec 2010 08:24:37 -0800, Malcolm Nooning wrote: > >>>>> I can see that in the pasted results of the code snippet (also pasted > >>>>> below), there are 48 decimal bytes between rows of an array. *The > >>>>> coded subtraction of row addresses is showing 12 instead of 48. *I > >>>>> cannot figure out why. *I see the same result on my Windows cygwin and > >>>>> my Linux CentOS boxes. *Any idea what is going on? > > >>>> The result of subtracting two pointers is the number of *elements* between > >>>> the pointers, not the number of bytes. 12 integers is 48 bytes. > > >>> But, there are 10 integers (int ncols = 10) per row, not 12. > >>> Where do the extra two come from? > > >> Probably housekeeping space used by the allocator. > > > No, that cannot be. > > Oh yes it can! > > > The C books tell us that whenever two pointers are subtracted we get > > the number of the elements between what they point to. > > To pointers within the same object. *Your rowptr[row] and rowptr[row-1] > are the results of different allocations. > > > A > > programmer would need to rely on the results of pointer subtraction > > being well defined. > > It is.... > > > There is something else going on, and I would > > really like to get a solid understanding of what it is. > > ...but not between pointers in different blocks. > > -- > Ian Collins Aha! That is the answer. To be explicit for those as dull as myself, in the snippet below rowptr[0] = malloc(ncols * sizeof(int)); The contents of rowptr[0] is a pointer to 10 integers. The contents of rowptr[1] is also pointer to 10 integers, but that pointer could be pointing anywhere the compiler found free memory for it, which is not necessarily contiguous with that of rowptr[0]. Thus, there is no "right" answer for the subtraction (int)(rowptr[row] - rowptr[row-1]). It does not even have to be consistent. It just happened to be 12 consistently because the compiler found contiguously free memory each malloc. The other 2 elements might be housekeeping, but it might be for absolutely nothing. My gcc has been around for 20 years, so I suppose it is pretty efficient by now. It is undoubtedly for housekeeping. Thanks, Ian |
Re: Pointer row addresses have unexpected numbers
Nobody <nobody@nowhere.com> writes:
> On Sat, 11 Dec 2010 08:24:37 -0800, Malcolm Nooning wrote: >> I can see that in the pasted results of the code snippet (also pasted >> below), there are 48 decimal bytes between rows of an array. The >> coded subtraction of row addresses is showing 12 instead of 48. I >> cannot figure out why. I see the same result on my Windows cygwin and >> my Linux CentOS boxes. Any idea what is going on? > > The result of subtracting two pointers is the number of *elements* between > the pointers, not the number of bytes. 12 integers is 48 bytes. Furthermore, the behavior of subtracting two pointers to separately allocated objects (which is what the program in the original post does) is undefined. -- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |
| All times are GMT. The time now is 10:03 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.