|
So, something weird is happening to a program I wrote and can't seem to figure it out. The program is a server that accepts connections on a socket and creates a new thread (pthread) to handle it. The code looks something like this:C code:
I assume creating a thread is too complex of an operation and it takes time to initialize the values passed to the new thread, so when it's time to initialize the values they've already changed somehow (because of a new connection). Is my assumption correct? I know I shouldn't use the threads like that, and that the correct way is to use a thread pool. I was asked to do it this way. edit: should point out this is not for work, it's for a networking class I'm taking. Polidoro fucked around with this message at 04:41 on Sep 17, 2012 |
# ? Sep 17, 2012 04:35 |
|
|
# ? May 17, 2024 09:45 |
|
That'd likely be because you're passing in a pointer to a local to the thread start function. Don't do that --- by the time the thread is running, the variable may already be out of scope, with its stack space used for something else (like the instance of FD of the next iteration).
|
# ? Sep 17, 2012 04:53 |
|
poo poo, never thought about that. Thank you.
|
# ? Sep 17, 2012 05:05 |
|
I think I'm just about finished. code:
Thanks for the help everyone. E: I should mention I know I'm missing a few outputs here and there. I just need to go back and add those in.
|
# ? Sep 17, 2012 05:52 |
|
So I'm doing some graph stuff in c for my algorithms class, I wrote my program on my mac and it works perfect (finds the square of a graph and outputs the list), so when I go and try and run it on the CS dept server, it gives me some crazy output:code:
code:
Edit: Printing my original set of points comes out normal, printing everything as it goes into my linked lists looks the same, it just does this crazy output when I'm reading from the linked lists and printing it out. It is always the second part of the linked list too...once again, this works perfectly on my mac, I am completely at a loss. opt fucked around with this message at 07:03 on Sep 17, 2012 |
# ? Sep 17, 2012 06:24 |
|
opt posted:Edit: Printing my original set of points comes out normal, printing everything as it goes into my linked lists looks the same, it just does this crazy output when I'm reading from the linked lists and printing it out. It is always the second part of the linked list too...once again, this works perfectly on my mac, I am completely at a loss. It's impossible to know because you didn't actually post the code, but could it be that when you print out the data at the end, the memory for it has already been freed?
|
# ? Sep 17, 2012 09:08 |
|
Removed: I figured it out finally, apparently my mac doesn't care if I don't set initial values for the first nodes of my linked list, but the server did, good learning experience at least.
opt fucked around with this message at 15:15 on Sep 17, 2012 |
# ? Sep 17, 2012 14:21 |
opt posted:Removed: I figured it out finally, apparently my mac doesn't care if I don't set initial values for the first nodes of my linked list, but the server did, good learning experience at least. If you post the code, someone here might be able to explain why that is and give you advice to help avoid the same problem in the future.
|
|
# ? Sep 19, 2012 10:47 |
I've got a question regarding, I think, constructors and ADTs. I've got a mostly-functional program that I've almost completed, but I can't figure out how to solve one problem. The program works with triangles and computes stuff like the angle of each corner and the area, based on provided values for each side of the triangle. A specification is that there be at least two constructors. One constructor handles the case where there is no input for any of the sides, while the other handles the case where there is input for all three sides. I have, consequently, set up the following in order to handle this: From triangle.h code:
code:
code:
code:
code:
I have been under the impression that the appropriate constructor will be called based upon what data is passed into the function because of overloading. This is the implementation I have been led to believe will work. What am I doing wrong?
|
|
# ? Sep 19, 2012 18:10 |
|
This is most vexing parse problem. Basically, Triangle tri(); declares a function tri that returns a Triangle, not a variable. Use Triangle tri; instead. Triangle() creates and yields an object, just like Triangle(3, 3, 3) would do (so you can do e.g. tri = Triangle(); later on). Also, use constructor initialisation list. And this case really doesn't need two constructors, but that's probably coursework so I'm not surprised that it's dumb.
|
# ? Sep 19, 2012 18:24 |
quote:This is most vexing parse problem. Basically, Triangle tri(); declares a function tri that returns a Triangle, not a variable. Use Triangle tri; instead. I'm please to report that this works. Thank you. It makes sense that you've now explained it.
|
|
# ? Sep 19, 2012 18:42 |
|
I have a best practices question. Say I've got some long-running function, and I want users of the function to be able to get progress updates, so I have it take some kind of progress callback: C code:
-Provide a release_happening_payload function which does whatever releasing needs to be done, or -Say that when the callback is done, the payload will be released by long_operation, so callers will have to copy whatever they need out. I'm leaning toward option two because it makes the interface cleaner, but if I've not thought of something I'd love to hear it.
|
# ? Sep 20, 2012 02:31 |
|
From my understanding, you're worried about callbacks that store some data from happening_payload, and then getting a use-after-free error if the long operation completes? If long_operation allocates struct happening_payload and everything inside it, then it owns the memory. Put some mention that happening_payload and all memory inside is invalid after the callback is called, and that you need to copy (or ref, if you have that) all data inside the struct.
|
# ? Sep 20, 2012 03:55 |
|
So option 2. Thanks!
|
# ? Sep 20, 2012 04:20 |
|
Does C mandate structure alignment? That is, will a structure definition be guaranteed to compile to the same ABI across all compilers with any number of flags, given the same target architecture?
|
# ? Sep 20, 2012 08:37 |
|
Suspicious Dish posted:Does C mandate structure alignment? That is, will a structure definition be guaranteed to compile to the same ABI across all compilers with any number of flags, given the same target architecture? No. (For example, will int be 32 bits or 64 bits (or 16)? Will long be 32 bits or 64 bits? Will the alignment of double be 4 or 8? Will the alignment of long double be 4 or 8 or 16? Different OSes and different compilers believe differently. You can't even guarantee size so good luck guaranteeing structure alignment. And practically speaking, in the wild, you'll see different structure alignment rules. Particularly for doubles being 4 or 8 byte aligned on x86. shrughes fucked around with this message at 09:27 on Sep 20, 2012 |
# ? Sep 20, 2012 09:25 |
Suspicious Dish posted:Does C mandate structure alignment? That is, will a structure definition be guaranteed to compile to the same ABI across all compilers with any number of flags, given the same target architecture? Assuming your compiler supports it (most do), #pragma pack(1) will guarantee that your structure is 1-byte aligned. However like the above poster mentioned, the size of the datatypes may differ between compilers and architectures as the C standard doesn't specify a maximum size for primitives, only a minimum.
|
|
# ? Sep 20, 2012 11:19 |
|
I was assuming the same architecture and OS, but with msvc vs. gcc vs. icc vs. clang vs. suncc or whatever. Does that mean that if I'm using a closed-source library that ships public structs in their header files as part of their API, I just have to pray to the gods that their compiler's structure alignment matches with mine?
|
# ? Sep 20, 2012 16:49 |
|
There is a more-nebulous-than-you-might-think concept called the "target platform" which compilers generally respect. Compilers for the same target platform are generally supposed to produce compatible code, at least on the C level. So as long as it's actually compiled for the platform you're going to use it on, you should be fine. To describe the target platform, GCC hath bequeathed upon the world the idea of a "target triple", a combination of the architecture, vendor, operating system, and optionally the environment. So, for example, i386-pc-linux-gnu is a different platform from i386-pc-mingw32 and i386-apple-darwin, and all of those do, in fact, have slightly different ABI rules.
|
# ? Sep 20, 2012 18:35 |
|
Found out I'm absolutely terrible at C++ a few weeks into this Intro to C++ class at the local community college. Hope you cool cats don't mind me bugging you with plebeian level poo poo.
|
# ? Sep 20, 2012 22:54 |
|
rjmccall posted:There is a more-nebulous-than-you-might-think concept called the "target platform" which compilers generally respect. Compilers for the same target platform are generally supposed to produce compatible code, at least on the C level. So as long as it's actually compiled for the platform you're going to use it on, you should be fine. Is there a standard set of rules for each "target platform" listed anywhere, in the gcc documentation or otherwise? Or is it "let's study the output that gcc gives, and then try to match it". Just curious here, seems like it would be interesting to read. But thanks for explaining; it certainly squashes a few ideas in my head.
|
# ? Sep 20, 2012 23:13 |
|
Suspicious Dish posted:Is there a standard set of rules for each "target platform" listed anywhere, in the gcc documentation or otherwise? Or is it "let's study the output that gcc gives, and then try to match it". Just curious here, seems like it would be interesting to read. In theory there's an obvious platform vendor who's responsible for documenting this; for example, here is the Apple documentation (which despite the name also includes fundamental type information). Usually the ABI comes from the architecture vendor; OS vendors then tweak that (or totally ignore it and do their own thing). Architecture vendors tend to only barely understand C, so a lot of things go answered — my perennial favorite is "what exactly is the guaranteed range of a stored, passed, or returned value of _Bool?". In the Unix world, there's also usually a delightful uncertainty about which things, exactly, still apply from the System V ABI. Oh, and there's a habit that I've never understood of putting massive header dumps in the ABI document — and not for basic C things, but for libraries like X11.
|
# ? Sep 21, 2012 01:30 |
|
There's also charming things like the i386 psABI having never been updated since its creation, so things like SSE and AVX are basically specified as "whatever gcc does"
|
# ? Sep 21, 2012 08:14 |
|
Anyone know of a good library for representing sets with ranges where you can do intersections, unions and differences? For example I'd like to be able to do operations like this (pseudocode): code:
|
# ? Sep 21, 2012 20:47 |
|
e: misread the question
|
# ? Sep 21, 2012 22:53 |
|
Contero posted:Anyone know of a good library for representing sets with ranges where you can do intersections, unions and differences? I haven't personally used it, but the Boost Interval Container Library should be able to do everything that you need.
|
# ? Sep 22, 2012 01:01 |
|
So I'm writing a little customer class for homework, and I'm wondering if this is okay to use as a constructor:code:
|
# ? Sep 24, 2012 21:16 |
|
The constructor should just becode:
If you haven't covered initializer lists yet: code:
I can't help but notice that your customers don't actually have any information stored about them in their constructor other than their mailing list status. Were you just trying to prune down your example, or are there some larger questions about class design here?
|
# ? Sep 24, 2012 21:27 |
|
I've got a Visual Studio question. I've been given some code by our instructor with some sub-directories and I can't get the include-statements to work properly. He uses Linux where it works perfectly. stuff like this: math/f3.c code:
And I've got a couple of these all with the samme errors. How should I write it for it to work? I'm not really familiar with VS, I normally write Java.
|
# ? Sep 24, 2012 21:59 |
|
an skeleton posted:and also, I was wondering if that was formatted OK, and if not what is wrong? done == false should be !done. As GrumpyDoctor said, don't put I/O logic in constructors. Closing braces are usually put on the new line, it's more readable this way: C++ code:
C++ code:
Boz0r posted:How should I write it for it to work? Open project settings and adjust include directories (under C/C++ Preprocessor or something like that) to include the one with math subdirectory. E.g. if that header lives in C:\foo\math\f3.h, add C:\foo as the include directory.
|
# ? Sep 24, 2012 22:28 |
Apart from what everyone else has said...an skeleton posted:
(In C, () in a function declaration means "undeclared parameter list" while in C++ it means "empty parameter list".)
|
|
# ? Sep 24, 2012 22:47 |
|
Boz0r posted:I've got a Visual Studio question. I've been given some code by our instructor with some sub-directories and I can't get the include-statements to work properly. He uses Linux where it works perfectly. First, you should use quotations for non-system header paths: code:
|
# ? Sep 24, 2012 22:58 |
|
GrumpyDoctor posted:The constructor should just be customer::customer(bool onMailingList) should be explicit customer::customer(bool onMailingList) to prevent implicit conversion.
|
# ? Sep 24, 2012 23:00 |
|
b0lt posted:customer::customer(bool onMailingList) should be explicit customer::customer(bool onMailingList) to prevent implicit conversion. Yup, totally forgot about that. Welcome to C++!
|
# ? Sep 24, 2012 23:30 |
|
GrumpyDoctor posted:The constructor should just be They are 'extending' a class called "Person." Thanks to all, and to the person who said "void" was not supposed to be in the parameters, that was put there by default by Visual Studio for whatever reason. One more question, why is Visual Studio not letting me initialize the Person or customer class in the main? It's been a while since I messed around with classes, but I don't remember having to do anything particularly special to get the main to recognize that those classes exist, other than having the '.cpp' and '.h' files in the same folder as the project?
|
# ? Sep 24, 2012 23:34 |
|
an skeleton posted:One more question, why is Visual Studio not letting me initialize the Person or customer class in the main? Post the code and errors. Did you include the header?
|
# ? Sep 24, 2012 23:37 |
|
PiotrLegnica posted:Post the code and errors. Did you include the header? code:
|
# ? Sep 24, 2012 23:45 |
an skeleton posted:
Nothing in that declares the Person class. You need to include your header that declares the class.
|
|
# ? Sep 24, 2012 23:49 |
|
an skeleton posted:here it is, it's probably some silly mistake but there are red lines under the Person/customer indicating that they aren't recognized by the compiler. You didn't include the header. Add #include "Person.hpp" and #include "customer.hpp" or however you called them.
|
# ? Sep 24, 2012 23:49 |
|
|
# ? May 17, 2024 09:45 |
|
I addedcode:
One more question: I want each customer to have it's own number assigned, custNum, that it gets when it is created. I tried making a totalCust global variable in the main that is 0, but I guess the customer class doesn't have access to that. an skeleton fucked around with this message at 00:03 on Sep 25, 2012 |
# ? Sep 24, 2012 23:55 |