|
Zakath posted:You're right, it was just what led me to this, and has no bearing on my question. Use a Microsoft compiler.
|
# ? Feb 29, 2012 04:57 |
|
|
# ? Jun 1, 2024 17:51 |
|
I need a refresher. The size of a pointer or reference is small, as it only represents the memory address of whatever it's pointing/referring to right? For example; say I have some object A that takes up 25MB of space on the stack. code:
A* would only take up what little space a memory address takes up while new A would take up the 25MB. Then, this variable myPointerToA would also take up whatever small space a memory address takes up too, correct? code:
|
# ? Feb 29, 2012 06:52 |
|
Princess Kakorin posted:EDIT: And what's the deal with void pointers? I've read up on them, but don't quite grasp them. A nice example of where this is useful is memcpy, which takes a void pointer destination and a constant void pointer source, and the reason for that is that memcpy is a legal function to call on any type of data, whether it's a number, a structure, or array of values. "new" is an allocation operation, it allocates a block of memory and returns a pointer to it, so the result of "new A" is actually a pointer of the type "A*"
|
# ? Feb 29, 2012 07:39 |
|
Princess Kakorin posted:The size of a pointer or reference is small, as it only represents the memory address of whatever it's pointing/referring to right? Right on both counts. The size of B::myPointerToA will be whatever the pointer size is on your system, not the full 25 megabytes of an A. Princess Kakorin posted:EDIT: And what's the deal with void pointers? I've read up on them, but don't quite grasp them. Someone else can probably give a better explanation, but basically it's just a pointer to a block of memory without any context of what's stored there. You'll generally see it in C functions that take a generic argument (ie: qsort()), or if you're implementing some sort of opaque type where you don't want the user to know or care about implementation details. The first example of an opaque type that comes to mind is the Windows HANDLE typedef, which is just a void*. The functions that return them don't want you modifying their underlying implementation at all, so they return a type that has no context information at all. It's up to functions that accept a HANDLE to cast it to the type that they expect - if you pass it the wrong type, it'll compile, but fail at runtime since the cast produced the wrong result. The C qsort() function is similar, in that the predicate function you supply has to accept two void pointers, and the function is responsible for casting them to the correct type - in this case you're not hiding the implementation, but it allows the library code to not care about it. w00tz0r fucked around with this message at 07:49 on Feb 29, 2012 |
# ? Feb 29, 2012 07:47 |
|
Cyril Sneer posted:Can you elaborate on this a bit? Certainly. If you grab the free edition of KomodoEdit and install it, you can open remote files like this: File->Open remote, gets you to ... this - where you have to set up your server info the first time. To do that, click the appropriate button, and you get this: Fill it out with the same info you'd use to ssh in. If you don't know the appropriate default path, leave it empty - it should work out anyway. If sftp doesn't work, try scp. Save, and in the open dialog pick the server you just set up - you're now browsing the remote end. Pick the relevant file, and you can edit it. ( I think what technically happens is that it grabs a local temporary copy and opens that, and saving uploads it back on top of the original.) Like so. Ignore my personal-use-only quality C. (Looking at that - WhyTH am I using an uint64_t instead of an int? None of those quantities would ever overflow a signed 32-bit int, and file descriptors are standardized as "int" specifically. Oh well, it does work, and I do other things further in that genuinely requires ints larger than 32 bits. And I love the error handling in the no-arguments case - segfaults count as informative error messages, right?) Computer viking fucked around with this message at 16:53 on Feb 29, 2012 |
# ? Feb 29, 2012 16:43 |
|
Strangely the best free way to transparently edit remote files on Windows I've found is using Notepad++ with the bundled NppFtp plugin, which supports SFTP. I'm not a huge fan of Notepad++ in general but that particular feature is really nice, and Notepad++ is pretty lightweight.
|
# ? Feb 29, 2012 21:26 |
|
Volte posted:Strangely the best free way to transparently edit remote files on Windows I've found is using Notepad++ with the bundled NppFtp plugin, which supports SFTP. I'm not a huge fan of Notepad++ in general but that particular feature is really nice, and Notepad++ is pretty lightweight. Heh, I use notepad++ now and then, and I hadn't noticed that. Useful.
|
# ? Feb 29, 2012 22:05 |
|
I am new to this forum, this is actually my first post, I read a lot but never say anything. But to get to my post. I am making a web server in C and just having a hell of a time getting even simple pages like Google.com to load into my browser. I have it able to load in the basic html of the Google.html I downloaded but it wont load the pictures for some reason. here is the area of my code that gets and sends the file to the browser. code:
I have my Google.html and the file with all of the images for the Google.html in the same directory as my executable. Any help is much appreciated!
|
# ? Mar 1, 2012 01:10 |
|
Uhm. a) Why are you testing with google's html? Write yourself a test page, it'll take all of 20 seconds and be much easier to test with than the spectacularly unreadable google front page. b) Won't all the img tags on there refer to specific, different, servers? If you want to dump the images in the same folder as the html file, they have to be on the form <img src="file.png" />, without any domain names. As far as I can see, that's not the case on the google frontpage.
|
# ? Mar 1, 2012 01:16 |
|
Came across this in a networking class today while the teacher was just trying to demonstrate overflow: C: code:
I guess it would have been worthwhile to try and access an eleventh element of a. That definitely would not have worked, right? ...right? I'm wondering if the fact that it's printing is more to do with puts() caring about a null terminated string more than it cares about a, and if it would have stopped sooner with printf(). The main problem, though, is that strcpy() was ok with putting more than 10 characters into a. That's really what's throwing me off. Neat Machine fucked around with this message at 16:00 on Mar 1, 2012 |
# ? Mar 1, 2012 15:46 |
|
Awesome Andrew posted:This...worked. How did this worked? (There was no overflow, it printed just fine.) I can imagine how, conceivably, it might go. Perhaps the memory after a was not allocated so it just went ahead and threw the characters there and didn't complain (same with b). When puts() looks at a, it reads through but doesn't encounter a terminator until the end of "Mary had a little lamb." so it stops there. But I mean...I just didn't expect it to happen like this. How was this handled exactly? 1. The string constants were allocated next to their respective arrays, and plowed over when as they were being copied from. If so, a second pass over that code should show corruption. 2. There was enough space put between a and b that there was no interference. The most certain way is to view it in the debugger. Say, have it dump the member some bytes before and after "a" so you are sure you catch it all. It may have just organized it in memory so that there was enough room to catch the extra space--just dumb luck there. That simple code block might have worked but it's a ticking time bomb in a larger program.
|
# ? Mar 1, 2012 15:59 |
|
Rocko Bonaparte posted:Some speculations: If it's considered empty, what happens when something tries to put new data there? Is it just replaced (now puts(a) gives something different) or is there some kind of conflict?
|
# ? Mar 1, 2012 16:05 |
|
If it makes you feel better, it does crash compiled with clang. Edit: Or with gcc -O3 From what I can tell, gcc just makes your char arrays a bunch bigger than you asked for. Vanadium fucked around with this message at 16:19 on Mar 1, 2012 |
# ? Mar 1, 2012 16:08 |
|
This is what gcc -S --no-builtin produces (by default it uses a built-in version of strcpy which would make the asm more confusing to read):code:
|
# ? Mar 1, 2012 16:24 |
|
Awesome Andrew posted:Came across this in a networking class today while the teacher was just trying to demonstrate overflow: In C, arrays can decay to pointers. When you call puts() there, puts() doesn't see a as an array of ten chars, it just sees an address in memory pointing to the beginning of a. It'll just keep writing until it hits the terminator. There's no checking, and in fact, there's no way for strcpy() to know how long it's safe to write for. It definitely would work to try to access an eleventh element of a. If you're just reading from it, odds are you'd be fine. C doesn't do any bounds checking of arrays. The word "array" is maybe a little heavier than what C really does. When you type "char a[10];", you are asking the compiler to set aside 10 bytes on the stack for you, and "a" stores the beginning of that chunk of memory. It's not really storing the length of "a" anywhere; it's up to you to keep track of that. It's not 100% true that arrays are pointers, but in this case, that's how you should think of it. When you pass "a" to strcpy(), it gets no information about its length, just where it starts. What happens if you write beyond the end of an array? It depends where the array is in memory. That depends on a bunch of things, including some practically random things that might vary each time you run your program. In this case, the arrays are on the stack. You might end up writing over other variables in your function. You might also do something crazier. There's a type of security vulnerability that can be exploited in this way. When you call a function, that function needs to store the address to jump back to when it returns. It puts that address somewhere on the stack, and if that gets overwritten with something else, rather than returning to where it came from, it'll jump to that address. It's possible to exploit programs that have these kinds of bugs by causing them to jump to somewhere else in memory that has code that does what the attacker wants.
|
# ? Mar 1, 2012 16:25 |
|
Man all of this talk reminds of an phone interview I think I bombed. They wanted to know what would happen if I returned a pointer to a stack variable in a function. I think the answer they wanted was "instant crash," but I told them instead was a ticking time bomb based on how it was compiled and the system's memory management rules. So I was trying to go through one way where it would crash the first time the referenced data was touched outside the function, and I don't think they wanted any of it. These folks weren't explicitly using x86 or some particular OS so I couldn't just peg it to one thing or another.
|
# ? Mar 1, 2012 16:50 |
|
Rocko Bonaparte posted:Man all of this talk reminds of an phone interview I think I bombed. They wanted to know what would happen if I returned a pointer to a stack variable in a function. I think the answer they wanted was "instant crash," but I told them instead was a ticking time bomb based on how it was compiled and the system's memory management rules. So I was trying to go through one way where it would crash the first time the referenced data was touched outside the function, and I don't think they wanted any of it. These folks weren't explicitly using x86 or some particular OS so I couldn't just peg it to one thing or another. If that was actually the answer they wanted you probably didn't want to work there anyway. Given most systems (with virtual memory) use 4k pages and given most stack frames are small, an instant crash would actually be fairly unlikely if it was dereferenced right away.
|
# ? Mar 1, 2012 18:32 |
|
Paniolo posted:One last strange thing I found is that if you declare, but don't define, a copy constructor, you'll get a link error, even though it's never actually referenced anywhere in the generated code. Aww, I missed an EH implementation question. At least I can still clear up this little mystery. Short answer: MSVC stores a pointer to the copy constructor in global metadata associated with the thrown exception type. Long answer: MSVC's C++ exceptions runtime is very "active", in the sense that a lot of the detail work of unwinding the stack and managing the exception object is managed directly by the runtime, rather than by extra code emitted for the function being unwound. For example, when you're unwinding through a call frame, you have to call all the destructors for locals and temporaries. In MSVC's design, the PC within a function being unwound is mapped to an entry within a "scope table" specific to that function and held in global constant memory; the table tells the runtime what nontrivially-destructible objects are active and what destructor to use for each; the runtime just walks that list and calls the destructors. As a contrasting example, the standard GCC/clang "personality function" turns that same PC into the address of a "landing pad" within the function; it then just resumes execution at that new address, trusting the function to do the right thing and then return control to the unwinder. (There are other huge differences, but it's this philosophical difference that's most relevant here.) If you apply that philosophy consistently, it follows that the exceptions runtime should also be directly in charge of managing the catch variables. Since an exception might have class type, possibly including non-trivial copy constructors and destructors, the runtime has to know how to copy and destroy the object, just in case a function catches the exception by value. In fact, it has to know how to copy and destroy objects of every possible type that can catch the exception. This is all stored in global constant memory, emitted whenever you throw an exception of a specific type, and a pointer to it is passed as the second argument to CxxThrowException. It's the reference from this metadata that's presumably failing to resolve and giving you your linker error. Interesting historical note: the fact that MSVC stores a pointer to the copy constructor with the exception and Itanium doesn't actually caused a pretty lively controversy during the design of C++11's std::exception_ptr. Some people were pushing for a guarantee that capturing an exception_ptr *had* to copy the exception. The MSVC people were happy with that because (1) they were already capturing the copy constructor and (2) they had to copy the exception anyway because it was allocated on the stack. We Itanium people were less pleased because it was impossible to satisfy that guarantee without breaking ABI, plus the copy isn't mandatory for us because we always heap-allocate exceptions. Eventually the committee picked language which let us maintain ABI but made some of the people pushing for exception_ptr kindof unhappy.
|
# ? Mar 7, 2012 09:46 |
|
rjmccall posted:In fact, it has to know how to copy and destroy objects of every possible type that can catch the exception.
|
# ? Mar 7, 2012 17:11 |
|
Plorkyeran posted:Every possible type that can be thrown as an exception? Or am I completely misunderstanding what you're saying. Every type that can catch the exception. So if you've got throw iguana_exception();, the exception metadata emitted for type iguana_exception has information for how to copy-construct an iguana_exception, a reptile_exception, a vertebrate_exception, and an animal_exception, just in case you catch as one of these by value.
|
# ? Mar 7, 2012 18:48 |
|
Oh, that makes sense. I was reading it as "every type that has a catch clause in one of its methods which could match it" which made zero sense.
|
# ? Mar 7, 2012 19:15 |
|
We're currently evaluating embedded C/C++ webservers at my job and I was wondering if anyone has any recommendations. One of the things we would like to be able to do is some form of long polling/Comet-style stuff. Granted, that's something that can be built on top of any web server, but I was wondering if there are any embedded servers that allow this to be done without too much fuss (i.e. I'd prefer not having to build the entire server-side of the Comet backend myself).
|
# ? Mar 9, 2012 10:51 |
|
Sagacity posted:We're currently evaluating embedded C/C++ webservers at my job and I was wondering if anyone has any recommendations. One of the things we would like to be able to do is some form of long polling/Comet-style stuff. Granted, that's something that can be built on top of any web server, but I was wondering if there are any embedded servers that allow this to be done without too much fuss (i.e. I'd prefer not having to build the entire server-side of the Comet backend myself). I don't know what makes a server "embedded", but I think the canonical good C/C++ webserver right now is nginx.
|
# ? Mar 9, 2012 12:03 |
|
shrughes posted:I don't know what makes a server "embedded" code:
|
# ? Mar 9, 2012 13:15 |
|
I've just finished porting my own to C++ but I'm thinking of picking up the Chromium's test server instead as they have WebSocket support: http://src.chromium.org/viewvc/chrome/trunk/src/net/server/
|
# ? Mar 9, 2012 16:31 |
|
Sagacity posted:Well, basically being able to link to a library that allows me to programatically say something like: That is a really strange definition of embedded. Anyway, all of the popular http daemons right now are written in C, including Apache, Comanche, Lighttpd and nginx. But that code looks suspiciously like it came from Sinatra, and I think if you are going to be actually writing your code in C/C++ you are going to have a difficult time finding something that will allow you to define routes and views like that.
|
# ? Mar 11, 2012 06:35 |
|
I recently did a project that required embedding a HTTP server and I used Mongoose. Worked well enough, and I think it will work just fine for Comet-style applications so long as the number of concurrent requests is fairly small. (It uses a one thread per request model, rather than being event driven, so with a lot of open requests you'd run out of threads for new connections after a while.)
|
# ? Mar 11, 2012 08:51 |
|
ComptimusPrime posted:That is a really strange definition of embedded. I'm not saying it absolutely HAS to be that high-level. Paniolo's suggestion (Mongoose) also seems to work quite well. It's just that I was curious if there were any servers that allowed a higher level of abstraction (like, indeed, basic routing and auth) than raw HTTP. I mean, there are plenty of solutions for other, admittedly higher-level, languages whereas there don't seem to be a lot of options for C/C++. In C# I can spin up a very simple HttpListener-based host and connect it to something like Nancy (a Sinatra clone) and I would have liked to have the same thing in C/C++
|
# ? Mar 11, 2012 12:49 |
|
I have code that is something like this;code:
Edit: VV Good point; here's the offending functions and the original calling function: http://pastebin.com/ABJKivHj (Keep in mind that I'm amateur so it may not make much sense from design perspective to implement the functionality like this) Valtis fucked around with this message at 20:02 on Mar 11, 2012 |
# ? Mar 11, 2012 19:47 |
|
So is there any standard/boost/decent variadic template version of sprintf yet? Or should I just stick with my snprintf wrapper?
|
# ? Mar 11, 2012 19:52 |
|
Valtis posted:I have code that is something like this; We can't answer a question like this unless we see the actual code. "Something like this" is of little help. It could be any number of things. My guess is that the object holding the pointer is probably going out of scope, but without seeing your code we can't say for sure. Make a small test case that shows the behavior and post it.
|
# ? Mar 11, 2012 19:54 |
|
Valtis posted:If I comment out the std::cout line that prints the memory address, the bar->GetValue() returns random number (-2~ billion or so usually), if I leave the line uncommented, I get right values. I didn't really dig into your code but often when you run into this kind of problem it's because the memory has already been freed but not overwritten; when you call cout, the iostreams library allocates a chunk of memory for its internal operations and that overwrites the old freed memory. The net effect is that it makes an operation which appears to not have any side effects look like it's modifying your object; in reality, you're just accessing unallocated memory
|
# ? Mar 11, 2012 20:30 |
|
Sagacity posted:How so? To most people, "embedded server" means something more like "server running on an embedded system."
|
# ? Mar 11, 2012 20:50 |
|
GrumpyDoctor posted:To most people, "embedded server" means something more like "server running on an embedded system."
|
# ? Mar 11, 2012 23:04 |
|
Valtis posted:
auto by itself will never infer as a reference type, so the type of skill here is Skill, not const Skill &. That means that initialization copies the vector element instead of binding a reference to it, so &skill ends up being the address of a local variable. auto basically works via template argument deduction, though, so you can force it to bind by reference by writing auto &skill : skills instead.
|
# ? Mar 12, 2012 00:13 |
|
Contero posted:So is there any standard/boost/decent variadic template version of sprintf yet? Or should I just stick with my snprintf wrapper? It depends on what the wrapper does (and if you have format string annotations and warnings turned on).
|
# ? Mar 12, 2012 00:53 |
|
rjmccall posted:auto by itself will never infer as a reference type, so the type of skill here is Skill, not const Skill &. That means that initialization copies the vector element instead of binding a reference to it, so &skill ends up being the address of a local variable. Thank you. I probably should have realized that myself but sometimes I tend to be painfully blind to obvious issues.
|
# ? Mar 12, 2012 11:32 |
|
Hello, I'm taking a parallel and distributed class at my university where we have to make an algorithm work in parallel. In order to get started, we need to generate thousands of random x,y coordinates in a circle around 2 particular points. What's the best way to generate these numbers such that it's uniformly distributed inside that circle? We don't need to generate the numbers in parallel (thread safety and all that). Edit: We're using c++
|
# ? Mar 12, 2012 23:00 |
|
The easiest-to-implement way of doing something like that is to generate uniform x,y coordinates (which is easy), and then reject the ones that fall outside the circle.
|
# ? Mar 12, 2012 23:20 |
|
|
# ? Jun 1, 2024 17:51 |
|
Jabor posted:The easiest-to-implement way of doing something like that is to generate uniform x,y coordinates (which is easy), and then reject the ones that fall outside the circle. Or just generate one for angle and one for distance, each appropriately bounded to 2pi and r. No need to discard anything.
|
# ? Mar 12, 2012 23:22 |