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
tadashi
Feb 20, 2006

Mustach posted:

You shouldn't have to do anything special for the iostream header to be found. Which specific project type did you select? Some of the options create projects targeting .NET's C++/CLI instead of native C++, but I'm not sure that they require anything special, either.

It looks like the problem was I as allowing VS 2010 to use pre-compiled headers. There is a checkbox for pre-compiled headers that can be unchecked during the project creation wizard for Win32 console applications....

Adbot
ADBOT LOVES YOU

ehnus
Apr 16, 2003

Now you're thinking with portals!
Electronic Arts released a bunch of code in the past week over at http://gpl.ea.com, including modifications they've made to Webkit.

More interestingly though is that EASTL has been released as part of this package, which someone has already extracted and put up on Github: http://github.com/paulhodge/EASTL

ToxicFrog
Apr 26, 2008


pliable posted:

1. No no, I've known this for years. C was the first language I've ever learned. I'm just coming back to it now after a long rear end hiatus.

2. I ask about this because I made a rookie mistake thinking that the compiler at runtime will automatically allocate space for a string literal, and then you can work with it...which I then learned is a big no no.

The compiler will automatically allocate space for the string literal, but you have no guarantee that that space will be writeable; most compilers will place it in the read-only initialized data segment, or local equivalent. You should consider string literals to be immutable.

quote:

3. I'll re-read those. Maybe something isn't just clicking when working with them, and they're a lot easier than I think they are.

No, string manipulation in C is pretty much unpleasant full stop.

That said, c++.com has a nice index of <string.h> functions, which you may find useful.

quote:

Vino: Knew about that stuff, one thing I was looking for was an easy way to manage them. Looks like I'm going to have to tap into my ASM knowledge and re-learn how to manually manage memory again! And yeah, I have to work with the ANSI standard C library. Can't use any other fancy libraries out there :(.

:confused: Where does asm come into this? If you have access to all of ANSI C, you have malloc/calloc/realloc to allocate memory and realloc/free to release it - these are all in <stdlib.h>.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

pseudorandom name posted:

pipe(7)

I'm not sure what exactly you're doing, but guessing as to what you're assignment actually is instead of what you've described, I'd say you only need two pipes (one for orders to the threads, one for replies from the threads to the master thread).

I think I'm in the same class as that guy lol.

So far I've been able to get it to do this:
code:
 creating process 2315 - 0
 creating process 2319 - 1
Seats available: 36
 creating process 2318 - 1
Seats available: 36
 creating process 2320 - 1
Seats available: 36
 creating process 2321 - 1
Seats available: 36
 creating process 2317 - 1
Seats available: 36
 creating process 2316 - 1
read: 36
Seats available: 36
read: 36
read: 36
read: 36
read: 36
read: 36
0: 36
1: 36
2: 36
3: 36
4: 36
5: 36
Can someone here give an example of how to use, I'm assuming, READ/WRITE to act as a mutex for a shared variable?

edit:

My function for the forks looks like this:

code:
        close(fd[child_idx][0]);
        char buf[BUFSIZE];
        
        int seats_open = seats_available; // originally *seats_open = &seats_available
        seats_open--; // originally *seats_open = *seats_open - 1;
        snprintf(buf, BUFSIZE, "%ld", (long int)seats_open);
        
        if(sizeof(buf) != write(fd[child_idx][1], buf, sizeof(buf)))
            exit(-3);
            
        std::cout << "Seats available: " << buf << std::endl;
        close(fd[child_idx][1]);
It doesn't seem to be updating the value of seats_available

Acer Pilot fucked around with this message at 15:53 on Oct 19, 2010

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

drcru posted:

It doesn't seem to be updating the value of seats_available
Where do you expect it to update that value? It copies it into "seats_open" and then only updates seats_open.

The "originally" method in the comments is closer to actually updating the value.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

roomforthetuna posted:

Where do you expect it to update that value? It copies it into "seats_open" and then only updates seats_open.

The "originally" method in the comments is closer to actually updating the value.

I changed it back to how it was originally now but it still only seems to deduct it from seats_available once... Any ideas on what I did wrong?

roomforthetuna
Mar 22, 2005

I don't need to know anything about virii! My CUSTOM PROGRAM keeps me protected! It's not like they'll try to come in through the Internet or something!

drcru posted:

I changed it back to how it was originally now but it still only seems to deduct it from seats_available once... Any ideas on what I did wrong?
Is seats_available a global variable? Are all the processes using the same one? If I was debugging this I'd try outputting the address alongside the value. (I know the answer to this, but how to figure it out is a more important lesson than the answer.)

Though even if it was working as you'd expect, you'd still need some sort of locking anyway, or you risk all 6 processes first reducing the number, then the same reduced number (30) being piped 6 times.

Here is someone talking about how to do shared data between forked processes. If you can do it with posix threads, that'd probably be better suited to your task, really, since they share all the memory already so you'd only need to deal with the locking. But seriously, first do the printing information trick to find out what's going wrong with your version, don't just blindly do it someone else's way.

Acer Pilot
Feb 17, 2007
put the 'the' in therapist

:dukedog:

roomforthetuna posted:

Though even if it was working as you'd expect, you'd still need some sort of locking anyway, or you risk all 6 processes first reducing the number, then the same reduced number (30) being piped 6 times.


This is exactly what's happening now, all 6 processes are reducing the number 6 times. Yikes!

Thanks, I'll look through the memory locations and that link.

I already did it via POSIX threads but our prof also wanted us to try it out with pipes or sockets. The pthreads seemed pretty easy but I'd never worked with pipes before :(.

HFX
Nov 29, 2004

drcru posted:

This is exactly what's happening now, all 6 processes are reducing the number 6 times. Yikes!

Thanks, I'll look through the memory locations and that link.

I already did it via POSIX threads but our prof also wanted us to try it out with pipes or sockets. The pthreads seemed pretty easy but I'd never worked with pipes before :(.

Pipes are a pain in the arse. However, they once were the only way to do things.

One way to do this is to have a process which actually handles the counts for all the rest. The child processes can then ask that processes to decrease its variable and report back the new value. That process could then send a message letting all other processes know what the new value is along with a request and sequence number that the value pertains too. The 6 children can now update any local references (if they care) with the new value.

This also works for unix sockets and network sockets. That above design makes a lot more sense once you start thinking distributed systems where things can happen asynchronously.

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
Has anybody here using emacs found a good way to go to the definition of some source code? All I can find online at best is "I don't use such-and-such but I would imagine it has it."

shrughes
Oct 11, 2008

(call/cc call/cc)

Rocko Bonaparte posted:

Has anybody here using emacs found a good way to go to the definition of some source code? All I can find online at best is "I don't use such-and-such but I would imagine it has it."

http://www.google.com/search?q=emacs+tags

Menacer
Nov 25, 2000
Failed Sega Accessory Ahoy!
I'm trying to debug some C++ code that I didn't write. I found a problem, found where the problem is happening, but I don't know how to fix it. Help me please!

Basically, a copy constructor isn't being called, and I believe the implicit copy constructor is being called instead. This is causing problems.

The first bit of code is an array wrapper constructor. We get to here just fine:
code:
template<class A> class ArrayWrapper
{
public:
    A& array;
    const int off;
    const int n;

    ArrayWrapper(A& input, const Vector<int>& range)
    :array(input), off(range.x), n(range.y-range.x)
    {}
};
I believe the array(input) should call the following copy constructor, right?

code:
template<class T> class Array
{
public:
    int end;
    T* base;
    
    Array(const Array<T>& input) 
        :end(input.end)
    {
        T* array=new T[end];
        base = array;
        T* old=input.base;
        for (int i=0; i < end; i++)
            array[i]=old[i];
    }
};
If I simply use 'Array<TYPE> new_array(old_array);' I hit the above copy constructor. However, if I do 'ArrayWrapper<Array<TYPE> > ArrayWrapper(old_array, some_vector);', I will get to the ArrayWrapper constructor, but it will not enter the Array copy constructor.

Any ideas?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Menacer posted:

Basically, a copy constructor isn't being called, and I believe the implicit copy constructor is being called instead. This is causing problems.

There's no copying going on here. If you initialize a reference with another reference, it just binds to the original referenced object. So the object referenced by array is the same object referenced by input.

Initializing a reference never involves an implicit copy. †

† To be precise, you can bind const references (or in MSVC, any reference) to temporaries, and those temporaries can be the result of a user-defined conversion, and maybe that can involve an implicit copy. But not if the source and target types are related at all.

rjmccall fucked around with this message at 09:45 on Oct 21, 2010

Menacer
Nov 25, 2000
Failed Sega Accessory Ahoy!
Oh man, now I feel all kinds of :downs:

Thanks for your help with that!

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!
As terse as it was, that was what I needed. I haven't used tags in emacs, but from that I was able to figure out what cedet is supposed to have to do it. I guess Semantic has a "smart jump" command that'll do what I need, but I'll have to see tonight.

ReaperUnreal
Feb 21, 2007
Trogdor is King
Don't really know where else to ask, but I'm looking for a very specific way to return a pair of items from a C function.

I'm quite aware that I could use a struct, or an array, or just a pointer to some spare memory, but that's not what I'm looking for. A long time ago, I read a blog post or forum post on using some inline assembly and the __naked directive to have some reasonable syntax in C for returning a pair of items.

Google isn't turning up anything in the first 10 pages, and the people on StackOverflow are saying the question is too localized. Does anyone remember this or possibly even still have a link lying around somewhere?

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

ReaperUnreal posted:

Don't really know where else to ask, but I'm looking for a very specific way to return a pair of items from a C function.

I'm quite aware that I could use a struct, or an array, or just a pointer to some spare memory, but that's not what I'm looking for. A long time ago, I read a blog post or forum post on using some inline assembly and the __naked directive to have some reasonable syntax in C for returning a pair of items.

Google isn't turning up anything in the first 10 pages, and the people on StackOverflow are saying the question is too localized. Does anyone remember this or possibly even still have a link lying around somewhere?

You've got some X/Y, buddy

ReaperUnreal
Feb 21, 2007
Trogdor is King

Otto Skorzeny posted:

You've got some X/Y, buddy

No, I'm quite aware that it's a terrible way to return values. I want to see that article again just so I can marvel at that implementation again. It's also what introduced me to calling conventions and the __naked directive.

It's also a great reference on how to program like a dick (which is good fun in moderation).

functional
Feb 12, 2008

(re: X/Y accusation)

ReaperUnreal posted:

No, I'm quite aware that it's a terrible way to return values...

Pet peeve of mine, besides the fact that programmers are socially deaf (Otto's cool tho as are most people here). Every single time I ask a programming question I get somebody giving me Y' for X when I really want Y. I am a flipping good programmer, I understand the X/Y problem. If I wanted to ask about just X I would ask for any solution to X. Usually I'm asking in the first place because X is an easy problem to solve, I already know how to do X, and I need Y for a specific reason (maybe just curiosity). A lot of question answerers have the dang Y' problem.

functional fucked around with this message at 23:54 on Oct 21, 2010

functional
Feb 12, 2008

Anyway, I also want to know the answer to ReaperUnreals question, because it's interesting, even though I would just use a pointer.

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


More of a Sun Studio specific question, I suppose, but you're the best hope I got. What sort of things could cause a program to leak memory (badly) when compiled optimized (in the case of Sun Studio, -xO3) but to leak nothing at all when compiled in debug (-g)? I've run Rational PUrify against the thing in both optimized and unoptimized form, but it can't find a thing, and visual scanning of code is proving fruitless, so I don't even know where to start here. :smith:

pseudorandom name
May 6, 2007

Are you sure it is actually leaking memory, and not holding on to memory for other reasons?

IIRC, libumem (their high-performance malloc) uses huge amounts of address space.

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


pseudorandom name posted:

Are you sure it is actually leaking memory, and not holding on to memory for other reasons?

IIRC, libumem (their high-performance malloc) uses huge amounts of address space.

I can't see how it isn't a leak, especially considering it'll get up to 12GB used in an hour while optimized but stay at around 100-120MB in debug (I just watched this happen). :pwn:

I can sort of watch the leak happen if I compile optimized with debug symbols (-g -xO3), and know what piece of the code I can step across to cause a small leak, but the debugger is unreliable for single stepping, so it doesn't help me much.

(Hadn't thought of umem though. We're not using it, but I could try preloading umem as a flailing-in-the-dark-will-this-work option. I guess.)

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!

functional posted:

(re: X/Y accusation)


Pet peeve of mine, besides the fact that programmers are socially deaf (Otto's cool tho as are most people here). Every single time I ask a programming question I get somebody giving me Y' for X when I really want Y. I am a flipping good programmer, I understand the X/Y problem. If I wanted to ask about just X I would ask for any solution to X. Usually I'm asking in the first place because X is an easy problem to solve, I already know how to do X, and I need Y for a specific reason (maybe just curiosity). A lot of question answerers have the dang Y' problem.
It's the first time I heard of the concept but I've seen it all over the place too. It seems to apply to all matter of technical things. Often there is some interesting--albeit obscure--lessons to be learned about something in trying to do Y.

Updating my thread to ask about my thought process on a memory problem I am having. I am getting nailed with double frees on some stuff, and here I thought I'd be okay with smart pointers! What I think is going on is something like this:

1. Constructor stores a pointer in a smart pointer to a very specific type of object, say class X which inherits from class W.
2. Constructor then stores pointer in a smart pointer that is use a more generic type of an object, say class W.
3. Destructor gets called and smart pointer of for object X gets blown away.
4. The more generic smart pointer then tries to similarly free object X.

I am assuming since technically the smart pointers where referring to two different types, they assumed them to be unique. Is there a good way to reestablish this relationship with Boost's smart pointers?

Rocko Bonaparte fucked around with this message at 07:26 on Oct 22, 2010

ToxicFrog
Apr 26, 2008


functional posted:

A lot of question answerers have the dang Y' problem.

Probably because a lot of question askers have the X/Y problem. Answering with Y' means you'll be right a lot more often than you are wrong.

I see this a lot where people are new to a language/system/library/etc and haven't bothered to learn much about it, so they run into a problem, think of how they would solve it in some other context they're comfortable with, formulate a (deeply flawed) solution based on that, and then ask for help implementing the solution without ever mentioning the problem they're trying to solve unless you drag it out of them:

:v: Hey, what would be the best way to keep track of the number of references to an object?
:eng101: Well, that depends on what sorts of object and why you need to keep track of the references.
...twenty minutes of increasingly frustrating discussion ensue...
:v: But I need reference counting to know when the object is about to be garbage collected, so that I can clean it up!
:eng99: Just register a finalizer for the object and the language will call it automatically just before it's collected.
:v: Oh, you can do that? Why didn't you say so in the first place, now I've wasted all this time!
:eng99: :suicide:

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.

Rocko Bonaparte posted:

Updating my thread to ask about my thought process on a memory problem I am having. I am getting nailed with double frees on some stuff, and here I thought I'd be okay with smart pointers! What I think is going on is something like this:

1. Constructor stores a pointer in a smart pointer to a very specific type of object, say class X which inherits from class W.
2. Constructor then stores pointer in a smart pointer that is use a more generic type of an object, say class W.
3. Destructor gets called and smart pointer of for object X gets blown away.
4. The more generic smart pointer then tries to similarly free object X.

I am assuming since technically the smart pointers where referring to two different types, they assumed them to be unique. Is there a good way to reestablish this relationship with Boost's smart pointers?
If you're doing this:
code:
X *raw = whatever;
shared_ptr<X> xp (raw);
shared_ptr<W> wp (raw);
You're going to get double deletes. That constructor assumes that it's getting an unshared pointer. You have to create a shared_ptr and then construct the other shared_ptr from it:
code:
shared_ptr<X> xp (raw);
shared_ptr<W> wp (xp);
This constructor is defined as long as the X* -> W* conversion is defined.

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


Dumb linking question in C++. Say I have a static (or dynamic) library compiled with C++ compiler X. Now say I have a main program that wants to link that library, except its object files are being compiled with C++ compiler Y. Is doing this link generally possible?

If not, what can I do short of recompiling the main program with compiler X (or getting the vendor to recompile my libraries with compiler Y, but hahhaha :smith: )

(In this specific case, X is Sun CC 12.1 and Y is GNU G++ 3.4.2 all on a solaris box of... some kind I honestly can't remember. But I'd rather know in the more general sense if there's a more general answer. :))

Ciaphas fucked around with this message at 04:29 on Oct 23, 2010

pseudorandom name
May 6, 2007

GCC and SunCC don't use the same C++ ABI, so, no, that won't be possible.

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


drat and five thousand blasts. I hate sun CC so much it is so phenomenally buggy :cry:

(Far be it from me to look stupider in here, but... ABI?)

OddObserver
Apr 3, 2009
Frequently impossible.

More specifically, it depends on whether they use the same C++ ABI (Application Binary Interface --- which specifies how function names are encoded and layout of things like objects and vtables). For a long time basically everyone's scheme was different --- even different gcc versions were incompatible. There are perhaps a few more compatibilities now: both g++ >= 3.2 and some other Unix-friendly compilers like Intel's use the same ABI that was cooperatively designed for Itanium, but there is still a lot more that's different than that's same.

(Also, I am not 100% sure of how the runtime library stuff is supposed to work when mixing compilers...)

It -seems- that Sun CC is incompatible, but luckily the difference appears to be huge --- you'll likely just get tons of link errors if you try.

Edit: if you want to know more about how an ABI looks, here is what g++ uses:
http://www.codesourcery.com/public/cxx-abi/abi.html

There is also of course an ABI for C that this builds on. Those are usually pretty much compatible since they're much simpler, but
sometimes problems still crop in with stuff like SSE and whatnot.

OddObserver fucked around with this message at 04:50 on Oct 23, 2010

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


Yeah, while I was at work I was trying to do this link--it kept throwing up undefined symbols even though I KNOW I got all the link arguments in the right order. That's when I realized I should ask the question I asked here.

I guess if the two compilers don't even name-mangle the same, well, that'd explain that.

Thanks folks :unsmith:

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
SunCC and GCC may not agree on the C++ ABI, but they should still agree on the C ABI, which you can embed within C++ using extern "C" declarations. So it's still possible to make the libraries interoperate if you can stand to go through a C interface.

Of course, that means each library has to treat pointers from the other as totally opaque, but that might be workable.

pliable
Sep 26, 2003

this is what u get for "180 x 180 avatars"

this is what u fucking get u bithc
Fun Shoe
How the gently caress do you read in binary data as a character? I'm not sure how to fully explain this, but I'm writing a huffman encoding program. I can be given binary or character data. My program works fine with character data, but when it comes to binary, it fails to render correct encoding. I'm reading in my data like this:

code:
    while((numBytes = read(fd, buffer, BUFFSIZE)) > 0) {
 
       for(i = 0; i < numBytes; i++) {
          byte = (buffer[i] & 0xFF);
          counts[(int)byte]++;
          /*
          counts[(int)buffer[i]]++;
          */
       }
    }
The bitwise and is my attempt to convert that binary data, as suggested by a friend. Any help would be appreciated, thanks!

ToxicFrog posted:

No, string manipulation in C is pretty much unpleasant full stop.

That said, c++.com has a nice index of <string.h> functions, which you may find useful.

Bookmarked, thank you :)

MutantBlue
Jun 8, 2001

pliable posted:

How the gently caress do you read in binary data as a character?

Is the buffer array defined as unsigned char?

Rocko Bonaparte
Mar 12, 2002

Every day is Friday!

Mustach posted:

code:
shared_ptr<X> xp (raw);
shared_ptr<W> wp (xp);
This constructor is defined as long as the X* -> W* conversion is defined.
I hadn't known that was possible. The compiler took it and my double deletes went away. It is also a lot easier to read on my code. Before I was writing a whole line's worth of stuff just to pull the raw pointer out, but this pretty much struck that all away.

tripwire
Nov 19, 2004

        ghost flow

ehnus posted:

Electronic Arts released a bunch of code in the past week over at http://gpl.ea.com, including modifications they've made to Webkit.

More interestingly though is that EASTL has been released as part of this package, which someone has already extracted and put up on Github: http://github.com/paulhodge/EASTL

Oooooooooooooooo

Has anyone tried getting a fairly performance intensive project and comparing performance of EASTL vs std STL?

pliable
Sep 26, 2003

this is what u get for "180 x 180 avatars"

this is what u fucking get u bithc
Fun Shoe

MutantBlue posted:

Is the buffer array defined as unsigned char?

This fixed my problem...thank you very much!

MutantBlue
Jun 8, 2001

pliable posted:

This fixed my problem...thank you very much!

Glad to help, you were getting char values in the range -128..127 which are obviously bad indices for your counts[] array.

Did you take out the junk that looked like

code:
byte = (buffer[i] & 0xFF);
counts[(int)byte]++;
and just use

code:
counts[buffer[i]]++;

Ciaphas
Nov 20, 2005

> BEWARE, COWARD :ovr:


This leak that's only appearing when I compile optimized and doesn't show up in Purify has me at my wit's end.

Does anyone have any ideas on how I might be able to track the total memory usage of particular objects, or even all objects of a given class? I get the feeling sizeof() wouldn't be the right answer here, but I'm basically flailing in the loving dark now. :(

(edit) I've tried overloading global new and delete, with #defines to let me get __FILE__ and __LINE__, but one of the libraries I have to use reeeally dislikes that (stupid placement new...).

(edit) I have a sudden suspicion that it might be an uninitialized member variable that's doing it. Debug compiles typically set those to 0 or blank string or whatever on declaration, whereas if you don't initialize them a release compile typically does jack and/or poo poo, right? Is there any non-compiler-specific way to check if any variable initializations are being forgotten?

(edit again) Oh, sorry, I forgot. I'm on Solaris, using Sun CC 12.1. So no Valgrind :(

(edit a third time) Man I wish I could use a linux livecd or had a linux box, solaris makes me so cross :mad:

Ciaphas fucked around with this message at 04:24 on Oct 24, 2010

Adbot
ADBOT LOVES YOU

OddObserver
Apr 3, 2009
If you're on Linux or OS X use valgrind's Massif tool.

Edit: and for non-initialized variables, its default Memcheck tool. It's also generally not true that debug builds do any special initialization
(though some debug version of malloc fill fresh memory with known patterns).

Edit #2: It might honestly be worth the effort to try building it under Linux to just run Valgrind. There is some chance it wouldn't be much work,
and if you don't have a Linux system it's easy enough to just use a LiveCD.

OddObserver fucked around with this message at 04:12 on Oct 24, 2010

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