|
rawstorm posted:I need help clearing dynamic memory. here is one of your more immediately pressing problems
|
# ? Dec 11, 2008 05:10 |
|
|
# ? Apr 25, 2024 04:43 |
|
dazjw posted:here is one of your more immediately pressing problems Shouldn't he get rid of the thing2 = NULL and replace delete with delete[]?
|
# ? Dec 11, 2008 06:46 |
|
Ari posted:Shouldn't he get rid of the thing2 = NULL and replace delete with delete[]? No.
|
# ? Dec 11, 2008 06:48 |
|
Look the code I posted was just a framework of the actual code. The real code looks much better and compiles and does what it's supposed to do. My question is that I have an array of vector<int>'s that I dynamically allocated memory to and I want to free up that memory. All I need is a snippet of code that does that. I also thought that there is no way to update the value of a vector in the middle of the vector without making a new vector.
|
# ? Dec 11, 2008 08:16 |
|
rawstorm posted:Look the code I posted was just a framework of the actual code. The real code looks much better and compiles and does what it's supposed to do. My question is that I have an array of vector<int>'s that I dynamically allocated memory to and I want to free up that memory. All I need is a snippet of code that does that. You would probably get better help if you posted code that wasn't directly contradicting what you were describing, just FYI.
|
# ? Dec 11, 2008 08:25 |
|
Okay, forget about the previous code I posted, let's say I dynamically allocate memory to an array of vector <int>'s like so:code:
|
# ? Dec 11, 2008 08:31 |
|
rawstorm posted:Okay, forget about the previous code I posted, let's say I dynamically allocate memory to an array of vector <int>'s like so: That won't work, either. You can't take a pointer to vector<int> and assign it a pointer to space for fooNum ints. Specifically, if you try to, you'd get this: code:
|
# ? Dec 11, 2008 08:34 |
|
rawstorm posted:Now I want to free up the memory I just allocated. What do I need to type on the next line that would free up that memory? Nothing because apparently you can't post code that makes sense or compiles.
|
# ? Dec 11, 2008 08:53 |
|
rawstorm posted:Okay, forget about the previous code I posted, let's say I dynamically allocate memory to an array of vector <int>'s like so: You know, when you allocate with new, you should probably make sure that the type you're allocating matches up with the pointer type. Just a thought. vv
|
# ? Dec 11, 2008 08:54 |
|
Whoops, I forgot to include 1 line, and wrote "int[fooNumber]" instead of "vector<int>[fooNumber]" code:
rawstorm fucked around with this message at 09:05 on Dec 11, 2008 |
# ? Dec 11, 2008 09:03 |
|
What can we say that that site doesn't say?
|
# ? Dec 11, 2008 09:09 |
|
Avenging Dentist posted:What can we say that that site doesn't say? I followed the sites advice and wrote code:
|
# ? Dec 11, 2008 09:10 |
|
rawstorm posted:I followed the sites advice and wrote Consider the order of these instructions for a moment.
|
# ? Dec 11, 2008 09:13 |
|
I'm no guru, but what's so difficult?code:
vvv I fixed that fast
|
# ? Dec 11, 2008 09:14 |
|
Morton the Mole posted:I'm no guru, but what's so difficult? Apparently knowing how not to break tables is pretty difficult.
|
# ? Dec 11, 2008 09:14 |
|
Avenging Dentist posted:Consider the order of these instructions for a moment. Hey, man, if he wants to free the memory he's allocated at NULL, he can drat well do it!
|
# ? Dec 11, 2008 09:15 |
|
Okay thanks guys.
|
# ? Dec 11, 2008 09:20 |
|
rawstorm posted:Okay thanks guys. I have a feeling he's still just doing code:
|
# ? Dec 11, 2008 09:39 |
|
On the subject of memory allocation, is there a better way to allocate space for an object but not call its constructor than doing this? (malloc is an obvious choice, but 1) is more verbose and 2) doesn't allow the use of delete later.)code:
Avenging Dentist fucked around with this message at 10:01 on Dec 11, 2008 |
# ? Dec 11, 2008 09:42 |
|
Avenging Dentist posted:On the subject of memory allocation, is there a better way to allocate space for an object but not call its constructor than doing this? (malloc is an obvious choice, but 1) is more verbose and 2) doesn't allow the use of delete later.) Arguably you should use T::operator new if it exists; I don't know how to do that (or if you can) in template metaprogramming.
|
# ? Dec 11, 2008 10:19 |
|
Well, here's a good reason why not to do this: segfaults http://codepad.org/wistRvN1 I'm not actually sure why this segfaults. EDIT: The more I think about it, the more it makes sense, given that you wouldn't necessarily know how many destructors to call. Still, this makes C++ slightly more stupid than before, since you can't dynamically allocate an array of objects unless they're default constructible. Avenging Dentist fucked around with this message at 10:36 on Dec 11, 2008 |
# ? Dec 11, 2008 10:29 |
|
Avenging Dentist posted:EDIT: The more I think about it, the more it makes sense, given that you wouldn't necessarily know how many destructors to call. Still, this makes C++ slightly more stupid than before, since you can't dynamically allocate an array of objects unless they're default constructible. Yeah, since string has a destructor, new string[n] has to remember n somehow. It looks like gcc basically allocates a struct { size_t; string[n]; }, and that's what delete[] expects to find.
|
# ? Dec 11, 2008 11:02 |
|
I managed to trick MSVC in all cases and GCC in the case where you have a destructor. code:
|
# ? Dec 11, 2008 11:14 |
|
Avenging Dentist posted:(malloc is an obvious choice, but 1) is more verbose and 2) doesn't allow the use of delete later.) Just out of curiosity, what exactly is the effect of using delete on malloc'd memory?
|
# ? Dec 11, 2008 11:16 |
|
riggsninja posted:Just out of curiosity, what exactly is the effect of using delete on malloc'd memory? Technically undefined, but it might work because new is usually defined in terms of malloc. However, in the case of delete[], it will probably blow up like it does in my evil example.
|
# ? Dec 11, 2008 11:18 |
|
Thanks to everyone who replied re: my function pointer issue. I wasn't aware of the syntax change but it answers my first internal question which was "how in the world did this ever compile?" But that's a few suggestions to go with, so thanks.
|
# ? Dec 11, 2008 15:15 |
|
Avenging Dentist posted:EDIT: The more I think about it, the more it makes sense, given that you wouldn't necessarily know how many destructors to call. Still, this makes C++ slightly more stupid than before, since you can't dynamically allocate an array of objects unless they're default constructible. I'm pretty sure you're supposed to be calling operator delete to be paired with your explicit operator new call, though it's been a while since I've done this. Cycle through explicitly calling destructors before deleting. Other options are allocating and deallocating the result of boost::aligned_storage or simply using std::allocator.
|
# ? Dec 11, 2008 15:42 |
|
Avenging Dentist posted:Adding an integral value N to a pointer foo *p increments the pointer's address by N*sizeof(foo). Thanks for answering. See, that's what I was thinking, but when I tried implementing it without all the casts, it wasn't working correctly. It's really the const declares in them that have me confused as why this way works and why this way was chosen. This code isn't coming from an amateur programmer.
|
# ? Dec 11, 2008 21:24 |
|
Can someone please tell me whats wrong with my code? This is my final project for my CS beginner class, and i cannot get the drat thing to work properly. I have sat here for over 4 hours trying to figure out whats wrong, but i cannot come up with anything. Hopefully, another set of eyes can help. So please, could you look at my code?code:
Edit 2: I am trying to write a program with http://www.cs.uky.edu/~keen/115/programs/5pgm.html as instructions Pookdaddy G fucked around with this message at 23:50 on Dec 11, 2008 |
# ? Dec 11, 2008 21:57 |
|
You wrote too many comments and overloaded the compiler
|
# ? Dec 11, 2008 22:13 |
|
Pookdaddy G posted:Can someone please tell me whats wrong with my code? This is my final project for my CS beginner class, and i cannot get the drat thing to work properly. I have sat here for over 4 hours trying to figure out whats wrong, but i cannot come up with anything. Hopefully, another set of eyes can help. So please, could you look at my code? Also, unless you were specifically following a convention as set by your professor, you might want to put a space or two in front of your comments dosomething(); //a nicely spaced comment as opposed to dosomething();//blah because as it stands now, your comments tend to blend in with the rest of the line and make it pretty much impossible for anyone who isn't you to care.
|
# ? Dec 11, 2008 22:55 |
|
It's the name lookup that's giving you incorrect results, right? So either the search function has a bug, or it's being called incorrectly, or the table is already incorrect before you get there. Here's a hint: 2 of the above are true. I would recommend that you set a breakpoint where you're doing the search. Inspect the values in the table to see if they're what you would expect. Then see what the search function returns, and determine whether that's what you would expect it to return. If it returns something unexpected, then step through it to see what strings are being compared, and see if it's doing what it should be doing. Edit: Here's another hint: When you're looking up www.cs.uky.edu, it returns an index of 5. So there are two problems here: First, why would it return 5, which is out of bounds (the array size is 5, so valid indices are from 0 to 4), and second, why didn't it find that string at index 0? Olly the Otter fucked around with this message at 23:17 on Dec 11, 2008 |
# ? Dec 11, 2008 22:56 |
|
Avenging Dentist posted:Well, here's a good reason why not to do this: segfaults code:
EDIT: Obviously the parameter passing could be handled better, likely with some boost trickery, but you get the idea. digibawb fucked around with this message at 23:29 on Dec 11, 2008 |
# ? Dec 11, 2008 23:24 |
|
Olly the Otter posted:It's the name lookup that's giving you incorrect results, right? So either the search function has a bug, or it's being called incorrectly, or the table is already incorrect before you get there. Here's a hint: 2 of the above are true. So your saying my function is being called incorrectly and the table is already incorrect before you get there? For the latter are you suggesting using a for statement? Also, for my table being called incorrectly, does that have to do my my if and else statements, or more so in the actual searchtable(blah blah blah)?Sorry, i'm not that good at coding. Pookdaddy G fucked around with this message at 00:00 on Dec 12, 2008 |
# ? Dec 11, 2008 23:40 |
|
digibawb posted:
This is actually remarkably similar to some code I wrote a while back with the intent of replacing new/delete with library functions. The semantics of new/delete are among my least favorite parts of C++, and except for placement new (which has awful syntax in my opinion), you can actually write a library that does everything new and delete normally do. Of course, as you've noticed, you either need to store the length of arrays in your buffer for destructor calls, or you can use a god-awful hack like I wrote to trick the compiler, since it has to store the length of any dynamic array with a nontrivial destructor.
|
# ? Dec 12, 2008 00:43 |
|
Pookdaddy G posted:So your saying my function is being called incorrectly and the table is already incorrect before you get there? For the latter are you suggesting using a for statement? Also, for my table being called incorrectly, does that have to do my my if and else statements, or more so in the actual searchtable(blah blah blah)?Sorry, i'm not that good at coding. No. There's a bug in the function, and it's being called incorrectly. I was hoping you would inspect values with the debugger to ascertain which is the case -- you'd find that the table actually has the correct values at the time that the search occurs. One bug is in the for loop in searchtable. You should have "count < largest", not "count <= largest". (The "largest" variable is badly named because it's actually the size, and the largest appropriate index is one less than that.) The other bug has to do with how you're calling the function. But if you don't see it yet, then go ahead and fix the above error and see how that changes the output. Maybe it will clarify things for you.
|
# ? Dec 12, 2008 00:44 |
|
Avenging Dentist posted:The semantics of new/delete are among my least favorite parts of C++, and except for placement new (which has awful syntax in my opinion) Agreed. I re-wrote all of our memory allocation handling earlier on this year so we could use a custom allocator for everything, and it was a lesson in pain. Not helping matters were that MFC has a nice bug where they new stuff in one place and call free on it later. wxWidgets wasn't a huge deal better, they have a nice part in their style guide which says to not do any allocation during static init, so that people can use custom allocators easily, then they go ahead and just break that rule left, right and centre. *shakes fist* EDIT: Oh, and the new call in MFC is in a header, so you pick up that call yourself, but the free is in the dll... grrr
|
# ? Dec 12, 2008 01:37 |
|
Olly the Otter posted:No. There's a bug in the function, and it's being called incorrectly. I was hoping you would inspect values with the debugger to ascertain which is the case -- you'd find that the table actually has the correct values at the time that the search occurs. Ok, so now i can actually see my full output. I noticed that i can find URL given the IP, but if given the URL i cannot find the IP. This leads me to think that my error is within code:
Edit: OK, i changed it to say "differ != 7). This works so it says it found the IP number, but now i am having to figure out how to have it cout the IP[differ] which is being stored as a "". Pookdaddy G fucked around with this message at 01:56 on Dec 12, 2008 |
# ? Dec 12, 2008 01:42 |
|
Is there some nanosleep or usleep equivalent in windows? Milliseconds are just slightly too coarse.
|
# ? Dec 12, 2008 03:18 |
|
|
# ? Apr 25, 2024 04:43 |
|
For a (singly) Linked List of Type T, LinkedList<T> how would I go about using a recursive quicksort on it with these parameters?code:
I've seen various different examples of it online and understand the algorithm, but this requires 3 different Linked Lists. One for the pivot, one list to hold everything before the pivot, and everything after. Getting the pivot into its own list is easy, since there's a function provided that pops off the front of one list and pushes it onto the other, but how to get the other two into lists combined with recursion, that's where I get lost. I guess what's confusing me the most is that I originally had the idea to iterate through the entire list using a for-loop, comparing each item into the original list to the pivot, and placing each element into its own respective list (the before list and the after list) based on how the comparison goes. Something like code:
|
# ? Dec 12, 2008 04:07 |