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 3 years ago.
Improve this question
I want to know how I can catch IndexOutofbounds in C.
I do not want my program to exit abnormally, I want to print a message for the user that clarify the error
how I can check for this case
char a[50];
fgets(a,200,stdin);
I need to exit the program and throw an error,and I do not need to change the 200 to use sizeof()
This is one of those things you simply cannot do in C. At least not without a lot of hazzle. You will have to keep track of such things yourself. So when you declare an array, then you will have to store the size and do something like this:
size_t size=1000;
int arr[size];
...
if(i>=size || i<0) {
// Handle error
} else {
// Do something with arr[i]
}
You can make abstractions and make it more Java-like with constructs like this:
struct array {
int *val;
size_t size;
};
int getVal(struct array array, size_t index) {
if(index>=array.size || index<0)
// Handle error
else
return array.val[index];
}
But if you are using constructs like that, chances are high that it might me a good idea to switch to another language instead.
If we look at your particular example:
char a[50];
fgets(a,200,stdin);
I'm sorry to say it, but it is impossible to make fgets throw an error in this situation.
First of all, C doesn’t have a structured exception handling mechanism. There’s no try...catch control structure in C (there is in C++ and C#, but those are different languages).
Secondly, C doesn’t do any bounds checking on array accesses - you, the programmer, are expected to do those checks yourself.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 months ago.
Improve this question
I was wondering what is the best way to get a value from a function.
A function can return a value like an int for example.
But you can also change the value of a variable with a pointer passed as a parameter to the function.
See below, two examples of codes that do this in two different ways, but produce the same result.
int example_return()
{
return (1);
}
int main(void)
{
int value;
value = example_return();
}
void example_ptr(int *a)
{
*a = 1;
}
int main(void)
{
int value;
example_ptr(&value);
}
Is there a real difference between the two options, which is the best way?
Side effects should be avoided if not absolutely needed. If you can return the value - return it. Basically, the function should a black-box which does something with parameters and returns the value.
Why:
It is much safer. You definitely will not invoke undefined behaviour.
It helps the compiler to optimize the code.
In your example -
int example_return(void)
{
return (1);
}
It depends on your purpose, If you have a pointer to data, then you have two levels of memory access. First to load an address from the pointer and second to actually load the data. If you simply directly reference a variable, there is only one level of memory access.
there are some advantages of using pointers ( Memory sharing, Runtime-sized data structures, and Resizable data structures).
most of the time pointers reduce the complexity of a program.
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 5 years ago.
Improve this question
I'm working on an home project to learn more about embedded systems. So I'm also not professional with C language :)
Lets say I have a struct:
static st_struct myStruct;
The struct is defined as:
typedef struct
{
int a;
long xy;
} st_struct
To keep the code simple here, the struct is defined and only valid within a protected RAM area, in the address space from 0x04001000 - 0x04003000.
This protected area prevents other tasks to write into it, they can only read.
If some task/function is trying to modify or write into this area, the CPU will reset.
The variable myStruct is located within address 0x04001f15.
I would like to provoke this behavior of trying to write into the "not allowed", exactly to the myStruct variable. What would be the best way to implement such kind of failure injection in this example? Can you give an example how to do this with pointer arithmetic?
From what I understand you want the code for the other application so produce the faulty behavior.
In the other process/program do this
typedef struct
{
int a;
long xy;
} st_struct;
st_struct *ms = (st_struct*) 0x04001f15;
ms->a = 0;
ms->xy = 0;
This way the other application will try to access the same struct and will fault.
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 am working on a problem where I have a "master array" of pointers to a typedef struct programs. For each item/structure that I create, I allocate memory to a programs* temp variable and store it into an array programs* master_array[x].
However, my problem requires me to handle errors in the master_array where if a program goes into a "blocked" state, then I have to manage that(those) process(es) separately.
I want to create a second array called programs* blocked_array[y] and store programs that are blocked in there. In this case, I can have two arrays pointing to the same program. However, when I am finished with handling a program in blocked, how can I deallocate it/dereference the blocked_array[y]'s pointer to that program without impacting the master_array[x]'s pointer?
Do I create an empty program temp, not allocate memory to this temp program and make the blocked_array[y] point to that temp program to effectively, empty out blocked_array?
Would this create some sort of unintended consequence or does doing this stop the blocked array from pointing to it while preserving the master_array[x]'s pointer? Any thoughts would help, thanks.
You can simply add a bool isBlocked; field in your struct programs, and then you can handle this with a single array by
if(master_array[x]->isBlocked)
{
// Do something
}
else
{
// Do something else
}
Alternatively, you can use another array to store "isBlocked" information:
bool isBlocked[sizeof master_array / sizeof master_array[0]] = {0};
...
isBlocked[x] = true;
...
if(isBlocked[x])
{
// Do something
}
else
{
// Do something else
}
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++).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I use C language for a natural language processing project.
I would like to store a dictionary file.
I used the following define statement
#define DICSIZE 46000
The question is about the number 46000 because it is the maximum number I can enter.
If I try a bigger number the program stop running.
How can I solve this problem?
Program stops not because of DICSIZE macro. It's usage.
I guess, some array is allocated locally(i.e In stack) by passing this macro as array size.
int myArray[DICSIZE];
So when the number is increased, you may face problem. I suggest to allocate memory dynamically using malloc().
I suspect you have a large array declared locally in a function like this:
int main()
{
MyRecordType myArray[DICSIZE];
...
return 0;
}
When DICSIZE gets large, myArray gets large, and you run out of stack space.
Use dynamic memory allocation instead:
int main()
{
MyRecordType * myArray = malloc(DICSIZE * sizeof(myArray[0]));
assert(myArray);
...
free(myArray);
return 0;
}