Page 1 of 1

C --\++ Operator

Posted: Thu Oct 24, 2002 8:08 pm
by PlayOS
Hi all,

I just have a question about the ++ operator, if I have an array of longs say long data[25], if I go data++ does this move me to the next element (ie 4 bytes) or to the next byte?

thanks.

Also, I hope that these c questions are not to far off topic here, just let me know if it is and will not post them here. However they are directly related to my OS programming, I am not learning C just for the sake of it, I am doing it only for OS programming.

Re:C --\++ Operator

Posted: Thu Oct 24, 2002 8:25 pm
by Tom
if you do a "array[ i + 1 ]" and i = 4, you have the fourth -1 array ( c uses 0 as the first one )

and you can do array++ to go to the next thing in the array, hope that helps,

Re:C --\++ Operator

Posted: Thu Oct 24, 2002 8:25 pm
by sonneveld
if you increment or decrement a pointer (that's kinda what array's are, pointers to a block of data), it increases it by the size of the data type.

so if I have an struct like this:

Code: Select all

struct largething
{
   int something;
    int more[100];
}
struct largething *ptr;

ptr++; 
the ptr will be incrememted by however many bytes the struct is (including alignment i *think).

subtracting pointers will also give you the number of datatypes.. not the number of bytes.

It gets tricky with void* pointers and arithmetic though. I know GCC defaults to char* size but other compilers may vary.

- Nick

Re:C --\++ Operator

Posted: Thu Oct 24, 2002 9:51 pm
by PlayOS
Thanks guys, I think some people do not like me posting C questions here so I will not do it anymore.

Thanks for your help.

Re:C --\++ Operator

Posted: Fri Oct 25, 2002 1:13 am
by Schol-R-LEA
PlayOS wrote: Hi all,

I just have a question about the ++ operator, if I have an array of longs say long data[25], if I go data++ does this move me to the next element (ie 4 bytes) or to the next byte?
I can't promise this is right, but I'll try.

As the question is stated, this won't work at all. Bases of declared arrays are not quite the same as pointers; among other things, their values are constant. From an assembly language POV, an array base is a label, whereas a pointer is a memory location in which an effective address has been stored. The code

Code: Select all

any_type data[23];
data++;

will give a type mismatch on the second line with most modern compilers.

OTOH, if you declared a pointer [tt]any_type *data_p;[/tt] and assigned the base of [tt]data[/tt] to it, then yes, according to the rules of pointer arithmetic, it increments by [tt]sizeof(any_type)[/tt]. Just remember that if the array is auto to the function, it only lasts as long as the function stack frame.

On a related note, while it is legal to do this:

Code: Select all

any_type data_curr[];   /* pointer to any_type */
data_curr = (any_type *)malloc(sizeof(any_type) * 23);
data_curr++;
it is a very bad idea; since [tt]data_m[/tt] is no longer pointing to the beginning of the allocated memory, it is no longer possible to [tt]free()[/tt] it correctly. OTOH, having a second variable, like this

Code: Select all

any_type *data_curr, data_top;  
data_top = data _curr = (any_type *)malloc(sizeof(any_type) * 23);
data_curr++;
free(data_top);
risks an aliasing bug unless you explicitly set [tt]data_curr = NULL;[/tt] after [tt]free()[/tt]ing [tt]data_top[/tt]. If there is more than one place where this can happen, or the data is [tt]free()[/tt]ed in a different function than it was [tt]malloc()[/tt]ed, it could easily be forgotten, making for a very hard bug to fix.
Also, I hope that these c questions are not to far off topic here, just let me know if it is and will not post them here. However they are directly related to my OS programming, I am not learning C just for the sake of it, I am doing it only for OS programming.
Well, they technically are OT, but they are close enough that they could be considered relevant. Posting C specific group may get faster and more exact results, however.

Re:C --\++ Operator

Posted: Fri Oct 25, 2002 1:26 am
by PlayOS
Thanks for your response, I agree that they are technically off topic but, I think that they are no more off topic than questions about gcc or ld or the use of any program, but these kind of questions are coming in this forum all the time and no one says nothing.

People feel that they can ask them here because they are developing an OS, they run into trouble, so they ask. I personally dont think that questions about gcc, ld or C are off topic if you are using or needing the knowledge to build your OS.

But if people dont want me to do it, then I wont. It's just that simple.

Thanks again.

Re:C --\++ Operator

Posted: Fri Oct 25, 2002 3:59 am
by Pype.Clicker
PlayOS wrote: Thanks for your response, I agree that they are technically off topic but, I think that they are no more off topic than questions about gcc or ld or the use of any program, but these kind of questions are coming in this forum all the time and no one says nothing.

People feel that they can ask them here because they are developing an OS, they run into trouble, so they ask. I personally dont think that questions about gcc, ld or C are off topic if you are using or needing the knowledge to build your OS.
hmmm ... perhaps we should split this forum in 2 or 3 parts: one for tools/language/etc. questions, one for hardware/pmode/etc. questions and a last one for generic design issues ...

Re:C --\++ Operator

Posted: Fri Oct 25, 2002 4:31 am
by Ozguxxx
I think it is not a good idea to divide forum because I am not looking for a C forum, I am looking for an OS dev. forum. So I think question that is argued here is completely out of interest of this forum. PlayOS you can get answer to your question by yourself very easily by just writing a simple (but intelligent) C program. I mean you could test your data[25] in the time you write the question here. Thanx...

Re:C --\++ Operator

Posted: Fri Oct 25, 2002 5:55 am
by Tim
I think a separate programming language forum is a good idea, since the posts here tend to be either OS theory or actual code. Maybe three: OS theory, x86 stuff ("how do I use V86?") and programming languages.

Still, if df is away (is he?), we'll get no new fora. :)

Re:C --\++ Operator

Posted: Fri Oct 25, 2002 10:44 am
by PlayOS
Ozguxxx, you are right, at least you come up with a reason. I have noticed the new programming forum that is here now, I will post all such questions there.

Tim, I think that is a good idea to have seperate forums for different topics, this way you could have beginner stuff in one, more advanced topics in another and then really advanced stuff in another.

However, I am glad that there is a new forum just for programming topics that are not specific to OS Dev.