|
ultra-inquisitor posted:Virtual functions are designed to avoid horrors such as switch-on-type.
|
# ? Jul 2, 2010 14:19 |
|
|
# ? Jun 9, 2024 02:07 |
|
ultra-inquisitor posted:Virtual functions are designed to avoid horrors such as switch-on-type. It can be an issue if you have possibly multiple types of objects which don't share hierarchy or you need to make a special call thus the virtual won't match for all possibilities. This is most likely to occur in either input or interacting with OPC (Other Peoples Code). Examples would be libraries, certain rpcs, etc. Usually though, the above about victuals is correct.
|
# ? Jul 2, 2010 17:42 |
|
Hmm as Wikipedia shows it wasn't how it was originally represented to me, so I can only fault myself for bringing it up. Design wise I was trying to come up with something that would cause different things to happen based on a protocol from a stream. I wanted to do it in a way that there wasn't such a tight coupling between what was parsing the stream and what responded to it. So I thought I'd just pass a base class between them. Both ends would still need some awareness of what was really being passed, so I added a tag in the base class to help figure it out. I don't know what to think about the tag because without another level of inheritance, it could turn stupid. Say, there is little value in an object being purely base class A, so we strike that out. B inherits A, and we have a case for that. Say then C inherits B. Then it would be tagged as C, but code that works on B could have a say in what happens. So it's basically taking over inheritance in a kind of lovely way. That Turkey Story brought up trying to code C++ like Java, so here is a case where in Java one could just sprinkle instanceof around to some extent. That could tell if an object coming in as type A was really C, but also if it could be B. With my one-level tag, call I could tell is if it's A, B, or C.
|
# ? Jul 3, 2010 06:19 |
|
You could do double-dispatch: example.
|
# ? Jul 3, 2010 07:42 |
|
I'm really new to templates (and C++ really) and am having some problems. How do you define a function in a template class that takes an instance of another template class as an argument? I'm basically trying to create a 4-dimensional vector class to use with homogeneous coordinates, so I'd really like it to have constructor that takes a 3-dimensional vector and sets the 4th component to 1 as default. So let's say I have something like this:code:
code:
code:
Foiltha fucked around with this message at 13:33 on Jul 3, 2010 |
# ? Jul 3, 2010 12:13 |
|
Hmm, what error message are you getting? I tried your example code and it works for me.
|
# ? Jul 3, 2010 16:49 |
|
I'm getting some weird behaviour in the file-reading code of some C I'm writing. It's supposed to read every 4 bytes from a file and put them into an array (arrays[0]) of unsigned ints. This appears to work file for a 56KB file (though the program runs slightly incorrectly which might be as a result of this or, more likely, something in the rest of the program) however if I run it for something more like 3MB it seems to segfault having read about 128KB. Even weirder (or perhaps I don't understand gdb enough) the gdb backtrace seems to 'skip out' which part of the reading function (parse_and_load) is causing the problem. Here's the full output, including stuff printed by the code: code:
code:
|
# ? Jul 4, 2010 03:02 |
|
I suggest you learn to use valgrind...
|
# ? Jul 4, 2010 03:57 |
|
OddObserver posted:I suggest you learn to use valgrind... Ah, I'd heard about how good valgrind is but completely forgotten. Thanks, you've not only helped me solve the main problem I was having, but (I think) the one I mentioned in passing, too.
|
# ? Jul 4, 2010 04:13 |
|
code:
2. On the second run through the loop, 'temp' points to bufsize / 2 words, but you copy bufsize words from it. It probably fails at the point it does, because it's only then that you run off pages owned by your process. But even before then, it's grossly undefined. 3. On the second run through the loop, you copy data into a possibly NULL pointer (file, returned from malloc). ctz fucked around with this message at 15:34 on Jul 4, 2010 |
# ? Jul 4, 2010 15:32 |
|
ctz posted:
1. Yeah I discovered this function last night. Awesome. 2. Yeah, valgrind pointed this out for me, but thanks. 3. welp, thanks
|
# ? Jul 4, 2010 23:42 |
|
Hey guys, I've got a quick question about strings in C. I'm trying to make a function that based on the input, returns a string of text. printf();'s wouldn't work because I'm using the function to give me strings for stuff like button names and general text around the program, things that wouldn't always have the same position. I've looked around online about how C handles strings, and it looks like it uses arrays. It seems really inefficient to make a bunch of different arrays for 4 or 5 letter strings. I am using ncurses pretty heavily in this program, so maybe something can be done involving that but from what I know I'm drawing a blank. Thanks guys, this is a really useful thread.
|
# ? Jul 5, 2010 02:16 |
|
haximus prime posted:Hey guys, I've got a quick question about strings in C. snprintf if you need to return a formatted string quote:I've looked around online about how C handles strings, and it looks like it uses arrays. It seems really inefficient to make a bunch of different arrays for 4 or 5 letter strings. Probably no inefficiency to worry about. You can store them as literals in your program if there are just a few, and decide if you need to strdup what you return or not.
|
# ? Jul 5, 2010 03:25 |
|
haximus prime posted:I've looked around online about how C handles strings, and it looks like it uses arrays. It seems really inefficient to make a bunch of different arrays for 4 or 5 letter strings. You must be coming from another language where arrays are objects with overhead. A 5 character array in C takes exactly 5 bytes of memory.
|
# ? Jul 5, 2010 03:52 |
|
Also, don't forget that if you're just returning string literals, you can just return a pointer to that literal as much as you want, with no memory overhead beyond the initial declaration. Also also, come on, son: Paniolo posted:A 5 character string in C takes exactly 6 bytes of memory. Dijkstracula fucked around with this message at 04:06 on Jul 5, 2010 |
# ? Jul 5, 2010 04:02 |
|
haximus prime posted:I've looked around online about how C handles strings, and it looks like it uses arrays. It seems really inefficient to make a bunch of different arrays for 4 or 5 letter strings. I'm curious about how you would recommend handling strings.
|
# ? Jul 5, 2010 04:06 |
|
Pack 4-character strings into a single 32-bit integer for great justice. Don't actually do this.
|
# ? Jul 5, 2010 04:11 |
|
Paniolo posted:You must be coming from another language where arrays are objects with overhead. Mistakes like this are why newer languages have arrays as objects with overhead.
|
# ? Jul 5, 2010 04:13 |
|
The1ManMoshPit posted:Pack 4-character strings into a single 32-bit integer for great justice. I work with a FORTRAN program where lots of string identifiers are 4 characters because of this. The number one requested feature is to be able to have id's longer than 4 characters. It will take a massive overhaul to change them. The1ManMoshPit posted:Don't actually do this.
|
# ? Jul 5, 2010 06:37 |
|
quote:A 5 character null terminated string in C takes exactly 6 bytes of memory. quote:A 5 character string in C takes exactly 5 bytes of memory. Developing with messaging systems you unfortunately learn that some people really do use non-null terminated strings.
|
# ? Jul 5, 2010 07:19 |
|
Dijkstracula posted:Also, don't forget that if you're just returning string literals, you can just return a pointer to that literal as much as you want, with no memory overhead beyond the initial declaration. No you pedantic gently caress a 5 character array takes 5 bytes of memory. Don't quote me and then edit what I said and then correct your own edit. If I had wanted to say string instead of array I'd have said so. Paniolo fucked around with this message at 09:05 on Jul 5, 2010 |
# ? Jul 5, 2010 08:57 |
|
Sounds like it's nap time in the C/C++ thread!
|
# ? Jul 5, 2010 12:42 |
|
while we're being pedantics, C's char type cannot store a character. depending on encoding, a character may need up to 4 bytes (using common encodings). so how about : 'A 5 character array in C takes between 5 and 20 bytes of memory.'
|
# ? Jul 5, 2010 13:58 |
|
ctz posted:while we're being pedantics, C's char type cannot store a character. depending on encoding, a character may need up to 4 bytes (using common encodings). so how about : Some codepoints are just diacritics. It may take many more bytes than that.
|
# ? Jul 5, 2010 14:54 |
|
Null Pointer posted:Some codepoints are just diacritics. It may take many more bytes than that. And some are ligatures so it could be less! Let's end this once and for all: An n character array, where n > 0, takes up between 1 and ∞ bytes of memory.
|
# ? Jul 5, 2010 15:18 |
|
And this is why we should just call them chars.
|
# ? Jul 5, 2010 15:19 |
|
Mustach posted:And this is why we should just call them chars. You mean cars.
|
# ? Jul 5, 2010 21:43 |
|
shrughes posted:You mean cars. Char, so char star rhymes (and sounds awesome)
|
# ? Jul 5, 2010 22:49 |
|
shrughes posted:You mean cars.
|
# ? Jul 5, 2010 22:49 |
|
I know there's precision() for setting the precision of a stream, but is there a way to force it to report that amount of decimal places whether it needs them or not? I'm thinking of the stream equivalent of the "%.04d" sequence for printf and friends. I'm inclined to use the stringstreams with sprintf or something similar actually because I can't find anything that will add the zeroes.
|
# ? Jul 6, 2010 01:42 |
|
Rocko Bonaparte posted:I know there's precision() for setting the precision of a stream, but is there a way to force it to report that amount of decimal places whether it needs them or not? I'm thinking of the stream equivalent of the "%.04d" sequence for printf and friends. I'm inclined to use the stringstreams with sprintf or something similar actually because I can't find anything that will add the zeroes. Honestly, I'd just never use streams. At all.
|
# ? Jul 6, 2010 01:59 |
|
Rocko Bonaparte posted:I know there's precision() for setting the precision of a stream, but is there a way to force it to report that amount of decimal places whether it needs them or not? I'm thinking of the stream equivalent of the "%.04d" sequence for printf and friends. I'm inclined to use the stringstreams with sprintf or something similar actually because I can't find anything that will add the zeroes. code:
code:
|
# ? Jul 6, 2010 02:07 |
|
Rocko Bonaparte posted:I know there's precision() for setting the precision of a stream, but is there a way to force it to report that amount of decimal places whether it needs them or not? I'm thinking of the stream equivalent of the "%.04d" sequence for printf and friends. I'm inclined to use the stringstreams with sprintf or something similar actually because I can't find anything that will add the zeroes. If you're not adverse to boost, use the boost::format library. You can get the type safety of avoiding the printf-family of function but the convenience of printf-style format strings. It is compatible with stream objects. code:
|
# ? Jul 6, 2010 03:00 |
|
Ugh at the overloading of the modulus operator, among other things
|
# ? Jul 6, 2010 03:02 |
|
Otto Skorzeny posted:Ugh at the overloading of the modulus operator, among other things Agreed, but it is better than "fixed << setprecision(5)" in my opinion. Hopefully boost::format will get an overhaul once variadic templates are common.
|
# ? Jul 6, 2010 03:06 |
|
Flobbster posted:
I didn't know about boost::format. It looks like it mimics Python's format() command, with the way it overloads modulus. Edit: Actually I just looked back at that and realized I experimented on that with a piece of code I wasn't running. Doh! It looks like << fixed << setprecision does work. Rocko Bonaparte fucked around with this message at 03:48 on Jul 6, 2010 |
# ? Jul 6, 2010 03:45 |
|
Maybe one day somebody will invent something nice using initialization lists and we can just do something like Format("{0} {2} {1}\n", {3, "stuff", 2.0f})
|
# ? Jul 6, 2010 05:23 |
|
pseudorandom name posted:Maybe one day somebody will invent something nice using initialization lists and we can just do something like Format("{0} {2} {1}\n", {3, "stuff", 2.0f}) Simple variadics are all you need for something like that, and even better, suffix operator overloading as well so that you can parse that 1st argument at compile time in order to verify the number of arguments/types that follow and give a compile-time error if there is a mismatch. You'd do this by making a suffix that yields a compile-time string which you could parse with a metafunction and then have your format template use SFINAE and/or static_assert.
|
# ? Jul 6, 2010 06:44 |
|
Does anyone have experience with debugging CreateFile errors? I'm still working with Someone Else's Code (its supposed to do some networking thing that should be fairly simple) and I don't have enough experience to really know where to start looking. The function call itself is:code:
|
# ? Jul 6, 2010 14:40 |
|
|
# ? Jun 9, 2024 02:07 |
|
Captain Frigate posted:Does anyone have experience with debugging CreateFile errors? I'm still working with Someone Else's Code (its supposed to do some networking thing that should be fairly simple) and I don't have enough experience to really know where to start looking. The function call itself is: See GetLastError and this. Of course you would know this if you read CreateFile. So basically RTFM.
|
# ? Jul 6, 2010 15:05 |