Avatar of mordekai
mordekai asked on

problems with pointer arithmetic

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!
C

Avatar of undefined
Last Comment
sunnycoder

8/22/2022 - Mon
ozo

_activeBlock +=  1; //should do what you want
ASKER CERTIFIED SOLUTION
sunnycoder

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
sunnycoder

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
beavis_shenzhen

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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER
mordekai

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
ASKER
mordekai

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?
beavis_shenzhen

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
sunnycoder

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
ASKER
mordekai

<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!
sunnycoder

glad to be of assistance :o)
Your help has saved me hundreds of hours of internet surfing.
fblack61
Avik Dasgupta

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.
ASKER
mordekai

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.
ozo

_activeBlock += sizeof( *_activeBlock );
does not point to the next *_activeBlock
_activeBlock += 1;
points to the next *_activeBlock;
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
sunnycoder

apparently what mordekai wanted was _activeBlock to hold next element in _blocks[]