Solved

problems with pointer arithmetic

Posted on 2004-03-23
14
332 Views
Last Modified: 2010-04-15
Here's the code:  (ignore the fact that it's in a class)

///////////////////////////////////////////
//////////////////////////////////////////
Block* _blocks[5]; // declaration in class

/////////////////////////////////////////
////////////////////////////////////////
void Driver::NextActiveBlock()
{
    if( _activeBlock == 0 || _activeBlock == _blocks[MAX_BLOCKS-1] )
        _activeBlock = _blocks[0];
    else
        _activeBlock += sizeof( _blocks[1]);  //problem is here.
}
//////////////////////////////////////////
//////////////////////////////////////////


Ok, _blocks is an array of pointers to Block objects.... so Block**
So i should be able to increment the _activeBlock from one element to the next by adding the size of the pointer within the array (should be 4).

But even if i just replace "sizeof(_blocks[1])" with "4".... _activeBlock doesn't end up pointing to the next element in the list.

Please note:  I realize that I could just increment an index into the array but I'm fiddling around with pointer arithmetic.  If i'm misunderstanding the way memory is allocated when a array of pointers is declared... can someone correct me or fill me in?

Thanks for your time!
0
Comment
Question by:mordekai
  • 5
  • 4
  • 2
  • +2
14 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 10664897
_activeBlock +=  1; //should do what you want
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 100 total points
ID: 10664898
Hi mordekai,

> Ok, _blocks is an array of pointers to Block objects.... so Block**
It may be internally treated as Block** but there is difference between memory organization for ** *[] and [] [] types

from what I can see activeBlock holds the value held in a member of blocks ... e.g. consider

activeBlock is stored at memory location 1000

blocks is stored at 2000 and contains { 3000,4000,5000,6000,7000 }

suppose activeBlock holds 4000, then executing
_activeBlock += sizeof( _blocks[1]);  //problem is here.
will make it point to 4004 and not 5000 as you may be expecting ....

what the array of block * held in blocks[] may be contiguous, the values held in that array are not guaranteed to be contiguous

Sunnycoder
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10664913
ozo,

> _activeBlock +=  1; //should do what you want
It will not ... for the same reasons ... _activeBlock + 1 will increment the value held in _activeblock (using pointer arithmetic ofcourse) but it will not make it point to the next element in the list of _blocks[]

mordekai,

refer to this link for a previous discussion
http:Q_20823846.html

Sunnycoder
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 1

Expert Comment

by:beavis_shenzhen
ID: 10664925
Think about this situation:
int * pint = NULL;
pint++;
the value of pint should be the sizeof(int) that is 4;

_activeBlock seemingly  is a pointer to class Block, so when
_activeBlock += 4
the value of _activeBlock is actually added by 4*N,where N is decided by the compiler;

so just try ((int*)_activeBlock)++;
hope these help.
0
 

Author Comment

by:mordekai
ID: 10664974
sunnycoder:  thanks for the speedy input. i'm trying to absorb all the information you have set before me :)

beavis_shenzhen:

"the value of _activeBlock is actually added by 4*N,where N is decided by the compiler;"

can you explain to me why 'N' is decided by the compiler?  _activeBlock points to an object of type Block.. which contains variables whose size is known at compile time ( i think ).  are you saying that 'N' can be variable?  What would I have to do to simply use _activeBlock like an iterator.... increment through the _blocks array element by element (without indexing into the array that is).

thanks again for your time
0
 

Author Comment

by:mordekai
ID: 10665011
sunnycoder:  It sounds like you're saying that it isn't possible to iterate through the array that i've created due to the fact that it isn't guaranteed to be contiguous?  Is that right?
0
 
LVL 1

Expert Comment

by:beavis_shenzhen
ID: 10665018
what I mean is that N is decided by the size of class Block.

consider:
char * pChar = NULL;
int * pInt = NULL;
double * pDouble = NULL;
CDialog * pDialog = NULL;
pChar++;//value is 00000001
pInt++;//value is 00000004
pDouble++;//value is 00000008
pDialog++// value is 00000074

so you know what I mean.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10665030
mordekai,

The array is contiguous but the values held in it are not

int b[5] = { 10,25,11,6,90};

int a = b[0];

If you want a to successively hold values held in b (i.e. b[1] b[2] b[3] ... )
then clearly a = a + sizeof(int); is not the way to go ...

you need to iterate over b
int i = 0
a = b[i++]; is the way to be used ....

replace int by Block *, a by _blocks and b by _activeBlock to arrive at your analogy

Sunnycoder
0
 

Author Comment

by:mordekai
ID: 10665040
<sigh>  i just spent 3 or 4 hours trying to write/debug/research this problem... because i thought i understood pointers.  lol

thanks for the clarification sunnycoder!
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10665052
glad to be of assistance :o)
0
 
LVL 2

Expert Comment

by:Avik77
ID: 10665067
what did u initialise _activeBlock with ??
was it
Block * _activeBlock;
OR
Block ** _activeBlock;

If it is the first one, i may suggest
_activeBlock += sizeof( Block *);

Avik.
0
 

Author Comment

by:mordekai
ID: 10665109
Avik77

the first one.  and I first tried _activeBlock += sizeof( Block* );  

Block* evaluates to 4, which when added to _activeBlock... does not point to the next element in _blocks.

one thing that I failed to mention in the question.. which (now that i think about it) may have something to do with my problem... each Block (the object to which a member of _blocks points) is declared with the new operator.  So they're allocated memory is in no way contiguous with the elements of _blocks.

It makes sense to me that if _blocks is allocated as such:
array[0]    array[0]    array[0]    array[0]    array[0]

[ Block* ]  [ block* ]  [ block* ]  [ block* ]  [ block* ]

then if you create a new pointer 'ptr', and point it to array[0], then you should be able to add the sizeof[Block*]  (via the function mentioned in msdn help files) to 'ptr' in order to point it to the next element in the list.

I clearly have more research to do.
0
 
LVL 84

Expert Comment

by:ozo
ID: 10665177
_activeBlock += sizeof( *_activeBlock );
does not point to the next *_activeBlock
_activeBlock += 1;
points to the next *_activeBlock;
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10665350
apparently what mordekai wanted was _activeBlock to hold next element in _blocks[]
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to align numbers in C using the %d 2 104
TCP/IP Socket - connection close results in data lost 14 140
smtp c source code 7 85
Super Scope, DHCP 5 91
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question