S
Seven Kast USA
hi
What is use of pointerrs in c programming.is fasster than other types
by
KAST
What is use of pointerrs in c programming.is fasster than other types
by
KAST
Seven said:hi
What is use of pointerrs in c programming.is fasster than other types
by
KAST
Seven said:hi
What is use of pointerrs in c programming.is fasster than other types
by
KAST
C pointers are what is called in other languages "indirection", or accessingSeven Kast USA said:hi
What is use of pointerrs in c programming.is fasster than other types
by
KAST
Indirection is an extremely fast operation on all current processors.
Seven said:hi
What is use of pointerrs in c programming.is fasster than other types
by
Most often we use the pointer instead of using the arrays, (For
Computaion). The reason behind the above is illustread below with
example
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
int *arrayPtr = array;
now suppose you have to fetch the value at 5th index of the array.
case 1
array[4];
case 2
arrayPtr = arrayPtr + 4;
*arrayPtr;
In case 1 the compiler will derefrence the array in the following
manner
*(array + (sizeof(array[0]) * 4));
and in case 2
arrayPtr = arrayPtr + 4;
so pointer discard the overhead of multiplication as we see
in the case 2 (i.e Pointer)
and one more thing in case 1 you tend to call every time sizeof
also so this is also another factor. (For this I am not sure)
pete said:Most often we use the pointer instead of using the arrays, (For
Computaion). The reason behind the above is illustread below with
example
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
int *arrayPtr = array;
now suppose you have to fetch the value at 5th index of the array.
case 1
array[4];
case 2
arrayPtr = arrayPtr + 4;
*arrayPtr;
In case 1 the compiler will derefrence the array in the following
manner
*(array + (sizeof(array[0]) * 4));
Close.
(char *)&array[4] is
((char *)array + (sizeof(array[0]) * 4))
and in case 2
arrayPtr = arrayPtr + 4;so pointer discard the overhead of multiplication as we see
in the case 2 (i.e Pointer)
and one more thing in case 1 you tend to call every time sizeof
also so this is also another factor. (For this I am not sure)
You can't call sizeof. It's not a function.
Whatever multiplication that you imagine taking place in array[4]
must also be taking place in (arrayPtr + 4).
(char *)(arrayPtr + 4)
is ((char *)arrayPtr + (sizeof(array[0]) * 4))
Most often we use the pointer instead of using the arrays, (For
Computaion). The reason behind the above is illustread below with
example
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
int *arrayPtr = array;
now suppose you have to fetch the value at 5th index of the array.
case 1
array[4];
case 2
arrayPtr = arrayPtr + 4;
*arrayPtr;
In case 1 the compiler will derefrence the array in the following
manner
*(array + (sizeof(array[0]) * 4));
Close.
(char *)&array[4] is
((char *)array + (sizeof(array[0]) * 4))
and in case 2
arrayPtr = arrayPtr + 4;so pointer discard the overhead of multiplication as we see
in the case 2 (i.e Pointer)
and one more thing in case 1 you tend to call every time sizeof
also so this is also another factor. (For this I am not sure)
You can't call sizeof. It's not a function.
correct, sorry for the typo mistake.
Whatever multiplication that you imagine taking place in array[4]
must also be taking place in (arrayPtr + 4).
(char *)(arrayPtr + 4)
is ((char *)arrayPtr + (sizeof(array[0]) * 4))
So you mean that both are having the same time to execute ??
Why and what is the reason for using the pointers ??
pete said:(e-mail address removed) wrote:
Most often we use the pointer instead of using the arrays, (For
Computaion). The reason behind the above is illustread below with
example
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
int *arrayPtr = array;
now suppose you have to fetch the value at 5th index of the array.
case 1
array[4];
case 2
arrayPtr = arrayPtr + 4;
*arrayPtr;
In case 1 the compiler will derefrence the array in the following
manner
*(array + (sizeof(array[0]) * 4));
Close.
(char *)&array[4] is
((char *)array + (sizeof(array[0]) * 4))
and in case 2
arrayPtr = arrayPtr + 4;
so pointer discard the overhead of multiplication as we see
in the case 2 (i.e Pointer)
and one more thing in case 1 you tend to call every time sizeof
also so this is also another factor. (For this I am not sure)
You can't call sizeof. It's not a function.
correct, sorry for the typo mistake.
Whatever multiplication that you imagine taking place in array[4]
must also be taking place in (arrayPtr + 4).
(char *)(arrayPtr + 4)
is ((char *)arrayPtr + (sizeof(array[0]) * 4))
So you mean that both are having the same time to execute ??
Either way could be faster.
It's possible that arrayPtr might be stored
as a base and an offset, in which case *arrayPtr
might be exactly as operationally intense as array[4].
There's different reasons.
Most of the standard library function interfaces
need to use pointers.
pete wrote:
(e-mail address removed) wrote:
Most often we use the pointer instead of using the arrays, (For
Computaion). The reason behind the above is illustread below with
example
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
int *arrayPtr = array;
now suppose you have to fetch the value at 5th index of the array.
case 1
array[4];
case 2
arrayPtr = arrayPtr + 4;
*arrayPtr;
In case 1 the compiler will derefrence the array in the following
manner
*(array + (sizeof(array[0]) * 4));
Close.
(char *)&array[4] is
((char *)array + (sizeof(array[0]) * 4))
and in case 2
arrayPtr = arrayPtr + 4;
so pointer discard the overhead of multiplication as we see
in the case 2 (i.e Pointer)
and one more thing in case 1 you tend to call every time sizeof
also so this is also another factor. (For this I am not sure)
You can't call sizeof. It's not a function.
correct, sorry for the typo mistake.
Whatever multiplication that you imagine taking place in array[4]
must also be taking place in (arrayPtr + 4).
(char *)(arrayPtr + 4)
is ((char *)arrayPtr + (sizeof(array[0]) * 4))
So you mean that both are having the same time to execute ??
Either way could be faster.
It's possible that arrayPtr might be stored
as a base and an offset, in which case *arrayPtr
might be exactly as operationally intense as array[4].
But, don't you think if the above senario is put into the
loop; Then definetly pointer is going to much faster then
case of array.
Futhermore many members may disagree,
what I said (in the explanation)
as it will make hardly a diffrence in both the case...... but if you
are in the loop then it will really make the diffrence in terms of
execution time.
Sorry I did not got you, what you said above.
pete said:(e-mail address removed) wrote:
pete wrote:
(e-mail address removed) wrote:
Most often we use the pointer instead of using the arrays, (For
Computaion). The reason behind the above is illustread below with
example
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
int *arrayPtr = array;
now suppose you have to fetch the value at 5th index of the array.
case 1
array[4];
case 2
arrayPtr = arrayPtr + 4;
*arrayPtr;
In case 1 the compiler will derefrence the array in the following
manner
*(array + (sizeof(array[0]) * 4));
Close.
(char *)&array[4] is
((char *)array + (sizeof(array[0]) * 4))
and in case 2
arrayPtr = arrayPtr + 4;
so pointer discard the overhead of multiplication as we see
in the case 2 (i.e Pointer)
and one more thing in case 1 you tend to call every time sizeof
also so this is also another factor. (For this I am not sure)
You can't call sizeof. It's not a function.
correct, sorry for the typo mistake.
Whatever multiplication that you imagine taking place in array[4]
must also be taking place in (arrayPtr + 4).
(char *)(arrayPtr + 4)
is ((char *)arrayPtr + (sizeof(array[0]) * 4))
So you mean that both are having the same time to execute ??
Either way could be faster.
It's possible that arrayPtr might be stored
as a base and an offset, in which case *arrayPtr
might be exactly as operationally intense as array[4].
But, don't you think if the above senario is put into the
loop; Then definetly pointer is going to much faster then
case of array.
Definitely? No.
The dissassembly on my compiler shows accessing the pointer
as taking an extra mov instruction.
1: /* BEGIN new.c */
2:
3: #include<stdio.h>
4:
5: int main(void)
6: {
00401020 push ebp
00401021 mov ebp,esp
00401023 sub esp,18h
7: int array[5] = {0};
00401026 mov dword ptr [array],0
0040102D xor eax,eax
0040102F mov dword ptr [ebp-14h],eax
00401032 mov dword ptr [ebp-10h],eax
00401035 mov dword ptr [ebp-0Ch],eax
00401038 mov dword ptr [ebp-8],eax
8: int *arrayPtr;
9:
10: arrayPtr = array + 4;
0040103B lea ecx,dword ptr [ebp-8]
0040103E mov dword ptr [arrayPtr],ecx
11: printf("%d\n", array[4]);
00401041 mov edx,dword ptr [ebp-8]
00401044 push edx
00401045 push offset ___xt_z(0x00413a30)+10Ch
0040104A call printf(0x004012d0)
0040104F add esp,8
12: printf("%d\n", *arrayPtr);
00401052 mov eax,dword ptr [arrayPtr]
00401055 mov ecx,dword ptr [eax]
00401057 push ecx
00401058 push offset ___xt_z(0x00413a34)+110h
0040105D call printf(0x004012d0)
00401062 add esp,8
13: return 0;
00401065 xor eax,eax
14: }
00401067 mov esp,ebp
00401069 pop ebp
0040106A ret
Futhermore many members may disagree,
what I said (in the explanation)
as it will make hardly a diffrence in both the case...... but if you
are in the loop then it will really make the diffrence in terms of
execution time.
The results will varry from one implementation to the next.
Sorry I did not got you, what you said above.
Most of the standard library functions,
especially the string functions, have pointers for parameters.
So, if you can't use pointers,
then you can't use most of the standard library.
void *memset(void *s, int c, size_t n);
void *memcpy(void *s1, const void *s2, size_t n);
void *memmove(void *s1, const void *s2, size_t n);
void *memchr(const void *s, int c, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
size_t strlen(const char *s);
char *strcpy(char *s1, const char *s2);
char *strncpy(char *s1, const char *s2, size_t n);
char *strcat(char *s1, const char *s2);
char *strncat(char *s1, const char *s2, size_t n);
char *strchr(const char *s, int c);
char *strrchr(const char *s, int c);
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
size_t strspn(const char *s1, const char *s2);
size_t strcspn(const char *s1, const char *s2);
char *strpbrk(const char *s1, const char *s2);
char *strstr(const char *s1, const char *s2);
char *strtok(char *s1, const char *s2);
--
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.