C: Two Different Arrays Pointing to the Same Struct [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 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
}

Related

Is it better to use a pointer instead of returning a value? [closed]

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.

Array out of bounds 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 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.

Failure Injection:Try to write into protected RAM area [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 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.

Easy way to deal with free in an 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 6 years ago.
Improve this question
When I have a function that gets a pointer int *vector with a couple int values. I want to delete element number n. So I will use free() on that element. The problem I have now that there is a "hole" in my array of int values. Is there an easy way that I dont have this problem or do I really have to make a new int pinter and reorder my vector?
Given a function of this form:
void delete_element(int *vector, size_t index) {
// ...
}
The actual argument corresponding to vector is expected to be a pointer to a series of one or more (implied: index + 1 or more) contiguous ints. This could be part or all of an ordinary int array, or it could be a dynamically allocated block. If the former, then you cannot free any part of the space at all. If the latter, then you can free or reallocate the whole space, but not just the part associated with one element.
To avoid the deletion leaving a hole in your array, you need to move the later elements down, and to do that, you need to know how many elements there are in total. Therefore, you need a more informative function signature, perhaps something like this:
void delete_element(int *vector, size_t *size, size_t index) {
// ...
}
The actual deletion might involve simply using memmove() to move the later elements (overwriting the one to be deleted), and then decrementing the size. With respect to the latter, note that I suggest passing a pointer to the vector size, so that the function can modify the caller's copy.
If you want also to shrink the allocation then you need to do a bit more work (involving calling realloc(), and communicating the revised value of vector back to the caller), but note that in that case your function will not work on ordinary arrays.
There is no way to free() part of a block returned by malloc(). If you want to delete record[n], you need to copy record[n+1]...record[last] into the array.
If you really need to free() each element, you must first malloc() each element.

how to initialize and use a 2d struct 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 8 years ago.
Improve this question
So what i am trying to do is have an array of lists, here is my code:
typedef struct stackList{
List * list;
} stack;
int main(){
int x;
stack ** stackTable;
for(x=0;x<100;x++)
stackTable[x]=malloc(sizeof(stack*)*100);
}
i get a segmentation fault on the for loop, i would assume the way i am trying to use the struct is wrong. Would i rather in the defintion of the struct use List ** list;
or is there a way to use it the way i am trying to use it
You get segmentation fault because you're accessing stackTable while it is uninitialized. You can't know to what address of memory it points, and you haven't allocated an array to hold the pointers that you are dereferencing.
You need to make stackTable point to a valid array of pointers, in this case I think is convenient to make it be an array:
Stack* stackTable[100];
Now you have an array of pointers to Stack, you can initialize them.
If instead you have just temporarily an array large 100, and you need to make it grow in future, that's how dynamically allocating it:
Stack** stackTable= malloc(100*sizeof(Stack*));
Before trying too hard to play with pointers and dynamic memory I might suggest writing some basic programs using basic 2d arrays. For instance:
char array2d[10][10];
Once you're confortable inserting elements into this array, extracting elements, etc, you can apply all of the same principles to any type.

Resources