Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

Super Dude posted:

How can I add elements to the beginning of a vector? I essentially want to merge a vector and a string, putting one ahead of the other. I was going to just take the characters from the string, starting with the end, and add them to the beginning of the vector. Unfortunately, I can only find the push_back function, which adds it to the end. Would it be ok to make the string into a vector, then tack on the other vector?

edit: Thanks Milde.

I know that this was resolved, but why are you using a vector instead of a deque when you know that you'll be inserting at the beginning? Deques are designed to grow from both ends whereas vectors are not.

Adbot
ADBOT LOVES YOU

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

WalletBeef posted:

Has anyone been able to get Purify 7.0 properly working with Visual C++ in Visual Studio 2008, if so, what did you have to do to get both pieces of software working correctly?

I'm having a hell of a time getting purify to show me the exact line numbers where errors are occuring.

Right now, I get something like: Error at Class.obj:123
instead of Error at Class.cc:123.

For some reason purify isn't finding my source code, despite the fact that I specified the exact directory in the source look up.

It apparently doesn't work with VS 2008 PDB files. That' my guess, anyway. I was upgraded from 2005 to 2008 and started seeing exactly the problem that you describe. The good news is that the line numbers are accurate (though sometimes it will strangely specify the wrong object file). You just have to open them up outside of Purify.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

WalletBeef posted:

Well, I installed VS 2005 and Purify is happy. It's showing line numbers, source code and all. Thanks a loving ton. :unsmith:

np. I've tried various cl.exe and link.exe switches and I can't get VS2008 to generate the old-style PDB files. I'm a deeply lazy man, though, and don't feel like going back to 2005. I also have a weirdish development environment (I mount my windows drive from my mac and edit everything in vim), so I'm used to just opening the source files and finding the offending lines. Hopefully, though, IBM will release a fix soon enough. I don't know if you've tried dealing with IBM support yet but DO NOT attempt to submit this as a bug. You'll end up with a self-inflicted bullet wound.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.
I came across this a year or two ago:

rob pike posted:

Simple rule: include files should never include include files. If instead they state (in comments or implicitly) what files they need to have included first, the problem of deciding which files to include is pushed to the user (programmer) but in a way that's easy to handle and that, by construction, avoids multiple inclusions. Multiple inclusions are a bane of systems programming. It's not rare to have files included five or more times to compile a single C source file. The Unix /usr/include/sys stuff is terrible this way.

There's a little dance involving #ifdef's that can prevent a file being read twice, but it's usually done wrong in practice - the #ifdef's are in the file itself, not the file that includes it. The result is often thousands of needless lines of code passing through the lexical analyzer, which is (in good compilers) the most expensive phase.

Just follow the simple rule.

It made sense at the time and I've followed this rule ever since (it's Rob Pike for Christ's sake). But when it comes to libraries it starts to get overly tedious for people using your library. You don't want to have a monolithic header file (think of the sockets library only used one header) but you also don't want them to have to read comments in your headers to find dependencies. What do other people do?

And yes, I do employ #ifdef guards on the header files as well.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

TSDK posted:

Good christ, that guy's an idiot.

Yes, Rob Pike is an idiot.

Hey, at least my post generated more comments in this thread in 1 day than in the last week!

What I generally do now is have a monolithic include file to distribute with my libraries, but within each project I adhere to Pike's Law. One distinct advantage that's come of this is is a better understanding of which components use which other components, making bad design is much more blatant. Note also that 95% of my code is c, not c++, so it's easier to get away with this approach.

The real question is, how pissed off is the next developer to take over my projects going to be? Very, I assume.






Well of course he's not infallible. I was just responding to TSDK casually dismissing the dude as an idiot when he's one of the better engineers of his time.
vvvvvvvvvvvvvvvvvvvvvv

Plastic Jesus fucked around with this message at 16:45 on Mar 27, 2008

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

That Turkey Story posted:

You don't think that adhering to this "law" is a bad design on it's own!?

You don't force users to manually include all dependencies for lots of reasons, even apart from the obvious tediousness and room for error.

I just said that I don't do this for libraries for exactly the reasons you just listed. I do it within my own projects for exactly the reasons I just listed. I will probably flatten things when they go to release so that you do not kill me. But during development it is _nice_ to know where dependencies lie.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

Bush is a QT posted:

I have a question about fflush(stdin) in C programming.
It's supposed to clear some input buffer that holds input data. However I have yet to come across a situation that requires fflush for stdin. If possible, could someone explain a situation or give an example when fflush(stdin) or even stdout would be required?

Always flush shared file handles in multi-threaded applications. I can't for the life of my think of a time when multiple threads would be accessing stdin, but it happens all the time with stdout. If you have several threads that all want to print poo poo to the screen you have to make them stand in line to do so or they'll end up printing over the top of each other. So you make them acquire a mutex, then write to stdout. You want to flush stdout at that point to ensure that everything has been written before you release the mutex.

Fake edit: just thought of when you'd want to fflush(stdin)- when your application will be used in a pipe, e.g. 'cat somefile|yourapp'.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

slovach posted:

I guess this thread is the closest to what I'm looking for, not exactly C/C++ but rather the Win32 API.

Anyway, by any chance is there a function that returns the handle to the window it's being called from? (which be your own, except I'd be calling it from an injected DLL, I'm going to be drawing with GDI over what I'm injected into.) Something like FindWindow sounds a little rear end backwards when I'm already injected.

I'm not positive that I properly understood your question- are you trying to get the HWND associated with the thread in which you're executing? Look into EnumThreadWindows()- it does exactly what the name might imply.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.
Like slovach, I have a Windows API question and don't know which thread is best. So I'm posting here.

Did something happen to context menu creation in Vista? The following works just fine in every version of Windows except Vista (and actually works in Vista if you're using the Windows Classic theme):

code:

HMENU menu;
DWORD cmd;
POINT p;
HWND wnd;

wnd = my_global_hidden_window;
menu = CreatePopupMenu();
AppendMenu(menu, MF_STRING, MYMENU_CMD1, TEXT("menu item 1"));
AppendMenu(menu, MF_STRING, MYMENU_CMD2, TEXT("menu item 2"));
AppendMenu(menu, MF_STRING, MYMENU_CMD3, TEXT("menu item 3"));
AppendMenu(menu, MF_STRING, MYMENU_CMD4, TEXT("menu item 4"));
GetCursorPos(&p);
SetForegroundWindow(wnd);
cmd = TrackPopupMenuEx(menu, (TPM_RIGHTALIGN|TPM_RETURNCMD), p.x, p.y, wnd, NULL);

It will create a menu that's 4 items long, but there will be no text in any menu entry. The TrackPopupMenuEx() call works fine and will return the proper value for each entry. It's just the printing of text for the menu items that's failing and, like I said, only in the Vista default theme. I've tried explicitly using both Unicode and ASCII strings and that doesn't seem to matter.


Edit:

Figured it out, and it had nothing to do with the actual menu creation. I didn't have 'default: return(DefWindowProc())' in my windows WindowProc. Sure am glad that it took me 4 days to figure out that I'm a retard.

Plastic Jesus fucked around with this message at 15:30 on Apr 18, 2008

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.
I'm moving this here from the General thread, because this is where it belongs.


clockwork automaton posted:

:D It's okay though, because I figured it out. It was an issue with user space memory and kernel space memory all I had to do was use strcpy and it was saved.

You've actually got quite a lot of bad going on with your code. I only glanced at the code up on pastebin, but I noticed these things:

* You're allocating memory for 'name' in main() and never freeing it. What's worse is that you never actually use this memory since you later set name to point to something in argv[].

* You don't check the return value of any of your memory allocation routines, a tremendously bad idea.

* In your vector_resize() function you will end up with members of your array pointing to bad memory since you do this:

code:
   for(i=0; i<vector_len; i++)
   {
      vector_new[i] = (struct data*)kmalloc(sizeof(struct data*), GFP_KERNEL);
      vector_new[i]->name = vector[i]->name;
      vector_new[i]->bytes = vector[i]->bytes;
   }
 
   vector_free();
   vector = vector_new;
Also, what's the point of your vector datatype in the first place?

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.
On the client:

* You have a potential buffer overflow on line #66, since there won't be a null terminator (pass SLOTS as the length param to read_line). This could get messy on line 110 where you do an strlen() on the read buffer. You should also explicitly bzero() the input buffer.

* You don't check to see if received is a valid pointer before you free() it. If someone does "9999" on the first turn, that'll crash.


On the server:

* You don't check to make sure that your calls to calloc() on liness #57 and #90 don't fail.

* You don't check to ensure that input is non-NULL in the compare() function. You should also check to ensure that it is at least (SLOTS + 1) long.

* You don't allocate any space for 'correct' on line 69. In fact, I think that your declaration will result in a const char *, not a char *, so the assignment on line 74 should fail spectacularly. That whole loop is kinda weird. Wouldn't something like this work just as well and more simply?
code:

memset(output, 0, SLOTS);
for(i = 0; i < SLOTS; i++){
   if((input[i] - '0') == randomColors[i])
      output[i] = '1';
}
* There is a tiny memory leak on line 35. result already points to a valid buffer at that point, so you should free that before you do the assignment from getColors();

* I have no idea how RPC deals with freeing malloc()'d data, but hopefully it does since you never free anything.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

Harokey posted:

I'm trying to integrate some UNIXy (cygwin) code over to a visual studio project. I've never really dealt with visual studio, or windows programming in general, so I'm having a lot of problems.

I'm trying to change a lot of the pthreads calls into windows threads.

I've created the thread using AFXBeginThread. But I'm having trouble with the mutex's... It looks like I should be using the "CMutex" object.

Unless it's absolutely critical that you use MFC for thread management I recommend using the core Win32 thread management API calls. They're very straight-forward and in my opinion kick the tits off of pthreads. The SDK docs for threads and fibers are particularly good, so an afternoon of reading will more than cover what you need to know to get started.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

Professor Science posted:

honestly, K&R is always a good idea.

Learning to program by reading K&R is like learning French by reading Rimbaud. It can be done, but you're going to be confused, frustrated and it'll take you 3 times longer than necessary.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

Insurrectum posted:

What's a way to determine the runtime of a program for something like a Project Euler problem?

man time

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.
Is there a default XML parsing library that is guaranteed to be included on all versions of Windows since 2k SP4? It looks like MSXML 3.0 is only on XP and higher, 4.0 has to be installed separately and 5.0 only comes with Office 2003.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

Avenging Dentist posted:

Why not just statically link to TinyXML or something if it's that big a deal?

Because I'm supposed to remove any non-default dependencies for some unknown loving reason.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.
I've beat my head against this for like 3 wasted hours and it must just be my misunderstanding of c++.

In file some_consts.h I have
code:
namespace Foo{
   const std::string MyString("blah");
};
I have a singleton logger declared like this:

code:
class CLogger : private Uncopyable{
public:
    static CLogger& GetInstance()
    {
       static CLogger logger;
       return(logger);
    }

    void WriteLog(const char *szFormat, ...);

private:
    CLogger();
    ~CLogger();
I have a bunch of utility functions declared like this:

code:
namespace MyUtil{
   bool LookupValueBasedOnMyString(std::string& sResults);
};
I call MyUtil::LookupValueBasedOnMyString() from CLogger()::CLogger() like this:

code:
fprintf(stderr, "CLogger::CLogger(): Foo::MyString is '%s'\n", Foo::MyString.c_str());
MyUtil::LookupValueBasedOnMyString(sSomeString);
In MyUtil::LookupValueBasedOnMyString() I do this:
code:
fprintf(stderr, "MyUtil::LookupValueBasedOnMyString(): Foo::MyString is '%s'\n", Foo::MyString.c_str());
In main I do this:
code:
CLogger logger(CLogger::GetInstance());
...
MyUtil::LookupValueBasedOnMyString(sString);
MyUtil::LookupValueBasedOnMyString(sString);
MyUtil::LookupValueBasedOnMyString(sString);
This is the output:

code:
CLogger::CLogger(): Foo::MyString is 'blah'
MyUtil::LookupValueBasedOnMyString(): Foo::MyString is ''
MyUtil::LookupValueBasedOnMyString(): Foo::MyString is 'blah'
MyUtil::LookupValueBasedOnMyString(): Foo::MyString is 'blah'
MyUtil::LookupValueBasedOnMyString(): Foo::MyString is 'blah'
Why in the gently caress is Foo::MyString empty in MyUtil::LookupValueBasedOnMyString() when it's called from the CLogger ctor but not when it's called from other places?

Compiler is VS2008.

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

Avenging Dentist posted:

EDIT: Didn't see the const. Const variables, by default, do have internal linkage. The "solution" doesn't actually solve anything though, because it still relies on a particular static initialization order.

EDITx2: The real solution is to just use a const char[]

Yah, I tried that solution and it did nothing (except make the string empty in CLogger while have content in MyUtil). Adding static made no difference in either approach.

Doing cost char* is just so ugly. And as a larger solution, how would I do this sort of thing with anything that's not a base type? Is it just not possible a namespace level?

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

Avenging Dentist posted:

What. Who cares?

Anyone who wants to pass a const string& instead of a const char * to a function? Or anyone who wants to use template string constants to reduce pain when you need support unicode? If I just cared about solving this exact problem I'd have used a #define and moved on.

Avenging Dentist posted:

The link in Standish's post explains one (of several) solutions to the problem.

Calling a function isn't really a solution to this problem. This is an extremely easy (and "effective" even) thing to do at the class level, I'm just wondering if it's something that can only be done within a class. And if so, why was that not mentioned in Meyers's book?

Adbot
ADBOT LOVES YOU

Plastic Jesus
Aug 26, 2006

I'm cranky most of the time.

Avenging Dentist posted:

:words:

Goddamn you're needlessly a prick.

I have this question because I've done the exact same thing with the CString in MFC (which does use templates, exactly for the reason I talked about) and haven't run into this problem. OMG I HAVE A STRING LITERAL IN MY CODE JESUS WHAT WILL I DO?

const CString OhGodYouInternetPosters(_T("You piss me off so much you stupid newbie programmers who have questions about books"));

Thanks for all the help though, you self-satisfied and condescending oval office.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply