|
Flashdance posted:This is because when I implement it that way, it deletes both the temp and the docarray entry whenever I use a delete statement and then other parts of the program crash. That's because they're the same thing. Isn't that the point of writing it that way?
|
# ? May 2, 2010 22:06 |
|
|
# ? Jun 8, 2024 19:22 |
|
Avenging Dentist posted:That's because they're the same thing. Isn't that the point of writing it that way? Well yes, which is why they're currently commented out. I was following a guide I found and that was how their ragged array was implemented; obviously their example is not going to work for what I want to do.
|
# ? May 2, 2010 22:09 |
|
That would work fine, but then you can't go and delete that data without expecting it to be deleted everywhere it's referenced. You'd want to copy the char * argument though since you have no guarantee that it was created with new. You've also got lots of off-by-one errors; you don't allocate space for the null terminator for your strings and your loop starting at current+1 should start at current. It's also a waste of time to make a temp array, copy the docarray stuff into it (and insert one element) and then copy the entire thing back to the docarray. Also why are you not just using a std::vector<std::string>?
|
# ? May 2, 2010 22:14 |
|
Avenging Dentist posted:Also why are you not just using a std::vector<std::string>? Oh yeah, I probably should have mentioned this first. I'm not allowed to use vectors or the string class for this particular part. Avenging Dentist posted:You'd want to copy the char * argument though since you have no guarantee that it was created with new. Why would this matter? Genuine question. Avenging Dentist posted:You've also got lots of off-by-one errors; you don't allocate space for the null terminator for your strings and your loop starting at current+1 should start at current. I thought strlen returned the length with the null terminator, but I checked after reading that and it apparently doesn't so I fixed that part. As for the loop, it starts at current + 1 because the 2 lines between the loops is copying c in what would be the current position. Avenging Dentist posted:It's also a waste of time to make a temp array, copy the docarray stuff into it (and insert one element) and then copy the entire thing back to the docarray. This is what I specifically concerned about. Right now I'm adding/deleting entries from the array by just making a new temp array one bigger/smaller, copying everything over, then recreating the normal array in the new size and copying everything back (skipping the deleted entry). I've been looking and looking for a way to add/remove items without making a new temp array but haven't figured one out yet. It's obviously expensive, cumbersome, and easy to make mistakes with.
|
# ? May 2, 2010 22:45 |
|
Flashdance posted:Why would this matter? Genuine question. char *foo = "bar"; delete foo; // Probably segfault Flashdance posted:As for the loop, it starts at current + 1 because the 2 lines between the loops is copying c in what would be the current position. Read through it again. Your indices are wrong. Flashdance posted:I've been looking and looking for a way to add/remove items without making a new temp array but haven't figured one out yet. It's obviously expensive, cumbersome, and easy to make mistakes with. You have to make a "temp" array. You don't have to make a new destination array. Just have it point to the temp array.
|
# ? May 2, 2010 22:58 |
|
Avenging Dentist posted:
I guess I was confused on why I should copy the char * argument if I'm not trying to delete it. You are referring to the char *c argument, right? I just want to make sure I'm not misunderstanding you. Avenging Dentist posted:Read through it again. Your indices are wrong. I took another look and sure enough. The program worked properly for display but appeared to be exactly the reason the array wasn't deleting. The programs works properly now with delete. Avenging Dentist posted:You have to make a "temp" array. You don't have to make a new destination array. Just have it point to the temp array. If I do that, aren't I back to the same problem as before with the multiple deletes from one pointer? If I allocate a temp array in the function, I have to delete it before leaving. Thanks a ton for your help, I think the program works fine at this point.
|
# ? May 2, 2010 23:27 |
|
Flashdance posted:I guess I was confused on why I should copy the char * argument if I'm not trying to delete it. You will delete it one day I hope! (When your destructor is called.) Flashdance posted:If I do that, aren't I back to the same problem as before with the multiple deletes from one pointer? If I allocate a temp array in the function, I have to delete it before leaving. No you don't. You really only need to do two things to make sure your memory management is correct: 1) have exactly the same number of news as deletes, 2) only delete things that have been newed. In this case, what you're doing is: code:
code:
|
# ? May 2, 2010 23:34 |
|
Avenging Dentist posted:No you don't. You really only need to do two things to make sure your memory management is correct: 1) have exactly the same number of news as deletes, 2) only delete things that have been newed. In this case, what you're doing is: Ok, so when I delete docarray without first deleting all docarray[n], it only removes the pointer to the array, which is reassigned by docarray = temp. If I'm reading this right, that means that I don't need to deallocate temp because docarray IS temp at this point. So what happens to the old docarray[n] entries? They were allocated with new as well. Why wouldn't this be a memory leak, since I'm using new without a corresponding delete?
|
# ? May 3, 2010 00:46 |
|
Flashdance posted:Ok, so when I delete docarray without first deleting all docarray[n], it only removes the pointer to the array, which is reassigned by docarray = temp. If I'm reading this right, that means that I don't need to deallocate temp because docarray IS temp at this point. docarray and temp are not pointers to arrays of strings, they are pointers to arrays of pointers. The only memory allocation you need to do in this function is allocate space for the new string you are inserting, which is presumably deleted in your destructor or something, and you need to allocate memory for a new array of pointers. The old array of pointers gets deleted, and then you just set docarray = temp, so at the end of the function, delete [] docarray; docarray = temp; All you should be doing is copying the pointers in the arrays that point to the strings.
|
# ? May 3, 2010 01:00 |
|
Perfect, I get it now. Thanks a bunch.
|
# ? May 3, 2010 01:07 |
|
Flashdance posted:Why wouldn't this be a memory leak, since I'm using new without a corresponding delete? The corresponding delete would be for when you are totally done with all of this and you're destroying your object.
|
# ? May 3, 2010 01:48 |
|
An array of vectors will be safely initialized, right? std::vector<int> bob[5]; also if I have a POD struct with an array in it, will the assignment operator copy the array correctly? tractor fanatic fucked around with this message at 02:11 on May 3, 2010 |
# ? May 3, 2010 02:05 |
|
Yes and yes.
|
# ? May 3, 2010 04:12 |
|
Zakalwe posted:Unless the lines are of fixed width, there is no way to skip ahead to the nth line without first scanning through n-1 lines. Otto Skorzeny posted:Not C++, but google for discussion of Mark Dominus' Tie::File for some info on the difficulties that surround efficiently doing line- or record-oriented operations on files in operating systems that follow the stream-of-bytes model for files. (If you were on an old IBM mainframe with record-based files, this would be easy to do semi-efficiently from a programming perspective, although record-based files have plenty of problems of their own). Thanks, I'll just go through the lines then I guess. That Perl module is interesting but I sadly don't have the time to go through it. It's really not that big of a deal, I just didn't want to waste the time iterating through the lines if I didn't have to.
|
# ? May 3, 2010 13:17 |
|
tractor fanatic posted:also if I have a POD struct with an array in it, will the assignment operator copy the array correctly? Assuming it's an array and not a pointer to an array, yes.
|
# ? May 3, 2010 16:05 |
|
Already posted this in the general programming quesitons thread, but since it is C++ specific I figured I would post it here as well. I want to make sure that I am using the new and delete functions in C++ correctly, because a program I have written seems to take up a lot of memory when it should not. A call to the network flow portion of my code creates 7 large arrays of long ints, which I delete at the end of the code. Thus, if the program were using 60,000K of memory before the network flow code, it would also be using 60,000K (or something close to it) after the call since the 7 large arrays that had been created have now been deleted. Instead the program uses more and more memory (about 120,000K more) after each call to the network flow code. In my code I have: code:
code:
|
# ? May 3, 2010 18:11 |
|
Your sample code looks ok (though there is really no good reason for you to manage this memory yourself). Run a leak detector on your code or something (e.g. valgrind).
|
# ? May 3, 2010 18:12 |
|
Avenging Dentist posted:Your sample code looks ok (though there is really no good reason for you to manage this memory yourself). Run a leak detector on your code or something (e.g. valgrind). I don't know how else I would manage the memory otherwise - this is what I found in the books I have been reading. Care to elaborate? I will look into the leak detector.
|
# ? May 3, 2010 18:19 |
|
Well for one thing, making a 2D array by using a pointer-to-array of pointers-to-arrays is needlessly complicated, uses more memory than you need, and fragments your memory. It would make more sense to allocate space for M*N elements in one array and manually handle the array stride (this is what your compiler does for statically-sized N-D arrays): array[x*N + y]. It would be even better if you put this into its own class (or used something like boost::multi_array) since that would abstract out the memory management, which is not essential to your algorithm. The benefit of putting this stuff in a class is that the memory is automatically freed when the destructor is called (i.e. the object goes out of scope). See RAII. You could also just use a std::vector instead of an array.
|
# ? May 3, 2010 18:25 |
|
Avenging Dentist posted:Well for one thing, making a 2D array by using a pointer-to-array of pointers-to-arrays is needlessly complicated, uses more memory than you need, and fragments your memory. It would make more sense to allocate space for M*N elements in one array and manually handle the array stride (this is what your compiler does for statically-sized N-D arrays): array[x*N + y]. The std::vector looks like something I can get my head around. I am going to read up on it and hopefully make my code better. Thank you.
|
# ? May 3, 2010 18:45 |
|
I have written a small program and built it into an .exe. It works just fine on the computer I wrote the code on which has Microsoft VS installed. But when I try to run the .exe on another computer that does not have Microsoft VS installed it will not run. Will my .exe only run on computers that have Microsoft VS installed? Or am I missing something when building my .exe? For reference the operating systems are Windows XP (the computer it will not run on) and Windows Vista (the computer I wrote the program on).
|
# ? May 3, 2010 23:19 |
|
Check the OP of this thread.
|
# ? May 3, 2010 23:31 |
|
Quick QT question (I didn't see a QT thread here, so this is the next best place, I guess.) I'm trying to make a simple app, that has a main window with some controls on the upper half (a few buttons, and a textbox) and a QListView on the bottom half. See: code:
If I use a QMainWindow, I'm vexed with what to use as my central widget. Ideally I want at least the QListView widget to dynamically grow in size when the window is resized. I'd also like to have the top widget intelligently move when the window is resize, too. If I use QtDesigner's trick of creating a blank QWidget and setting that as the central widget, then parenting all the other widgets to that QWidget, I can't figure out how to resize all my widgets with the window. I tried using a QSplitter, with the QListView on the bottom and a QWidget at the top, and that worked with the resizing part OK, but I don't want the user to be able to move the split around like that, and plus the top half still has the same problem as using a blank QWidget as a central window. I'm brand new to Qt, so I must be missing something obvious, but help me out here.
|
# ? May 4, 2010 01:45 |
|
Magicmat posted:Quick QT question (I didn't see a QT thread here, so this is the next best place, I guess.) Set a QVBoxLayout as the layout for that widget. Add a child QHBoxLayout. Add buttons and time as child of the horizontal layout Add the QListView. The size would be split in the sensible way automatically.
|
# ? May 4, 2010 01:59 |
|
You could also put your row of buttons into a toolbar I think.
|
# ? May 4, 2010 09:36 |
|
What's the proper way to share member implementations between template specializations?code:
Any ideas?
|
# ? May 5, 2010 04:52 |
|
Curiously recurring template pattern?
|
# ? May 5, 2010 05:05 |
|
http://www.parashift.com/c++-faq-lite/templates.html#faq-35.9 ?
|
# ? May 5, 2010 05:08 |
|
UraniumAnchor posted:http://www.parashift.com/c++-faq-lite/templates.html#faq-35.9 ? No that's for template functions. I don't think you can apply that to member functions like that. Also yeah CRTP: http://codepad.org/9dxPTqXL
|
# ? May 5, 2010 05:10 |
|
You're right that there's no way to share a direct member implementation between different "patterns". If you just wanted to vary an member implementation you could specialize that member alone, but that won't get you what you want.code:
|
# ? May 5, 2010 05:11 |
|
Yeah I was actually considering pointing out that you could do it that way too. It's really just an inversion of CRTP (though CRTP is more general).
|
# ? May 5, 2010 05:12 |
|
So I am having trouble with an assignment. I am brand new at C programming and my professor decided the end of the semester would be a good time for us to learn the ins and outs of fork, wait, and exit commands. Basically there are two parts of this. The first part is to create 3 2D arrays (A, B, and C) where A is a matrix of 1s, B is a matrix of 2s, and C is a matrix of 0s. We are then supposed to multiply each element of A with its corresponding element in B and store it in C. That is supposed to happen under a unique child process and that child process is supposed to terminate once the multiplication has completed. That was to illustrate a point about how fork handles memory. I get that. To get a better idea of what I mean here is the code:code:
quote:Do the same matrix multiply but pass the values back with the status. You pass limited results back to the parent (look carefully at the documentation for exit and wait system calls, the status structure and the shift (>>) operator). I did a man on exit and wait, but I couldn't find anything about a status structure. The shift operator I already knew. I found stuff about the status and how it is a return value on the fork method, so that is easy to pass back since it's done for me. The problem is with passing back the product. Another professor suggested that I use pipes (the prof that assigned this is out of town, didn't tell us, and isn't answering emails, wahoo!), but that's not working/I didn't do it right. Here is what I have for part 2 so far. code:
Tux Racer fucked around with this message at 06:01 on May 5, 2010 |
# ? May 5, 2010 05:46 |
|
Avenging Dentist posted:Yeah I was actually considering pointing out that you could do it that way too. It's really just an inversion of CRTP (though CRTP is more general). True enough. Incidentally, you can explicitly specialize members of class templates. It doesn't help Contero, though, because you can't actually change the structure of the class in any way. code:
The only other restriction is that the class whose member you're specializing can't itself still be templated, i.e. you have to have specific specializations all the way down: code:
|
# ? May 5, 2010 05:46 |
|
Tux Racer posted:I did a man on exit and wait, but I couldn't find anything about a status structure. The shift operator I already knew. I found stuff about the status and how it is a return value on the fork method, so that is easy to pass back since it's done for me. Your professor wants you to do one multiplication in each child process. The child process will use its termination status to tell the parent process what the result of the multiplication is. The "status structure" is just that the status int written by wait() is "structured" in an interesting way: it conveys multiple pieces of information which are represented using different bits in the int. Look at the section of the wait() manpage about the macros WIFSIGNALED, WEXITSTATUS, etc. pipe() is a perfectly sensible way of communicating between processes but, as it's not what your professor is asking for, I'm not sure why the other professor brought it up.
|
# ? May 5, 2010 05:59 |
|
rjmccall posted:Your professor wants you to do one multiplication in each child process. The child process will use its termination status to tell the parent process what the result of the multiplication is. The "status structure" is just that the status int written by wait() is "structured" in an interesting way: it conveys multiple pieces of information which are represented using different bits in the int. Look at the section of the wait() manpage about the macros WIFSIGNALED, WEXITSTATUS, etc. So would something like C[i][j] = WEXITSTATUS(status); with whatever bitwise operation i need to do be correct? Or is there something else I am missing?
|
# ? May 5, 2010 06:09 |
|
Tux Racer posted:So would something like C[i][j] = WEXITSTATUS(status); with whatever bitwise operation i need to do be correct? Or is there something else I am missing? If you want to check whether exit() and wait() work like you think they might, make a little program and try it.
|
# ? May 5, 2010 06:23 |
|
rjmccall posted:If you want to check whether exit() and wait() work like you think they might, make a little program and try it. Alright thanks for your help.
|
# ? May 5, 2010 06:26 |
|
I want to do some stuff with dynamic class loading but I'm wondering if I'm getting into a platform-specific nightmare. Generally I want to write an arbitrary amount of modules that do some operations on arrays of data. It would be nice to have this code to have access to the arrays directly, but then have a seperate mode where if I don't trust the code (mostly that it might crash), I can load it up in a seperate process, copy the data over, and see if it can handle it there. So I guess I'm also getting into an interprocess communications spat too. I see some stuff about dynamic loading in Linux from this tutorial: http://www.linuxjournal.com/article/3687 Will I then have to code something different for Windows and other platforms? Note that I would expect to have to rebuild the module for each platform, just that once its built I'd like to be able to load it in after the main program has been compiled, installed, and started.
|
# ? May 5, 2010 17:00 |
|
Avenging Dentist posted:Curiously recurring template pattern? This looks to be the best solution, especially since I don't have just one specialization, but about 5 or 6. It's for a matrix library I'm working on. Square matrices have their own functions (inverse, determinant) while column matrices have vector operations, and a 3x1 matrix has the usual 3d vector operations and can have its data accessed through .x .y .z etc. rjmccall posted:
It's templates all the way down!
|
# ? May 5, 2010 19:18 |
|
|
# ? Jun 8, 2024 19:22 |
|
rjmccall posted:Incidentally, you can explicitly specialize members of class templates. It doesn't help Contero, though, because you can't actually change the structure of the class in any way. Yeah I know, but I got sick of typing.
|
# ? May 5, 2010 19:20 |