iterating pointer gives array subscript is above array bounds error [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have seen allot of posts about the array subscript is above array bounds error and i understand it happends when the iterator equals the max size of the array but my case is different:
static inline void setUninitialized(uint32 * xoBuff, uint32 xiItemSize)
{
uint32 i;
for (i=0 ; i < (xiItemSize/sizeof(uint32)) ; i++)
{
*(xoBuff++) = (uint32)MAP_UNINITIALIZED_VALUE;
}
}
when i run this code i get the array subscript is above array bounds error but i cant figure out why, i have literally tried all the posible combinations of incrementing the iterator and casting all kinds of variables.
the definition of uint32 is:
typedef unsigned long uint32;
any ideas why this keeps happening?
EDIT:
the way that the function is called is as follows:
TableEntry sEntry;
setUninitialized((uint32 *)&sEntry.policyKey, sizeof(PolicyKey));
the policyKey field is of instance PolicyKey

The line
sizeof(PolicyKey)
doesn't give you the size of the array. You can't ever get the size of an array in C. Rather, it's giving you the size of the pointer PolicyKey (assuming that's what it is), which will always be the same size (probably 8). So then (xiItemSize/sizeof(uint32)) will always (again assuming that everything on your computer is normally-sized) evaluate to 2.
It's hard to say anything else without knowing what sEntry looks like, but my guess is that you pass in a buffer that's only one-item long at some point, and it segfaults when it tries to dereference *(xoBuff++).

Related

Dereferencing a void pointer using its size in C [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I know that we cannot dereference a void pointer until and unless we typecast it.
void *ptr = call_a_function();
printf("%d",*(int *)ptr);
This informs the datatype of element(in this case its integer) to the compiler so that it can dereference the amount of space required for it(in this case its 4 bytes).
Suppose I dont know the final datatype, however I know the size of datatype. Can I still dereference it using only the size(4 bytes) and not the datatype(not using int)??
In other words is there a way to tell the compiler how many bytes of memory to read by providing the number of bytes to extract and not the datatype??
EDIT -
I needed to swap the contents of two void * pointers.
Got influenced by the regular way of swapping two values i.e storing the value of one pointer in a temporary variable and then proceed with swapping.
Was trying to attempt the same thing without knowing the datatype but only its size( since the function with this swap code accepts size of variable as one parameter ). However after reading the answers and comments, got to realize that I really dont need to dereference it. Probably memcpy is the correct way to go.
Suppose I dont know the final datatype, however I know the size of datatype. Can I still dereference it using only the size(4 bytes) and not the datatype(not using int)??
Since you cannot deduce or assume the data type, you can't dereference the pointer once to get the complete object. However, you can extract each byte and use it anyway you want.
void *ptr = call_a_function();
unsigned char* cptr = (unsigned char*)ptr;
for (int i = 0; i < num_bytes; ++i )
{
unsigned char c = cptr[i];
// Use c
}
In this case, the whole is not the sum of its parts. Casting not only provides the compiler with the size of the data but also how that data is to be interpreted. Suppose you get 4 bytes, this could be a string of three characters with a NULL byte or a signed integer, which could be encoded in big-endian or little-endian depending on the platform. So knowing the size is not sufficient to properly handle the data even if you could tell the compiler how many bytes to read/dereference.

The meaning of "asterisk type asterisk" in C : *(volatile int *) foo [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have tried looking around but was not able to find an answer for this. I found something explaining when we use a double asterisk, **, however, I am not sure whether this applied to my case.
I have come across embedded systems code which looks a bit foreign to me :
port0 = *(volatile int *)(0x1C002100)
what this operation is doing is that it reads the GPIO port whose address is 0x1C002100. However what is the deal with all those asterisks ?
I would have written this something like :
port0 = *0x1C002100
Are they doing some type of pointer type casting and hence we use 2 asterisks ? That would be my best guess. Thank you for your help !
Look at the expression, it first convert a integer constant to a pointer, then it deference that pointer, which will yield a integer. It is essentially the same as:
int *p = (volatile int *)(0x1C002100);
int n = *p;
port0 = n;
The first * denotes the pointer type, the second is a dereference operator.
However second line is invalid C code, since you cannot dereference a integer.
port0 = *0x1C002100;

Pointers in C with arrays [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Here is the code:
#include <stdio.h>
int main ()
{
int c[4][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11},{12,13,14,15}};
int (*p)[3] = (int(*)[3])(c+2);
return 0;
}
Its interesting that it sets p to be:
{{8,9,10},{11,12,13},{14,15,-8224}}
I do not understand what is happening, especially in the 6th line of the code. Please help me!
int c[4][4] declares a 2D array with dimensions 4x4.
int (*p)[3] declares an array pointer to an array of 3 elements.
(int(*)[3])(c+2) invokes a pointer aliasing bug, by treating the address of c+2 as if there was an array of 3 elements there.
Pointer conversions between type int (*)[4] (the type of c+2) and type int(*)[3] are not safe. In practice, most compilers will likely give you some deterministic result from this code, but they are not required to do so. The program may as well crash and burn and then that's the programmer's fault.

issue with length macro [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have a simple macro:
#define LENGTH(arr) (sizeof(arr)/sizeof(arr[0]))
and for whatever reason, it seems to work fine when setting something like:
int length = LENGTH(arr)
but not to compare with in a loop
while(i < LENGTH(arr))
For the same arr, the macro will either work or it won't.
The likely problem is that the arr in the loop is a pointer rather than an array. The macro does not work with pointers, it only works with arrays.
Remember that arrays decays to pointer when passed around. So if you pass an array to a function, it's not longer an array but a pointer inside the function. A pointer which have only have information about the type, but not the size of the array.
sizeof(arr) is not the same as what a more high level language gives you when you do arr.Count() or arr.Length.
This gives you the storage space of the arr variable. This does not mean the length of the array.
If you have char *arr = malloc(sizeof(*arr) * 100); and you do sizeof(arr) you will usually (depending on your system) get 4. If you do sizeof(*arr) or sizeof(arr[0]), which is the same thing, you have sizeof(char) which is 1, so the macro gives you 4/1 = 4 : wrong. It works with char arr[100] because sizeof(arr) gives you 100 as the array does not decay to a pointer type for sizeof.

How to edit the index of a pointer array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Ok so I am curious as to how you would go about using a pointer array to access a value in the index. Like for example:
printf("%c", (*character)[0]);
I know I have this code wrong, but I have know clue how to fix it. Say I want to access the 0 position in the pointer array and then print it like above, how would I do it?
Assuming character is char character[] = {'1'};
character[someIndex] means someIndex[character] means *(character+someIndex)
If that is what you wanted to know. So you should be doing something like:
printf("%c", *(character+0));
Which is equivalent to
printf("%c", *character);
printf("%c", character[0]);
Just missed out - regarding this statement
index of a pointer array?
Please know Arrays are not Pointers. If that is where you were confused.
From your question I observe that you have declare a character pointer something like this
char *character="something";
and now you want access its contents by indexing.
So why worrying,No matter whether you have declare pointer array or array.You can access its contents by any of following way:-
printf("%c",character[1]); //general method
or by pointer notation
printf("%c",*(character+1));
or
printf("%c",*(1+character));//commutative law
or more surprisingly you can use following method too
printf("%c",1[character]);
char *arr[20]={"Stackoverflow"};
This means you have an char type pointer array where 20 memory location's addresses are stored in an 20 size array. 1st memory location in arr[0] points to the string "Stackoverflow" and rest of them are not assigned(so you will get garbage value or maybe segmentation fault). Now, if you want to access 0th memory location just do this:
printf("%s\n",arr[0]);

Resources