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
TSDK
Nov 24, 2003

I got a wooden uploading this one

Bitruder posted:

I have a feeling it's because p_plugin is of type A*, however, my main program doesn't know what "B" is called... so I have to use type A*. Any ideas what I can do here?
Drop down to disassembley and step through what happens when you step into init. I have a feeling that the compiler might have reasoned that, since you only have one version of A in your program, it can optimise that virtual function call into a concrete one.

I've never tried using virtual function calls over a DLL boundary, but I wouldn't be surprised if you need to jump through one or two hoops. This looks like a nice tutorial:
http://www.codeproject.com/KB/DLL/PluginSystem.aspx

Adbot
ADBOT LOVES YOU

DividesByZero
Jan 24, 2006
Everything should be made as simple as possible, but no simpler

TSDK posted:

Your understanding is wrong:

*repeats "thou shalt not try to use uninitialised pointers" one thousand times*

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge

DividesByZero posted:

Why doesn't this work? My understanding is that scanf allocates memory and stores the read object at the pointer provided as the second argument. That is scanf("%s",names[i]) should store the scanned in string at the location pointed to by the ith element of names. Where am I doing something dumb? Is it because names[i] is not actually pointing anywhere yet since it's not initialised when I do the scanf?

Now you know why this is wrong you might be tempted to use gets(). Don't. At the least use fgets() or if you want to go non standard and are using a GNU based system, use getline()

Contero
Mar 28, 2004

I've used svn in groups for smaller programming projects, but it's never been very organized and I honestly don't know a lot about how to manage a repository. I'm about to start working on a new project and I want to make it a bit more organized.

I want to set up the repository so that each person working on the project can work on their own branch that might be a bit experimental and can incorporate their changes back into the trunk version after a certain amount of QA is done. The only problem is I have no idea how to do this and even if I messed around with it I have no idea if I'm doing it right. So what's the right way to accomplish this?


Edit: gently caress I thought I was in the general thread

Contero fucked around with this message at 02:10 on Mar 30, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
By using git. :c00l:

awesmoe
Nov 30, 2005

Pillbug

Contero posted:

I've used svn in groups for smaller programming projects, but it's never been very organized and I honestly don't know a lot about how to manage a repository. I'm about to start working on a new project and I want to make it a bit more organized.

I want to set up the repository so that each person working on the project can work on their own branch that might be a bit experimental and can incorporate their changes back into the trunk version after a certain amount of QA is done. The only problem is I have no idea how to do this and even if I messed around with it I have no idea if I'm doing it right. So what's the right way to accomplish this?

This isn't a c/c++ question, but anyway. As I understand it, if the main focus of your VCS is to allow developers to have their own play area, then a distributed solution (git, rather than svn) might be the way to go.

Ghotli
Dec 31, 2005

what am art?
art am what?
what.
Git is made for this sort of thing. I moved over to it a few months ago and find it to be more sensible than SVN on all fronts.

Thug Bonnet
Sep 22, 2004

anime tits ftw

What is a reasonable way to express a many-to-many relationship between objects of differing type with the ability of an object of either type to find out about it's relationships?

There should be a mediator involved between any given relationship, but how would I accomplish the actual structure without just having lists that need traversing?

For example

[ObjectA]<-->[Mediator]<-->[ObjectB]

Either object would be need to be able to query the mediator and find which objects are claiming a relationship. Ideally this would be similar to the STL::multimap that can be queried by either key or value.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Look at Boost.Bimap: http://www.boost.org/doc/libs/1_38_0/libs/bimap/doc/html/index.html

Thug Bonnet
Sep 22, 2004

anime tits ftw

Avenging Dentist posted:

Look at Boost.Bimap: http://www.boost.org/doc/libs/1_38_0/libs/bimap/doc/html/index.html

Beautiful, thanks! Is that type of structure commonly known as a "bitmap"? I haven't seen it used that way before..

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Thug Bonnet posted:

Beautiful, thanks! Is that type of structure commonly known as a "bitmap"? I haven't seen it used that way before..

Bimap, no T.

Thug Bonnet
Sep 22, 2004

anime tits ftw

Avenging Dentist posted:

Bimap, no T.

Yow, thanks again

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Thug Bonnet posted:

Yow, thanks again

But you can implement bimaps as adjacency matricies, stored as bitmaps.

ShardPhoenix
Jun 15, 2001

Pickle: Inspected.

Luminous posted:

I know you read the link the other guy posted and said it helped, but couldn't tell if you had actually found the solution to whatever you were doing.

Anyway, I cooked this very small example up that shows some ways you might use int**.
I was able to get it working with the help of the other link. Basically I'd forgotten that I had to allocate all the memory manually when using the pointer syntax rather than the array syntax - although I see that your way of doing it by copying the pre-existing array avoids having to use malloc.

RavenKrows
May 29, 2008
Hello everyone, I'm currently working on an array and file open/close program for C. My program has become rather wonky lately and is giving me strange values. Not included in the code below is a case where I set i = 0 and then proceeded to print its value to screen and I was rewarded with something similar to 4197468.

Anyways, the number 4197468 is the result of this little statement down here which is a small chunk of the code, frankly it should be zero here as well so I'm at a loss. A friend of mine thinks I must be accessing files incorrectly but I honestly can't find the issue. Any help would be appreciated.
code:
int main (void)
{
   int seed;
   int numtoRoll;
   int diceRolls[36] = {0};
   FILE* FPIn;
   FILE* FPOut;

   printf("%d", "diceRolls[35]");
   if(openFiles(&FPIn, &FPOut));
   {
      getInput(&FPIn, &seed, &numtoRoll);
      genRand(seed, numtoRoll, diceRolls);
   }

   return 0;
}
I'll also include my open files
code:
bool openFiles(FILE **pFPIn, FILE **pFPOut)
{
   bool result = true;
   
   if ((*pFPIn = fopen("test.txt", "r")) == NULL)
      {
      printf("Error opening test.txt\n");
      result = false;
      }
   else
   {
      if((*pFPOut = fopen("test.out", "w")) == NULL)
      {
         printf("Error opening test.out\n");
         result = false;
      }
   }
   return result;
}
and finally my get input
code:
void getInput(FILE** pFPIn, int *pSeed, int *pnumtoRoll)
{
   fscanf(*pFPIn, "%d %d", pSeed, pnumtoRoll);
   fclose(*pFPIn);
   return;
}
The two values that fscanf will get are 5 and 100 respectively, it does this successfully, or at least they've had no impact on the errors as I've manually entered the values later where they're needed and I end up with the same problems.

Dijkstracula
Mar 18, 2003

You can't spell 'vector field' without me, Professor!

code:
printf("%d", "diceRolls[35]");
Look carefully at this line. Specifically, what is the type of the second argument, and what is the format character you're specifying in the first argument? Which is the wrong one?

edit: If you make the correction, you'll see that diceRolls[35] is in fact 0. Also, using the debugger is better than sprinkling printfs around your code.

Dijkstracula fucked around with this message at 18:13 on Mar 29, 2009

RavenKrows
May 29, 2008

Dijkstracula posted:

code:
printf("%d", "diceRolls[35]");
Look carefully at this line. Specifically, what is the type of the second argument, and what is the format character you're specifying in the first argument? Which is the wrong one?

edit: If you make the correction, you'll see that diceRolls[35] is in fact 0. Also, using the debugger is better than sprinkling printfs around your code.

I stared at it blankly for a second, then I realized I R stupid. Thank you for this and thank you for the debugger. My array which didn't appear to be working correctly is now fixed as well.

Mach Won
Jun 17, 2004

Is UNC playing? My post is either whining about (1) ref favoritism, (2) unfair recruiting/player caliber, (3) opposing team sucking or (4) the inevitable UNC win. The day you see me giving UNC credit for anything is the day someone hacked my account.

Roy era: 1-16 :qq: RIVALS!!!!!
Haha, what the hell, how do I fix this problem? I'm using Visual Studio 2008 for reference.

I'm trying to complete this little tutorial, http://www.codeproject.com/KB/IP/winsockintro01.aspx, and I'm getting this error:

fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include <windows.h>

Now, I hunted down where windows.h is included and it is in winsock2.h. The author says that MFC support must be added, but this apparently creates a conflict, where I can't include windows.h. How do I get around this?

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
What happens if you do what it tells you and don't explicitly include windows.h?

Mach Won
Jun 17, 2004

Is UNC playing? My post is either whining about (1) ref favoritism, (2) unfair recruiting/player caliber, (3) opposing team sucking or (4) the inevitable UNC win. The day you see me giving UNC credit for anything is the day someone hacked my account.

Roy era: 1-16 :qq: RIVALS!!!!!
I don't ever explicitly include it, I include winsock2.h.

Mustach
Mar 2, 2003

In this long line, there's been some real strange genes. You've got 'em all, with some extras thrown in.
I don't know enough about MFC to figure out the header mess, but it looks like he only uses MFC for AfxBeginThread() so that he can have his little _getch() loop. Since WinSock is all you care about, you could not include MFC, change main to
code:
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){
    ServerThread(0);
    return 0;
}
and then just close the window to kill the program.

Pooball
Sep 21, 2005
Warm and squishy.
edit: this was wrong

TSDK
Nov 24, 2003

I got a wooden uploading this one

Mach Won posted:

I don't ever explicitly include it, I include winsock2.h.

http://msdn.microsoft.com/en-us/library/09dd1ycd(VS.80).aspx posted:

MFC supports Windows Sockets 1 but does not support Windows Sockets 2. Windows Sockets 2 first shipped with Windows 98 and is the version included with Windows 2000.
So it looks like you either need to use CSocket, if you want to keep MFC, or drop MFC in order to use winsock2.

Mach Won
Jun 17, 2004

Is UNC playing? My post is either whining about (1) ref favoritism, (2) unfair recruiting/player caliber, (3) opposing team sucking or (4) the inevitable UNC win. The day you see me giving UNC credit for anything is the day someone hacked my account.

Roy era: 1-16 :qq: RIVALS!!!!!
I don't care which I use, I just want this to work. Why would that guy say to use winsock2.h in his tutorial if it doesn't work :psyduck:

Pooball
Sep 21, 2005
Warm and squishy.
He says "Open the file stdafx.h and add the following line :- #include <winsock2.h>"

Did you add it at the start? Try adding it at the end instead.

edit:
in fact this solution is in one of the comments: http://www.codeproject.com/KB/IP/winsockintro01.aspx?msg=2010952#xx2010952xx

Look for the bit in stdafx.h where it says "// TODO: reference additional headers your program requires here".

Pooball fucked around with this message at 23:50 on Mar 30, 2009

POKEMAN SAM
Jul 8, 2004
I'm playing around with TMP in GCC 4.3.2's half-implementation of C++0x, and I was wondering if there was a way to somehow do the following:

code:

template <char x, char... c>
struct mystruct {
...
};

int main () {

   mystruct<"asdf">::go();

}

It obviously won't let me do it just like that, and I thought I'd get lucky by using user-defined literals to transform the "asdf" string during compile-time, but GCC 4.3 doesn't support user-defined literals...

Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

I was reading earlier, and saw this:

quote:

First, however, let’s start with a simple cache quiz: which code is faster?
code:
/* going vertical */
for (x=0;x<width;x++)
  for (y=0;y<height;y++)
     a[y*width+x] = b[y*width+x] + c[y*width+x]

/* going horizontal */
for (y=0;y<height;y++)
  for (x=0;x<width;x++)
     a[y*width+x] = b[y*width+x] + c[y*width+x]
The second one. Right? Every programmer knows that the second is faster, and they know why (I hope). A simple matrix addition is simple enough that we can “reason” our way into which is theoretically faster without actually trying it. I recently ran a much more complex example of a similar problem.

Can someone explain to me why? I havent't programmed in a while and I'm disappointed that I can't figure it out.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Bob Morales posted:

Can someone explain to me why? I havent't programmed in a while and I'm disappointed that I can't figure it out.

Assume the structure in question is large. When you make large jumps (incrementing by width each iteration, instead of incrementing by 1) you increase the likelihood of a cache miss, because CPU caches bring in lines of adjacent words from memory at a time.

So, with large jumps, the logic is essentially: read several words from memory, do operation on one of those words, read several different words from memory, etc. Much of the cache is wasted, and you tend to stall on memory accesses.

With incremental jumps, the logic becomes: read several words from memory, do operation on first of them, do operation on next of them, do operation on next of them, etc, read next set of words from memory, etc. The CPU spends more time doing actual work, and less time doing memory accesses.

Bob Morales
Aug 18, 2006


Just wear the fucking mask, Bob

I don't care how many people I probably infected with COVID-19 while refusing to wear a mask, my comfort is far more important than the health and safety of everyone around me!

It was an article on optimizing cache use.

That makes perfect sense, I had a brain fart. I guess I wasn't aware that more memory was cached than actually accessed.

heeen
May 14, 2005

CAT NEVER STOPS
Can someone help me with this error:

error C2664: 'v8::FunctionTemplate::New' : cannot convert parameter 1 from 'v8::Handle<T> (__cdecl *)(const v8::Arguments &)' to 'v8::InvocationCallback'

Relevant definitions:

code:
typedef Handle<Value> (*InvocationCallback)(const Arguments& args);




template<class C> class V8ScriptClass
{
public:
    template<class C, typename Rtype, typename Ptype1, Rtype (C::*FuncPtr)(Ptype1)> 
    void RegisterFunc(const char* const scriptname)
    {
        objtemplate->Set(
                v8::String::New(scriptname), 
                v8::FunctionTemplate::New(
                V8ScriptClass<C>::RelayCallback<C, Rtype, Ptype1, FuncPtr>
                        ));
    };

template<typename Rtype, typename Ptype1, Rtype (*FuncPtr)(Ptype1 param1)>
static v8::Handle<v8::Value> RelayCallback(const v8::Arguments& args)
{
        std::cerr<<__FUNCTION__<<std::endl;
        v8::HandleScope handle_scope;
        return handle_scope.Close(toJSType( ((FuncPtr)(toCType(args[0]))) ));
};


class EXPORT FunctionTemplate : public Template {
 public:
  /** Creates a function template.*/
  static Local<FunctionTemplate> New(
      InvocationCallback callback = 0,
      Handle<Value> data = Handle<Value>(),
      Handle<Signature> signature = Handle<Signature>());

Looks to me like the typedef and the actual function signature are identical.

Olly the Otter
Jul 22, 2007
code:
V8ScriptClass<C>::RelayCallback<C, Rtype, Ptype1, FuncPtr>
RelayCallback only has three template parameters, so this should be:

code:
V8ScriptClass<C>::RelayCallback<Rtype, Ptype1, FuncPtr>

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.

Standish
May 21, 2001

Plastic Jesus posted:

In file some_consts.h I have
code:
const std::string MyString("blah");
By default variables have file scope, so every time you do #include "some_consts.h" you're creating a new copy of MyString with the scope of the file that includes it (remember #include is really really dumb and just does plain text substitution). If you output the address of the MyString objects:
code:
fprintf(stderr, "CLogger::CLogger(): Foo::MyString is '%s'@%p\n", Foo::MyString.c_str(), &Foo::MyString);

fprintf(stderr, "MyUtil::LookupValueBasedOnMyString(): Foo::MyString is '%s'@%p\n", Foo::MyString.c_str(), &Foo::MyString);
you can see that they're actually different objects.

It's blank because you're running into the static initialization order fiasco.

Solution is to declare (but not define) Foo::MyString in your header:
code:
extern const std::string MyString;
and then define it in exactly one of your cpp files:
code:
const std::string Foo::MyString("blah");

Standish fucked around with this message at 22:12 on Apr 2, 2009

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Standish posted:

By default variables have file scope, so every time you do #include "some_consts.h" you're creating a new copy of MyString with the scope of the file that includes it (remember #include is really really dumb and just does plain text substitution).

Completely false. This only applies to objects which have internal linkage, as specified by static (or, more-or-less, by anonymous namespaces). File scope is a completely different concept from linkage and refers to the lexical scope of an identifier, not its linkage. It doesn't make sense to talk about file scope being default because you must explicitly declare an identifier at file scope for it to have file scope.

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[]

Avenging Dentist fucked around with this message at 22:34 on Apr 2, 2009

Whilst farting I
Apr 25, 2006

If I have a character array that's going through a loop and constantly getting new information, what's the best way to stop things like this from happening/reset them each time through

quote:

Hi
Hello
Hillo

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Null-terminate your strings.

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?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Plastic Jesus posted:

Doing cost char* is just so ugly.

What. Who cares?

Plastic Jesus posted:

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?

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

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

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh

Plastic Jesus posted:

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.

Luckily for you, char* can be passed to any function that accepts an std::string (yes, I already know you're going to whine about the "efficiency" of constructing a new string because newbie programmers loving love unnecessary micro-optimizations). You can also use TCHAR in Windows, and do similar stuff in Unix, though I'd recommend against it, since sizeof(wchar_t) == 4 on most Unixes, and you would be (rightly) laughed at for using UTF-32.

Also lol at the idea that using templated strings would do anything to make Unicode less painful. The C++ Standard Library's Unicode support is minimal at best. Besides that, you still have a string literal in your code that you'd need to adjust for Unicode. std::wstring doesn't accept narrow character strings. Hell, the size of wide characters isn't even specified (as implied above).

Plastic Jesus posted:

Calling a function isn't really a solution to this problem.

Yes it is. You do understand what static inside of a function denotes, right?

Plastic Jesus posted:

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.

That's because you don't understand C's (and by extension, C++'s) compilation model. If you did, the solution would be obvious to you.

Plastic Jesus posted:

And if so, why was that not mentioned in Meyers's book?

"Oh no, book X doesn't tell me how to solve a particular problem, better complain about it!"

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