Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I was wondering if there is a possibility to shift a whole block in an array. I intend to delete one item in an array of dynamic length, and after I deleted it, I want the whole block to shift to the right. So far I do this element, by element, but this is not efficient. So I was wondering if there is a better solution.
Important: the order of the elements needs to be kept the same.
Using memmove will probably be more efficient than doing an element by element copy as then you can take advantage of the fact you're doing a bulk move rather than lots of little moves (and compilers often provide highly optimized memmove implementations), but that's about it. You need to move everything around in memory, so you're going to have to move it.
If you're doing this a lot with your arrays, it probably means you need a different data structure.
You can use linked list instead of array. In linked list deleting is easy. You just have to point the next next elements.
Use a ring buffer, like this:
unsigned idx;
int Buf[1024];
void EnterValue(int value)
{
idx++;
Buf[idx & 1023] = value;
}
int GetOldValue(unsigned age)
{
assert(age<1024);
return Buf[ (idx-age) & 1023 ];
}
Related
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 1 year ago.
Improve this question
What's up?
I am very interested in the question of how to add a new element to my array
uint32 glfw_extension_count = 0;
const char** glfw_extensions;
glfw_extensions = glfwGetRequiredInstanceExtensions(&glfw_extension_count);
return glfw_extensions;
I just need add to 'glfw_extensions' VK_EXT_DEBUG_UTILS_EXTENSION_NAME
The (great!) documentation says:
Pointer lifetime
The returned array is allocated and freed by GLFW. You should not free it yourself. It is guaranteed to be valid only until the library is terminated.
So clearly you can't do the necessary re-allocation. So you must allocate a new array, copy the data in, and then append your additional entry. I would assume that you can safely copy the string pointers themselves, didn't see a lot of discussion about that in the documentation page so it might need further verification.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I'm facing an issue on a PIC from Microchip but my question is more general. This will help me check if it is an issue from my code or from the debugger of MPLAB X which is sometimes buggy.
So here is my question, let's say I have this function:
int test( int *array )
{
int a = array[0];
return a;
}
Now, if I want to pass an array it's working, there is no issue. But let's say I want to use a single variable instead of the array like this:
int main()
{
while(1)
{
int test_variable_1 = 8;
int test_variable_2 = test( &test_variable_1 );
}
return 0;
}
I did not use an array but for me this should work as expected since the test_variable_1 is like an array of size of one int.
EDIT QUESTION: So, is it possible to pass a single variable like this (using the pointer with &) instead of a real array into an function? Also, is it a good/bad practice?
Thanks in advance and don't hesitate to tell me if I'm unclear.
Have a good day!
Adrien
So, is it possible to pass a single variable like this (using the pointer with &) instead of a real array into an function?
Yes. In fact C doesn't even know that you passed an array. For details, see Do pointers support "array style indexing"?
Also, is it a good/bad practice?
It's fine. Though ideally the function should take the number of items as one of the parameters passed.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a simple text editor. I'd like to add a C-like /* comment blocks */ colorizer to it.
I mean multi-line comments of course.
Its data structure looks as follows:
struct TextEdit
{
struct Line
{
char* str;
int len;
};
Line* index;
int lines;
void Modified( int line_from, int line_to );
bool IsInBlockComment( int line, int column );
};
Modified() is called every time the text is modified so the editor has a chance to re-scan the contents of the modified range of lines.
How would you implement the IsInBlockComment() method which can determine if a given position in text is inside a block comment?
What extra data should be added and maintained by Modified() to be able to do it efficiently?
Important detail:
/* and */ tokens should not be in effect if they occur after a // comment or inside "" and '' literal strings.
I'm not asking for working source code or detailed specs, I ask for a brief concept only.
First of all notice that C supports multiline strings, so you already had this problem, just not addressed it.
Your parser engine has a state. You have to know whether you're inside a literal string or a C like comment, etc.
Store this state for the starting position of each line. This way the parser can pick up where it left off.
If the new state of the the next line differs from the old one, you have to continue parsing, otherwise you can stop.
By controlling the frequency of such state snapshots (e.g.: storing it only for every 10th line) you can control the trade-off between speed and memory footprint.
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 9 years ago.
Improve this question
Hi friends I was working on a project where we need to use quite a few multidimensional arrays. I am using for loops to access different array elements, then I just thought what if I don’t have a liberty to use looping? How am I going to access array element?
I am new to C, so thought of discussing here, I am sure there might be thousans of people who could have thought the same way, and hopefully found the solution.
Below example of multidimensional array is give, please guide me.
Thanks
static int t[3][2][4] = { {2,4,3,6,
1,6,7,9,},
{8,2,1,1,
2,3,7,3,},
{1,6,2,4,
0,7,9,5,},
};
Please Help me...thanks!
If you need to go through all of the values inside the loop without manual handling (i.e. x = t[1][1][1] then x = t[1][1][2] etc) then you want to use loops, enhanced loops or iterators. However since you're using C the only of those three options available are standard loops, which you're trying not to use. So... there's mo straight forward way to do that really.
If you're willing to use some other C libraries however then there may be more options for you. Iterator libraries probably exist.
A non-straightforward way to do it (if you're looking for one) could be through recursion, however that's really quite wasteful. I advise you just use loops :P
What are you trying to prove with loops and without loops should be first thought.
If u want to access all the elements and not use loop is like writing a lot of code manually and waste of memory(in your case 3 * 2 * 4 no of lines instead of few ).
Instead of showing the array if you had put in your code how and where you accessing elements it would have been more clear to tell what you wanted .
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 a function
void join(table first, void(*pfapply)(const char *inp1,const void *inp2,void *addn),const void *addn);
here first is a instance of structure table as below
struct table
{
char inp1;
int inp2;
}struct table *first;
Now how to add this extra variable "addn" in this "join function"
In my symbol table i have two values inp1 and inp2..how to add addn to my structure using the above function?
I need to bind addn to the two variables inp1 and inp2...
The addn is probably just the API being friendly and providing support for an additional parameter to the pfapply() callback.
This is a sign of good design, since it allows the callback to access some of its data without having to make it global.
If you don't need any additional data, just ignore the argument inside your apply-function, and call join() with NULL as the third argument.
You don't need to change anything in the declaration of table, all is fine.