Vector implementation in C [closed] - c

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I tried googling it, but no solid answer. What are available, still maintained dynamic array implementations for C? What are pros and cons for every one of them, and what is the best one(speed/footprint ratio)? Just asking, so that I don't have to reinvent the wheel.

GArray from GLib does what you want.

If you are looking for something like NSMutableArray, from Objective-C, or something like ArrayList from Java, you won't find anything (std C, at least).
You can create your own dynamic array implementation in C, though. It will take you a few code lines and is not that hard to implement.
All you need to have in mind is Time vs Memory. You can do an implementation that allocates a new array with a bigger size, every time you push/add an element, and then pops it for you in the return or by reference, or you can reallocate memory every time. I don't see big advantages in neither one, except that realloc is a C library function that I think is low level implemented, meaning it is probably faster, and in matters of implementation I would go with the realloc one since it is faster to implement.
You can even build an api that gives you sorting types and clean all methods.
Now is up to you.
Hope this helps.

Related

Why is the "FILE" data type in C not simply written as "file"? [duplicate]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
This just seems odd to me, most other things are lower case.
Is there some historical reason?
It's a macro. Macros have historically had all caps in C. Unfortunately recent trends appear to have broken that fact.
Small history lesson: Also, FILE was an io buffer abstraction in UNIX v7 libc stdio. A FILE doesn't necessarily represent a physical file, just something that can do block IO. Source:
http://www.bsdlover.cn/study/UnixTree/V7/usr/include/stdio.h.html
"file" was already defined by the kernel as well:
http://www.bsdlover.cn/study/UnixTree/V7/usr/include/sys/file.h.html
As someone else said here it's probably a typedef now, but I don't think C had typedefs back in '79 as it only just had structs. Then again I wasn't born then so... :)
It's all caps almost certainly because at least originally it was a macro. Nowadays, chances are pretty good that it's a typedef instead, but nobody changed the name to boot (and doing so would be a pretty lousy idea).

Is Linked List still relevant? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
I came across this article: Should you ever use Linked List. It cites that given the technological advances in available memory and RAM structures, using arrays would be better than Linked List.
There's also an old question When to use a linked list over an array/array list?
Do the arguments in the article really hold and are/have Linked List become obsolete or what would be the scenarios where using a LinkedList would still be better than Arrays if the arguments are true ?
(Explainations for any point with example would be helpful)
Nonsense. O(n) will never beat constant-time. Any use of lists that's required to perform well for insertions with saved iterators will use linked lists. They're a fundamental structure and won't go away.
I'd spin the argument the other way: linked lists are more acceptable these days. On a 386, you have to be careful with performance, but now, we write programs in Python even and put up with their speed. From the amount of code written in languages that use a VM (or are interpreted) I think it's fair to say a lot of people aren't at the level of worrying about cache misses in their choice of data structure.
We have fast CPUs now, so often don't need to worry about the few extra instructions that might be needed in implementing our data structures. We can look at the uses we have, work out what requirements we have and pick our structures based on their asymptotic performance. This also makes the code more maintainable: you won't have to change in your code if you find out in six months' time that for n=100 list is quicker after all. Profiling is hard work, so we should be very comfortable in our CPU-guzzling days to pick the structure with the algorithmic properties we want rather than guessing at vector.

Which basic C library functions can I implement to practice and improve my skills in C? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I know it sounds kind of subjective, but I want to implement basic library functions in C in order to gain better understanding in C (mostly in pointers) and improve my skills, since I'm only a beginner (in C & programming in general).
I also find it useful since you get to understand better what happens behind the scenes and how those basic functions work.
Which basic C library functions can I implement? I'm looking for simple ones like strcpy, atoi, strstr that are not too complicated (I'm only a beginner) but still require some thinking.
Strings function are always challanging (and required). Try split a string to tokens, join strings, replace string in strings. Believe me, after that not only you will be a better C programmer, you will also appreciate open source libraries! (and C++ STL..)
Good luck!
After having implemented strtok(), you may want to switch to next level with qsort() and its function pointer.

C if else condition [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
In C if i have only one line in the if condition then can i combine it into one line to reduce the size of code. The size of code is long so I intend to take this step to reduce size of code. Should I should I not? Is it a good programming practice?
It makes no sense to be concerned about the size of the sources at the point of sacrificing readability, especially since sources are extremely small compared to almost any kind of other data that our computers usually process/store (e.g. the bzipped sourced of the Whole Firefox are 85 MB - smaller than any medium-length video). Also, omitting a newline won't change the compilation times of a millisecond.
So, if you prefer one-line ifs for your own stylistic reasons it's fine (although it's often frowned upon), but for saving a few bytes it makes no sense at all.
Actually if the code is long then it's not good programming practice as that code will not be as readable.
so it's better to use if else syntax to make your code readable

Why is FILE all-caps as in FILE*? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
This just seems odd to me, most other things are lower case.
Is there some historical reason?
It's a macro. Macros have historically had all caps in C. Unfortunately recent trends appear to have broken that fact.
Small history lesson: Also, FILE was an io buffer abstraction in UNIX v7 libc stdio. A FILE doesn't necessarily represent a physical file, just something that can do block IO. Source:
http://www.bsdlover.cn/study/UnixTree/V7/usr/include/stdio.h.html
"file" was already defined by the kernel as well:
http://www.bsdlover.cn/study/UnixTree/V7/usr/include/sys/file.h.html
As someone else said here it's probably a typedef now, but I don't think C had typedefs back in '79 as it only just had structs. Then again I wasn't born then so... :)
It's all caps almost certainly because at least originally it was a macro. Nowadays, chances are pretty good that it's a typedef instead, but nobody changed the name to boot (and doing so would be a pretty lousy idea).

Resources