For those new to message boards please try to follow a few simple rules when posting your question.
Choose the correct forum for your message. Posting a VB.NET question in the C++ forum will end in tears.
Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
Keep the subject line brief, but descriptive. eg "File Serialization problem"
Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
Be careful when including code that you haven't made a typo. Typing mistakes can become the focal point instead of the actual question you asked.
Do not remove or empty a message if others have replied. Keep the thread intact and available for others to search and read. If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you.
If you are posting source code with your question, place it inside <pre></pre> tags. We advise you also check the "Encode HTML tags when pasting" checkbox before pasting anything inside the PRE block, and make sure "Ignore HTML tags in this message" check box is unchecked.
Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
Please do not post links to your question in one forum from another, unrelated forum (such as the lounge). It will be deleted.
Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
No advertising or soliciting.
We reserve the right to move your posts to a more appropriate forum or to delete anything deemed inappropriate or illegal.
So itoa is really NOT "integer to ASCII" conversion, right? I knew it was a silly question. I suppose I can use some kind of lookup method the get from random index # to character array of alphabet. I'll check stdio. Thanks
I have access to array of pixel data (size:512x512x8bpp) from a framegrabber which I currently assign to a 8bpp bitmap which is assigned to a GDI+ graphics object later on.
Now, I would like to convert my array of 8bpp pixel data into 24bpp (BGRA, but I have no use for Alpha channel) and assign it to my bitmap and then modify individual pixel data to different colors depending on other conditions.
When I say efficient, I want it to be done in real-time without much delay.
Although I use C# most of the time, I try to use C for small exercises -- to keep my C sharp!
Anyway... I don't recall having to pass arrays back when I was using C full-time (in the 90s), so I don't know which technique is preferred.
As I see it, there are two basic techniques as precedents in C:
0) Passing the length of the array with the array -- e.g. main ( int argc , char *argv )
1) Having a "special value" at the end of the array to indicate the end -- e.g. NULL-terminated strings.
So, if I want to pass an array of structs (four ints), which technique is preferred by the C/C++ community?
I'm actually considering door number...
2) Using a linked list instead. It just seems cleaner, though memory usage is actually increased by 25% in this case.
Again, this is just an exercise, but practicing poor technique can be worse than not practicing at all.
So, what say you? If you have to pick door 0 or door 1, which do you prefer?
Which method you use depends on what your data looks like. If you have an array of int, then there may not be a special value that can be the sentinel for the end of the array. On the other hand if you have an array of pointers to something, then maybe a NULL pointer is a good choice as a sentinel. Not always, though. If you had an sparse array of pointers, it would be conventional for the empty elements to be NULL, so you could not use that as the sentinel.
Advantages to passing the length are that you can work your way forwards or backwards through the array without having to count how many first, and you know the bounds, so you shouldn't have any Undefined Behaviour from trying to access outside the array.
With these particular structs, there is a field in which a 0 (or anything less than 1) could easily be used to indicate that the item is the terminator.
And in this particular exercise, I need to visit each item once per call, so knowing the length would only keep me from going off the end, not aid in navigation.
I'll give door 1 a try. I think that will require only that I allocate one more item and make a small change to the for loop I would otherwise use.
Method 0 is the one I observe most of the time (and use myself more frequently). Even Windows API use it.
Sentinel method has its usages (k5054 already pointed out its drawbacks).
I won't use method 2, I mean I won't choose a linked list instead of an array just in order to avoid the extra parameter in my function.
The sentinel method worked well in this case, but then Chris Losinger suggested method 3 -- using a container*. So I'm using that now. This technique has the added benefit that I can store the number of items allocated as well as the number of items in use -- sort of like how a List works in .net. I can add items up to the limit (I have no need to expand the array; I know how many items I need up front).
* Didn't we used to call that a Control Block? A simple way to avoid having bullions and bullions of function parameters?
Didn't we used to call that a Control Block? A simple way to avoid having bullions and bullions of function parameters?
Of course I am aware of the general technique (pass a struct instead of tons of parameters, again Windows API docet), however it is the very first time I hear the term 'Control Block' used with such a meaning.
By the way, you are welcome.
Last Visit: 31-Dec-99 18:00 Last Update: 2-Aug-15 15:03