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
OddObserver
Apr 3, 2009
Googling for that comment finds this:
http://profile.iiita.ac.in/IEC2008063/Documents/study/imp books/adc/Microsoft Visual Studio 9.0/VC/ce/crt/src/xexp.c

And a bunch of similar files. Seems like some sort of source file for FP routines in MSVC runtime library has some floating point array, and the auto-completion indexed it somehow.

Adbot
ADBOT LOVES YOU

slovach
Oct 6, 2005
Lennie Fuckin' Briscoe
Figures I should have just googled it :(

RonniePudding
Oct 18, 2005

I'm trying to pass a string from C++ to C#.

Here is what I have right now:

My C++ exported function:
code:
PLUGIN_API char* getJavascript(){

       *jsArray = const_cast<char*> ( pWindow->returnJS().c_str () ); <--doesn't work

       //*jsArray = "this is a test"; <-----this works perfectly!

        return jsArray[0] ;
}
function that passes string to above exported function:
code:
inline std::string UBW::returnJS() {  return jsString;  } //jsString here is defined as "test string"
C# code that receives char pointer and marshals producing a string:
code:
	[DllImport ("UBW")]
	private static extern IntPtr UBW_getJavascript(int windowID);
	
	void updateJS(){
		
		jsString = Marshal.PtrToStringAnsi(UBW_getJavascript(currentID));
		
	}
Now, in the above code, I can uncomment the string before the return (in the first block of code) and the string is properly passed into C#. However casting to const and converting to c style string is creating some problems but I'm not sure how. Any help would be appreciated. Thanks.

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.
"Some problems" could be anything, but I'm going to guess that it's because you're returning a temporary std::string's c_str(). The result of c_str() is owned by the std::string object, and so will be deleted when the object's scope ends (in this case, the end of the statement which assigns it to *jsArray). You'll need to make a dynamically-allocated copy of the C-style string and return that. This means you'll have to make an API function deleteJavascript() or something like that, and call it after you assign to jsString in the C# code like this:
code:
[DllImport ("UBW")]
private static extern IntPtr UBW_getJavascript(int windowID);

void updateJS(){
    IntPtr ptr = UBW_getJavascript(currentID);
    jsString = Marshal.PtrToStringAnsi(ptr);
    UBW_deleteJavascript(ptr);
}

RonniePudding
Oct 18, 2005

Thanks for the reply Mustach. I tried your method but still get no output.
What's strange is that I can output the result into a log file just fine:
code:
cerr << "~passed string: " << const_cast<char*> ( pWindow->returnJS().c_str () ) << endl;
results in "string is null for now!" - Which is correct.

So this leads me to believe the problem is somewhere in the C# and how it is reading the string.

Captain Frigate
Apr 30, 2007

you cant have it, you dont have nuff teef to chew it
I hate to poo poo up the thread with my newbie nonsense again, but I have another question concerning socket operations. The code that I've been given seems to be having trouble with socket permissions, but I'm not really sure what to do about it in this case.

This:
code:
errortest = setsockopt(OpSocket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&ttl, sizeof(ttl));
does not have any problems, but this:
code:
errortest = connect(OpSocket, (SOCKADDR*)&outadd, sizeof(outadd)); 
gives me an error code of 10013, which is supposed to be a permissions error, but I'm not sure what to do about that in this case. Is this what I need to deal with? Anyone have any advice?

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.

RonniePudding posted:

Thanks for the reply Mustach. I tried your method but still get no output.
Does your C++ code look like this:
code:
const std::string& js = pWindow->returnJS();
char *result = new char[js.size()+1];
strcpy(result, js.c_str());
return result;

quote:

What's strange is that I can output the result into a log file just fine:
code:
cerr << "~passed string: " << const_cast<char*> ( pWindow->returnJS().c_str () ) << endl;
That's because the C-string is copied to cerr before it gets deleted.

RonniePudding
Oct 18, 2005

Mustach you are a god amongst men. That works perfectly!

Fifty-Nine
Oct 15, 2003

slovach posted:



what the heck is this.

I dunno who Tim is or where Visual Studio is getting this from.

It's me. I'm Tim Prince. Enjoy your assorted numbers; you're welcome.

Seriously though this freaked me out because I have the same name as the guy in the comment.

wasabimilkshake
Aug 21, 2007

North Carolina votes yes.
Dumb question, but I dig the details:

In classes where I make heavy use of STL containers, I'm creating iterators locally in a majority of those classes' methods. You can only type
code:
std::vector<Entity*>::iterator iter = mEntities.begin();
so many times before it occurs to you that you could maybe keep an iterator around as a member variable to avoid repetition, both in typing and in allocation. Is this a good idea, a bad idea, or does it make a difference?

Vanadium
Jan 8, 2005

Maybe if you do typedef std::vector<Entity*>::iterator my_iter it will be more managable, maybe you can also have a function begin() that just returns mEntities.begin(), i do not think keeping iterators around makes that much sense

OddObserver
Apr 3, 2009
Pretty much the most (only?) sensible way of implementing a std::vector<T>::iterator is as a struct/class that contains only a T*. No allocations or other heavy-duty stuff involved. If anything, putting it into a local may end up wasting memory.

.. And extending scope of variables beyond when they're needed is a bad idea. For example, if you're not careful, you might end up using the same "cached" iterator in 2 functions that may end up (perhaps indirectly) called from each other.

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
I'm trying to find a good container either in STL or boost that'll store a list of things in an order, using an operator I define, but also has some awareness of the individuality of the objects it contains. I don't think I'm being too clear so I'll just say what screwed me up tonight.

I was using a set with a compare operator that worked on one particular field of a class I wanted sorted. It turned out that if I had two objects where that one field happened to be equal, then subsequent insertions would fail. After all, a value of that key type was already in there. So I switched to a multiset. Now the problem is if I want to erase a particular object from the multiset, it will whack everything that matches that particular object's key.

So what I wanted instead was something that will sort on the key but does something like a complete equality check when it comes time for deletion. And I was hoping for a complexity better than linear, which is what got me into using sets in the first place.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Define a strict total order for your class so that two different objects aren't equal.

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!

Plorkyeran posted:

Define a strict total order for your class so that two different objects aren't equal.
I think I know what you're saying; further compare on additional attributes such that one will be less than the other unless they're basically perfect matches of each other.

I wish I had thought of that last night, but I think I was generally frustrated.

wasabimilkshake
Aug 21, 2007

North Carolina votes yes.
I'm working on a C++ project in Visual Studio 2008 which uses several external libraries. Everything works fine in debug mode, but when I try to compile for release or in MT configuration (as opposed to MD or MDd), I get a stream of linker errors and warnings that make my head spin because I know relatively little about compiling and linking to external libraries. When I run the project on another computer, I get a side-by-side configuration error.

Regardless of configuration, I always get a couple of macro redefinition warnings (APIENTRY and WINGDIAPI in glfw.h) and a warning that "defaultlib 'MSVCRTD' conflicts with use of other libs." When I set the runtime library to Multi-Threaded (non-DLL), I get linker errors complaining that a bunch of standard library functions (_malloc, _free, _memmove, _feof) are already defined in LIBCMT.lib. (Full build log)

One issue, I've been led to believe, is that some libraries have been compiled under different versions than others, or in different configurations. GLFW is a library which provides some window management and input handling functionality for OpenGL, and it also relies on opengl32 and glu32. If I download it from glfw.org, I get headers and precompiled library files. The source has a makefile, but the highest version of Visual Studio it includes is MSVC++ 2005. Is this something a need to be concerned about in attempting to fix the problems I'm having with building and linking?

Is there a workaround that I could use, or is the best course of action to identify and rectify the conflicts? From what I've researched it seems like both are a possibility, but I'm not really competent enough as it is to follow through with the latter.

More generally, does anyone know of any resources that would help me understand the mystic art of linking any better?

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Debug runtimes don't come preinstalled and aren't licensed for redistribution, so if you haven't explicitly installed them or Visual Studio on a computer you can't run debug builds which dynamically link the runtime.

Everything being linked together to form an executable needs to link against the same version of the runtime library. Switching only one thing between debug and release or static and dynamic runtime linking produces the linker errors you're seeing.

wasabimilkshake
Aug 21, 2007

North Carolina votes yes.
What determines which version of the runtime library a particular piece of object code links against?

For example, if I had been building in debug mode and I wanted to start building in release mode, would I need to recompile the libraries against which I'm linking (OpenGL, GLU, GLFW, SOIL, OpenAL, Box2D) in release mode as well?

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
In general, if you are linking against a library that you are compiling yourself you'll need to build both debug and release versions of it, and if you're linking against supplied binaries you have to hope that they're supplying binaries built in the configurations you need.

Note that if you toss all of your dependencies into the solution with your project and set your project to depend on them (rather than building them separately and explicitly passing the .lib to the linker), visual studio does 90% of the work of ensuring that you're linking against the right things for you.

functional
Feb 12, 2008

I would like to mmap a large amount of MAP_ANONYMOUS memory. I would like to page in and reserve all the memory for my process. If I cannot immediately page in all of the addresses because there is not enough memory I would would like for the command to fail gracefully in some way. What is currently happening is that the system allows the mmap to return successfully, but then later on as those addresses are accessed and it tries to make new pages available it shortsightedly runs out of reserves and segfaults. I am aware that this problem is somewhat built-in to Linux and Linux-like systems. I am looking for workarounds.

functional fucked around with this message at 03:34 on Jul 20, 2010

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

functional posted:

I would like to mmap a large amount of MAP_ANONYMOUS memory. I would like to page in and reserve all the memory for my process. If I cannot immediately page in all of the addresses because there is not enough memory I would would like for the command to fail gracefully in some way. What is currently happening is that the system allows the mmap to return successfully, but then later on as those addresses are accessed and it tries to make new pages available it shortsightedly runs out of reserves and segfaults. I am aware that this problem is somewhat built-in to Linux and Linux-like systems. I am looking for workarounds.

If you want to ensure that you have backing store available for a piece of memory, you can do so by allocating your own swap for that memory ahead of time. First, create a temporary file, and immediately unlink it from the filesystem while retaining its filehandle. truncate(2) it to the correct size, or actually write to it until it's the correct size if you want to force a ENOSPC error right away in the event of low diskspace. mmap it MAP_SHARED (you hold the only reference to this inode, so no-one else will step on your feet). This memory will be available to your process no matter what else is going on with the system, because its resources are completely private to your process. The downside, of course, is that you are no longer using the system's normal swap space, but are occupying space on some random potentially-slow filesystem instead. This problem is trivially avoided by creating the file on a tmpfs mount, which is what most systems provide for /tmp nowadays.

functional
Feb 12, 2008

ShoulderDaemon posted:

Great explanation

Wow, thanks!

Is there any way to separate memory that's "in memory" from memory that's "on the disk?" It would be helpful to restrict some allocations to fast memory so you don't have the delay of going to the disk.

wasabimilkshake
Aug 21, 2007

North Carolina votes yes.

Plorkyeran posted:

In general, if you are linking against a library that you are compiling yourself you'll need to build both debug and release versions of it, and if you're linking against supplied binaries you have to hope that they're supplying binaries built in the configurations you need.

Note that if you toss all of your dependencies into the solution with your project and set your project to depend on them (rather than building them separately and explicitly passing the .lib to the linker), visual studio does 90% of the work of ensuring that you're linking against the right things for you.
I included the MSVS projects for everything but OpenAL, and that reduced my errors to just a few that I solved with /NODEFAULTLIB:LIBCMT.lib. It compiles in Release mode and runs without issue on another computer.

Thanks a ton for the advice; you made my day.

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

functional posted:

Wow, thanks!

Is there any way to separate memory that's "in memory" from memory that's "on the disk?" It would be helpful to restrict some allocations to fast memory so you don't have the delay of going to the disk.

I am not sure what you are asking exactly but if you use the mlock family of functions you can prevent some pages from ever going to backing store, and use those as your "fast memory". If you aren't running as root, you are typically restricted to locking a very small number of pages in this manner.

hohum
Mar 17, 2010

umoms.
Since C and C++ are actually not the same language but rather loosely related standards governed by two different standards bodies: is anyone in favor of splitting this thread into a "C" megathread and a "C++" megathread?

Since it seems to be mostly C++ questions here I say "C" should be a new thread.

raminasi
Jan 25, 2005

a last drink with no ice
Are there enough C questions for its own thread?

Painless
Jan 9, 2005

Turn ons: frogs, small mammals, piles of compost
Turn offs: large birds, pitchforks
See you at the beach!
While I agree with the idea of rounding up all C programmers and placing them in re-education camps, I don't think this is the thread to start it as C questions are extremely rare.

Vanadium
Jan 8, 2005

We could deport them to the coding horror thread

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.
Most regulars to this thread know the distinctions between the two languages and even when one gets something wrong, somebody else usually corrects them. In fact, I think it's better that C and C++ share a thread so that those distinctions can be highlighted when they come up.

BigRedDot
Mar 6, 2008

Vanadium posted:

We could deport them to the coding horror thread
There, are worse things than straight C. For instance, the horrible melange that my coworkers produce is real, actual "C/C++", or as I prefer to call it: "C-".

Fecotourist
Nov 1, 2008
I may have an application for Automatic Differentiation. I'm starting to look at FADBAD++ and CppAD. Are there other packages that are clearly better? Any other wisdom?
http://www.fadbad.com/fadbad.html
http://www.coin-or.org/CppAD/

epswing
Nov 4, 2003

Soiled Meat
:hb: Warning: C++ newbie :hb:

I'm looking ahead to something we'd like to implement in our C++ desktop app in about a month. We're partnering with a GPS truck-tracking company which will be sending positional updates at regular intervals. Each update will be POSTed via HTTP from their system(s) to our app, at an address of our choosing (eg. http://the-machine-our-app-is-running-on/truckUpdate).

I see two ways of processing this HTTP request.

1) Package a lightweight web server (like BadBlue or Tntnet) within the app. When a request comes in, the app can deal with it on the fly.

2) Use some other web server (apache+mod_python, apache tomcat, etc) on the same machine to listen for requests, and dump data into an intermediate container (plaintext/json/xml files, or some RDBMS). The main app would then poll the container for new data regularly.

The data being received is not mission critical, so the delay presented in #2 is probably acceptable (poll the container every half-minute or so). What I don't know much about is what's involved in #1, and if it's considered a Bad Idea (tm) in general to expose a responsive desktop app in this way.

Any comments/suggestions?

Edit: I realize this isn't a programming question per se, I just want to flush out any obvious "WHY would you even THINK of doing this in C++ you IDIOT" points.

epswing fucked around with this message at 16:46 on Jul 21, 2010

Zerf
Dec 17, 2004

I miss you, sandman

BigRedDot posted:

There, are worse things than straight C. For instance, the horrible melange that my coworkers produce is real, actual "C/C++", or as I prefer to call it: "C-".

C-, why haven't I thought of that before? I need to remember to use that one :)

FWIW, hohum, I think a separate C thread could be interesting, granted there's enough interest to get it going...

HFX
Nov 29, 2004

Zerf posted:

C-, why haven't I thought of that before? I need to remember to use that one :)

FWIW, hohum, I think a separate C thread could be interesting, granted there's enough interest to get it going...

In my opinion, there is not enough to separate the two languages to bother with two threads. It would just add another thread I have in my bookmarks which never gets little to/no traffic. In addition, people randomly coming to ask questions would probably end up asking here since the C++ one is likely to be the first one they see.

BigRedDot
Mar 6, 2008

How about "C++ but maybe also C Programming Questions Not Worth Their Own Thread"

FWIW I think one thread is adequate, even if I do hate the sight of "C/C++"

HFX
Nov 29, 2004

BigRedDot posted:

How about "C++ but maybe also C Programming Questions Not Worth Their Own Thread"

FWIW I think one thread is adequate, even if I do hate the sight of "C/C++"

Yet most recruiters / headhunters expect to see it that way on resumes.

BigRedDot
Mar 6, 2008

HFX posted:

Yet most recruiters / headhunters expect to see it that way on resumes.

I guess? Mine has said "C and C++ on Unix platforms" for the last 15 years and I've somehow managed to get offers.

Crazy RRRussian
Mar 5, 2010

by Fistgrrl
Is there a good way to sort of do a form of reflection in C++ and find address of a method by name at run time and call it? What I would like to do is to find address of a member method of a certain class by name and call it on an instance that I already have.

I suppose I would need a cross-platform library that is able to read symbols from an executable/shared library at run time and then I would also have to deal with name mangling and virtual tables?

Bandamyion
Oct 22, 2008

Crazy RRRussian posted:

Is there a good way to sort of do a form of reflection in C++ and find address of a method by name at run time and call it? What I would like to do is to find address of a member method of a certain class by name and call it on an instance that I already have.

I suppose I would need a cross-platform library that is able to read symbols from an executable/shared library at run time and then I would also have to deal with name mangling and virtual tables?

I may be out of date, but no, there isn't. The 2 major points you bring up in the second part (symbol table format and virtual-table layout) are compiler dependant and getting C++ compiler vendors to stick to some sort of platform-independant standard has been impossible from the looks of history. The whole idea of COM and of interface programming has been the a sort of standardising the community has engineered themselves to try to overcome the problem. It works, but can be a bit messy.

Nothing as nice as .Net reflection, but then again, it may be a bit to easy to get in to some C++ functions you shouldn't be going near if C++ "reflection" was that easy :)

PS: Hello thread and fellow bit-jugglers. Nice read. Still got to go through most of the thread yet :)

Adbot
ADBOT LOVES YOU

POKEMAN SAM
Jul 8, 2004

Bandamyion posted:

I may be out of date, but no, there isn't. The 2 major points you bring up in the second part (symbol table format and virtual-table layout) are compiler dependant and getting C++ compiler vendors to stick to some sort of platform-independant standard has been impossible from the looks of history. The whole idea of COM and of interface programming has been the a sort of standardising the community has engineered themselves to try to overcome the problem. It works, but can be a bit messy.

Pro-tip since you're new to the thread, Crazy RRRussian is a fakeposter who has been around these parts for a while under different names (Hexadecimal, Entheogen, ...)

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