|
ok, I might be stupid and not understanding anything, but here's a problem I'm having: This code reads in a file line by line, adding each element to a linked list (separated by commas). It works perfectly for the first line, but seg faults right after it exits the while loop. Here's the code: code:
|
# ? Dec 7, 2009 06:58 |
|
|
# ? May 19, 2024 06:36 |
|
Strtok() returns null when there aren't any tokens left, but you aren't checking the return value before passing it as a parameter to atoi(). This is probably what you wanted:code:
Paniolo fucked around with this message at 07:22 on Dec 7, 2009 |
# ? Dec 7, 2009 07:06 |
|
I've got a interview with Microsoft this week and I've been brushing up my C skills but I ran into a problem when trying to reverse the order of the words in a string ("hello friend" becomes "ollef dneirf").code:
code:
The code works in C# (with a StringBuilder object b/c String objects are immutable) so I know the logic of it is sound. Any idea?
|
# ? Dec 8, 2009 07:18 |
|
CliffyBMine! posted:Any idea? Stop trying to write to a string-literal. (Also that code is a huge mess organizationally.)
|
# ? Dec 8, 2009 07:36 |
|
CliffyBMine! posted::words See http://csharppad.org/ViewPaste/WWAhztYz6EKgRMHMFWOS4A I converted your code to .NET code and its output is broken. "dnierf" isn't the backwards of "friend".
|
# ? Dec 8, 2009 07:40 |
|
Avenging Dentist posted:Stop trying to write to a string-literal. (Also that code is a huge mess organizationally.) This is my second try, I wanted to see if I could do it in-place this time. Is it possible? edit: shrughes posted:See http://csharppad.org/ViewPaste/WWAhztYz6EKgRMHMFWOS4A That's weird, the output of my C# code is "olleh dneirf". CliffyBMine! fucked around with this message at 07:49 on Dec 8, 2009 |
# ? Dec 8, 2009 07:43 |
|
CliffyBMine! posted:This is my second try, I wanted to see if I could do it in-place this time. Is it possible? Of course it is, and it's possible to make it a lot easier to read.
|
# ? Dec 8, 2009 07:48 |
|
CliffyBMine! posted:Any idea? First of all, the most important thing is to be able to describe your thought process while writing the algorithm. Judging from the poor way you presented your code here (debug flags still intact, no comments), you should probably work on that. Secondly, you're probably calling your function like reverseOrderOfString("A string") - that dog won't hunt in C. Rather than modifying the string in place, your function should allocate a new string, copy it from the input string (do the word-reversal during the copy), and then return the new string - much better function. Thirdly, just answer your interview questions in C# if you're more comfortable with it - in my experience most interviewers will allow you to choose the language, they're really only interested in your thought process and problem-solving ability. Good luck. Edit: Yes, you can do an in-place version, but you'll get segmentation faults if you try to modify a string literal. AD already pointed out that the organization could be better. Gumbercules fucked around with this message at 07:52 on Dec 8, 2009 |
# ? Dec 8, 2009 07:48 |
|
Gumbercules posted:First of all, the most important thing is to be able to describe your thought process while writing the algorithm. Judging from the poor way you presented your code here (debug flags still intact, no comments), you should probably work on that. On this note, changing "hello friend" to "olleh dneirf" is not "reversing the order of the words in a string", it is "reversing the order of the letters in each word", and any interviewer for a programming position worth their salt will see huge warning lights if you can't describe problem statements clearly and correctly. Remember: It'll probably be assumed that as long as you can communicate well and program in some language, you can be trained to use any normal programming environment, language, and libraries that the job needs you to use. You don't need to show off your ability to do specifics like work in-place in C unless they specifically ask for that - you need to demonstrate that you can do problem-solving in general.
|
# ? Dec 8, 2009 07:54 |
|
Gumbercules posted:First of all, the most important thing is to be able to describe your thought process while writing the algorithm. Judging from the poor way you presented your code here (debug flags still intact, no comments), you should probably work on that. Good to know, the college recruiter I was assigned to only mentioned C and C++ so I assumed it was required. Thanks for the advice by the way! ShoulderDaemon posted:On this note, changing "hello friend" to "olleh dneirf" is not "reversing the order of the words in a string", it is "reversing the order of the letters in each word", and any interviewer for a programming position worth their salt will see huge warning lights if you can't describe problem statements clearly and correctly. Yeah that's what I meant, sorry I'll try to work on code presentation next. Thanks for the advice guys, it helps a lot.
|
# ? Dec 8, 2009 08:14 |
|
CliffyBMine! posted:code:
code:
|
# ? Dec 8, 2009 08:56 |
|
FastEddie posted:I hereby present FastEddie the Cavern of COBOL FizzBuzz Award for correctly solving the wrong problem!!!
|
# ? Dec 8, 2009 09:10 |
|
code:
|
# ? Dec 8, 2009 09:13 |
|
print reverse.' 'for split/ /,'hello friend' Grats on writing python with more punctuation than perl, somehow
|
# ? Dec 8, 2009 09:33 |
|
Captain Lou posted::-1]
|
# ? Dec 8, 2009 09:40 |
|
FastEddie posted:And here's a test: FAIL - expected 六, got ��� test("六", "六");
|
# ? Dec 8, 2009 09:59 |
|
Avenging Dentist posted:I hereby present FastEddie the Cavern of COBOL FizzBuzz Award for correctly solving the wrong problem!!!
|
# ? Dec 9, 2009 00:37 |
|
FastEddie posted:Not if you put "olleh dneirf" into it. He wants to reverse the words in a sentence, no? And he's already reversed each word. ShoulderDaemon posted:On this note, changing "hello friend" to "olleh dneirf" is not "reversing the order of the words in a string", it is "reversing the order of the letters in each word", and any interviewer for a programming position worth their salt will see huge warning lights if you can't describe problem statements clearly and correctly.
|
# ? Dec 9, 2009 00:40 |
|
FastEddie posted:Not if you put "olleh dneirf" into it. He wants to reverse the words in a sentence, no? And he's already reversed each word. Yes, that is clearly what he meant and you're clearly not just backpedaling in an attempt to save face.
|
# ? Dec 9, 2009 00:40 |
|
Avenging Dentist posted:Yes, that is clearly what he meant and you're clearly not just backpedaling in an attempt to save face.
|
# ? Dec 9, 2009 00:45 |
|
FastEddie posted:Why would he want to reverse the characters in each word? That's the stupidest interview question I've ever heard of! I'll be sure to mention this to the interviewer if the question comes up.
|
# ? Dec 9, 2009 05:11 |
|
shrughes posted:FAIL - expected 六, got ��� maybe im missing something here but surely any function that manipulates words and letters is gonna choke if you call it with a picture?
|
# ? Dec 9, 2009 08:00 |
|
Nigglypuff posted:maybe im missing something here but surely any function that manipulates words and letters is gonna choke if you call it with a picture? Yes, you're missing something.
|
# ? Dec 9, 2009 08:28 |
|
but aren't English words just pictures of letters?
|
# ? Dec 9, 2009 08:35 |
|
On a C++ test I took there was the question "Why did Stroustrup apologize for auto_ptr?". It was a piece of information that I missed. Can anyone explain?
|
# ? Dec 9, 2009 17:52 |
|
I have a function definition as follows: void foo(void *arr, size_t num_elem, size_t elem_size) Arr can point to any array of strings, ints, longs, floats, whatever. I'm having trouble referencing this array and preforming things such as memcpy, or just referencing this array at all. I know I need to use casting and pointer arithmetic, and I don't have trouble if I know the array is full of ints or floats, but strings/chars are confusing me. How would I go about memcpying this array regardless of type, and referencing its elements, regardless of type. Yes this is school related, but I've been messing with this for a while now, and this is just a very small part of a very large program. Any tips would be appreciated.
|
# ? Dec 9, 2009 19:24 |
|
It's not a good idea to memcpy non-POD types (ie classes like string) because they may have dynamically allocated members. To reference its elements you need to know what type it is. Just size alone won't work. Either pass in some identifier which specifies this and cast it, or (better) use templates.
|
# ? Dec 9, 2009 19:52 |
|
You cannot tell whether you want to copy the data in the array or whether you need to interpret the array as pointers to follow, so there is nothing you can do. What do you mean by referencing the elements? Without knowing what type they are, that does not really buy you anything.
|
# ? Dec 9, 2009 20:02 |
|
mellifluous posted:On a C++ test I took there was the question "Why did Stroustrup apologize for auto_ptr?". It was a piece of information that I missed. Can anyone explain? Because it was a terrible, lazy conflagration of the proposed safe_ptr and counted_ptr (now unique_ptr and shared_ptr in C++0x).
|
# ? Dec 9, 2009 20:04 |
|
irishcoffee posted:I have a function definition as follows: code:
quote:and referencing its elements, regardless of type. code:
FastEddie fucked around with this message at 21:01 on Dec 9, 2009 |
# ? Dec 9, 2009 20:58 |
|
Can somebody explain how using references instead of pointers when recording children for a tree can save on space and provide a short coding example?
|
# ? Dec 9, 2009 23:12 |
|
RussianManiac posted:Can somebody explain how using references instead of pointers when recording children for a tree can save on space and provide a short coding example? No because that makes no sense. Basically every real compiler implements references as const pointers, more or less (note: not pointers to const).
|
# ? Dec 9, 2009 23:42 |
|
Avenging Dentist posted:No because that makes no sense. Basically every real compiler implements references as const pointers, more or less (note: not pointers to const). I was told that some compilers might do optimizations and use less than word size if reference is used versus pointer. Are there situations where this is possible?
|
# ? Dec 9, 2009 23:45 |
|
RussianManiac posted:I was told that some compilers might do optimizations and use less than word size if reference is used versus pointer. Are there situations where this is possible? I guess on architectures where near and far pointers are different sizes, the compiler might be able to statically verify that certain references only need the storage allocated for a near pointer? I mean, theoretically, it's possible for a compiler to prove in some cases that all accesses of some object are through a pointer in a parent object, and just inline the child into the parent, but that involves potentially special-casing functions that access the parent, and it raises all sorts of complications with sizeof, aliasing, pointer arithmetic... I don't think any halfway sane C compiler would ever try to go down that road.
|
# ? Dec 10, 2009 00:01 |
|
ShoulderDaemon posted:I guess on architectures where near and far pointers are different sizes, the compiler might be able to statically verify that certain references only need the storage allocated for a near pointer? My CS professor mentioned this the other day when I was talking about my SuffixTree implementation and saving on memory. I guess he must have been tripping, or I just misunderstood him? RussianManiac fucked around with this message at 00:05 on Dec 10, 2009 |
# ? Dec 10, 2009 00:02 |
|
RussianManiac posted:My CS professor mentioned this the other day when I was talking about my SuffixTree implementation and saving on memory. I guess he must have been tripping? For structures like trees where you frequently know an upper bound on the space requirements in advance, a common tactic is to allocate an array of nodes instead of individual nodes, and store offsets into the array instead of pointers. If you know you'll never need a tree with more than 216 nodes, for example, you can then use a smaller space to store the index than you could store the pointer in. Are you sure this isn't what he was talking about? Or, depending on your structure, you can make the pointers completely implicit in the ordering of the nodes in the array, as is commonly done with balanced binary trees.
|
# ? Dec 10, 2009 00:07 |
|
ShoulderDaemon posted:For structures like trees where you frequently know an upper bound on the space requirements in advance, a common tactic is to allocate an array of nodes instead of individual nodes, and store offsets into the array instead of pointers. If you know you'll never need a tree with more than 216 nodes, for example, you can then use a smaller space to store the index than you could store the pointer in. Are you sure this isn't what he was talking about? Oh, it might have been. Makes sense. Thanks! I think in my case the number of nodes shouldn't be getting bigger than 2^32, so I could save some space this way on 64 bit machine right? But if my number of nodes is larger than 2^32 then there is no point in doing this? Is there a way to store an integer using 5 or 6 bytes that would also be efficient?
|
# ? Dec 10, 2009 00:11 |
|
Using reference for trees would also be rather annoying, considering you can't just set them to null...
|
# ? Dec 10, 2009 00:13 |
|
I need to create a shitload (dozens of millions) of objects and store them in an std::vector. Currently I'm doing a loop for the number of objects I need to create, and new-ing them one by one and shoving them into the vector. The objects are all the same type and use the default constructor with no parameters (ie vec[i] = new foo();) The problem of course, is that's slow as gently caress, and ends up taking almost twice as much memory as it should, due to fragmentation. Is there any black magic I can do to solve this? Note also that doing a single big allocation is not good either, since it's gigs of memory. Ideally I would like to solve both the speed issue and the memory usage issue, but the most important is the memory issue.
|
# ? Dec 10, 2009 14:39 |
|
|
# ? May 19, 2024 06:36 |
|
I do not have any black magic to offer, so chances are you thought of this and it just does not work for your, but maybe you could use a deque<foo> instead of a vector<foo*> so you do not have to allocate pointers to your objects. The deque makes it not-necessarily-contiguous, so presumably it will be doing a few smaller allocations rather than a single big one, too. Otherwise you could manually allocate chunks of memory, like new foo[1024]; and then have the elements of your vector point inside those arrays. The downside is that you would have to track the lifetime of the chunks separately, I suppose, but it would let you work around the fragmentation issue. A fancier approach to this is making the vector use a custom allocator that does something similar, but I have no idea how much that buys you. Presumably you cannot get around having to call the default constructor for each object, but maybe allocating raw memory (ie, char[n]) and just memsetting it to zero is appropriate and faster for your objects. There is a bunch of stuff you can do with choosing another data structures, I suppose, but I have no idea what your requirements are beyond storing a huge amount of objects somewhere.
|
# ? Dec 10, 2009 14:59 |