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
Phillyt
Dec 2, 2006

by Tiny Fistpump

floWenoL posted:

Why don't you just read the lines directly into the 2-d array?

It's a requirement to read them into a ragged array. However, once read into a ragged array, it is free game what we do with them.

Like I said, this popped up in my head that the easiest way to solve the dillemma would be to:
1) Read the strings in as a ragged array.
2) Convert the ragged array into a two dimensional char array.
3) Use the two dimensional character array with the rest of my program already written for two dimensional character arrays.

Adbot
ADBOT LOVES YOU

TheSleeper
Feb 20, 2003
Since they're just regular old null terminated c strings, there is really no reason to have a square 2d array. Can you post an example of where in your code a 2d array is 'easier'?

iSuck
Jul 22, 2005

by T. Mascis
If anyone could help me it would be immensely appreciated. I'm pretty new in playing around with C++ and MySQL databases, but I'm trying to create an application which connects to a locally hosted DB and does basic I/O. However, upon compilation, it gives me the following errors:

quote:

1>001_10Sep2009.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: char const * __thiscall sql::SQLException::getSQLState(void)const " (__imp_?getSQLState@SQLException@sql@@QBEPBDXZ) referenced in function __catch$_main$0
1>001_10Sep2009.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: int __thiscall sql::SQLException::getErrorCode(void)const " (__imp_?getErrorCode@SQLException@sql@@QBEHXZ) referenced in function __catch$_main$0
1>001_10Sep2009.obj : error LNK2019: unresolved external symbol __imp__get_driver_instance referenced in function _main

The code can be found here: http://dev.mysql.com/tech-resources/articles/mysql-connector-cpp.html

My intuition tells me the compiler simply can't find the function prototypes or the linker is having trouble in a similar vein. Also, I'm using Visual Studio 9.0.

beuges
Jul 4, 2005
fluffy bunny butterfly broomstick

iSuck posted:

If anyone could help me it would be immensely appreciated. I'm pretty new in playing around with C++ and MySQL databases, but I'm trying to create an application which connects to a locally hosted DB and does basic I/O. However, upon compilation, it gives me the following errors:


The code can be found here: http://dev.mysql.com/tech-resources/articles/mysql-connector-cpp.html

My intuition tells me the compiler simply can't find the function prototypes or the linker is having trouble in a similar vein. Also, I'm using Visual Studio 9.0.

You most likely need to add the .lib that goes with the .dll you're using to your project. Your code is calling functions in a dll, but the linker needs to have the .lib file to know how to resolve the calls to those functions.

Nomnom Cookie
Aug 30, 2009



beuges posted:

You most likely need to add the .lib that goes with the .dll you're using to your project. Your code is calling functions in a dll, but the linker needs to have the .lib file to know how to resolve the calls to those functions.
As an aside, unresolved externals are linker errors. The compiler had a prototype, no problem, and put a reference to the function in the obj. Then the linker tries to resolve all these references and can't find the function in any of the input libs or objs. A missing .lib is the most likely culprit, but outdated headers could do it too. And I have personally experienced it from an external project being compiled for the wrong libc.

iSuck
Jul 22, 2005

by T. Mascis

beuges posted:

You most likely need to add the .lib that goes with the .dll you're using to your project. Your code is calling functions in a dll, but the linker needs to have the .lib file to know how to resolve the calls to those functions.
I think you were right. This is the tutorial I used to debug my code: http://blog.ulf-wendel.de/?p=215#hello

seiken
Feb 7, 2005

hah ha ha
I'm not a programming beginner, but I've never learned the first thing about parallel programming/threads/coding for multi-core systems/all that stuff, in any language. Given that I use C++ more than any other language, where would be a good place to start?

Kimani
Dec 20, 2003

Anime avatar - check
ADTRW poster - check
Defends watching child porn - CHECK!!!

seiken posted:

I'm not a programming beginner, but I've never learned the first thing about parallel programming/threads/coding for multi-core systems/all that stuff, in any language. Given that I use C++ more than any other language, where would be a good place to start?

The Little Book of Semaphores is very, very good at presenting the problems associated with parallel programming and the tools used to tackle them. It's language agnostic and should show you all the theory.

I've done threaded stuff on Windows and I've used the critical section ( essentially a mutex ) and semaphore in the Platform SDK. Boost also has a pretty sweet barrier. The terms will make sense once you check out that book.

That's all I got for ya.

Avenging Dentist
Oct 1, 2005

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

Kimani posted:

The Little Book of Semaphores is very, very good at presenting the problems associated with parallel programming and the tools used to tackle them. It's language agnostic and should show you all the theory.

Too bad mutual exclusion is fundamentally one of the most difficult-to-maintain methods of parallelism* (and these days, one might question the need for manually handling locks when libraries/APIs like TBB and OpenMP exist). For doing "real work" with parallelism, the solution is typically MPI. Granted, this is beginning to change in the high-performance computing world, now that each compute node may has multiple cores of its own, and now that researchers are getting more interested in GPU programming for more than the "can we do it" factor.

Also, if you're using C++, you should never ever bother with the Win32 lock constructs unless you are under absolutely insane requirements (e.g. "no CRT dependencies ever"). TR1, which Microsoft has implemented and is available for VS2008, contains a fully standardized C++ threading library, which has also been incorporated into the next draft standard.

* In theory, a good static analyzer would help with this, but I'm not sure that one exists, and it's going to take clang a few years before it can even compile C++ well, let alone statically analyze it.

seiken
Feb 7, 2005

hah ha ha

Kimani posted:

The Little Book of Semaphores is very, very good at presenting the problems associated with parallel programming and the tools used to tackle them. It's language agnostic and should show you all the theory.

I've done threaded stuff on Windows and I've used the critical section ( essentially a mutex ) and semaphore in the Platform SDK. Boost also has a pretty sweet barrier. The terms will make sense once you check out that book.

That's all I got for ya.

Sweet, after reading a couple chapters of that book I understood enough to use the Platform SDK implementations to get my video rendering thingy to do different frames at the same time on different CPUs. (An incredibly simple problem to solve, as it turns out, but it was what I had my sights set on originally.) Thanks, guy!

Zakalwe
May 12, 2002

Wanted For:
  • Terrorism
  • Kidnapping
  • Poor Taste
  • Unlawful Carnal Gopher Knowledge
I've used helgrind, a part of the excellent valgrind suite to help track down locking problems before.

I personally use pthreads for the heavy duty work and OpenMP for simple loop constructs. OpenMP is beginning to get usable now that tasks and recursion can be implemented.

iSuck
Jul 22, 2005

by T. Mascis
I'm using code from this example and having a new issue. Every time the code goes through this section:
code:
		/* insert couple of rows of data into City table using Prepared Statements */
		prep_stmt = con -> prepareStatement ("INSERT INTO city (CityName) VALUES (?)");

		cout << "\tInserting \"London, UK\" into the table, City .." << endl;
		
		prep_stmt -> setString (1, "London, UK");
		updatecount = prep_stmt -> executeUpdate();

		cout << "\tCreating a save point \"SAVEPT1\" .." << endl;
		savept = con -> setSavepoint ("SAVEPT1");

		cout << "\tInserting \"Paris, France\" into the table, City .." << endl;

		//prep_stmt -> setString (1, "Paris, France");
		//updatecount = prep_stmt -> executeUpdate();

		cout << "\tRolling back until the last save point \"SAVEPT1\" .." << endl;
		con -> rollback (savept);
		con -> releaseSavepoint (savept);

		cout << "\tCommitting outstanding updates to the database .." << endl;
		con -> commit();
It writes zeroes to the database table instead of the values "London, UK" and "Paris, France". Why am I unable to execute these queries? Now the database connection seems fine as I'm able to read column data from different tables and schema. Also, the MetaData comes out like the following section, so I don't think it has any write restrictions:
code:
Database Metadata
-----------------
Database Product Name: MySQL
Database Product Version: 5.1.24-rc-standard
Database User Name: root@localhost

Driver name: MySQL Connector/C++
Driver version: 1.0.5

Database in Read-Only Mode?: false
Supports Transactions?: true
Supports DML Transactions only?: false
Supports Batch Updates?: true
Supports Outer Joins?: true
Supports Multiple Transactions?: true
Supports Named Parameters?: false
Supports Statement Pooling?: false
Supports Stored Procedures?: true
Supports Union?: true

Maximum Connections: 151
Maximum Columns per Table: 512
Maximum Columns per Index: 16
Maximum Row Size per Table: 2147483639 bytes

iSuck fucked around with this message at 04:14 on Sep 15, 2009

DoctorTristan
Mar 11, 2006

I would look up into your lifeless eyes and wave, like this. Can you and your associates arrange that for me, Mr. Morden?
I'm getting something slightly odd with my STL containers. The problem occurs in the following function call

code:
equalSkeletons(**It, principalSkel);
The function definition is:
code:
bool equalSkeletons(const EA2Skeleton& left, const EA2Skeleton& right){

        return = std::equal((left.segmentStore).begin(), (left.segmentStore).end(), (right.segmentStore).begin() );
    }
The arguments **It, principalSkel are both of type EA2Skeleton:
code:
class EA2Skeleton {
 
            unsigned int maxlength;
        public:
            std::vector<EA2SkeletonSegment*> segmentStore;            
            EA2Skeleton ( int length );
            EA2Skeleton ( const EA2Skeleton& skel );
            ~EA2Skeleton(); 
            void push_back ( EA2SkeletonSegment* segArg );
	    int num_segs();
	    int num_points();
        };
This is a container class for pointers to type EA2SkeletonSegment: a class I've defined elsewhere. Yes, I know I'm storing them as public members.

What the function call is supposed to do is compare two EA2Skeletons by comparing the EA2SkeletonSegments they point to - if two EA2Skeletons point to the the same objects then they're considered equal.

The debugger is telling me that I'm dereferencing a past-the-end iterator during the evaluation (not sure if that's the right word) of the second argument to the function. This didn't happen in my testing, and I'm not sure why it's happening or how to fix it. Any thoughts?

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Why would you not check the length first? Aside from avoiding potential crashes, it'd be faster in most cases.

showdown
Jul 10, 2001

Smellrose
Quick question for anyone who uses scope guards:

If you declare a resource release before you obtain it, is it optimized out in release builds?
In other words does:

code:
HANDLE h = INVALID_HANDLE_VALUE;
ON_BLOCK_EXIT( ::ReleaseHandle, h );

h = //whatever gethandle function
not work? The guard here never executes but other guards that I use work.

I don't have the exact implementation but I'm sure it's pretty similar to Loki or the modified version in the boost archive.

I won't have the code again til I'm back in in a few days so I can't test a debug build.

DoctorTristan
Mar 11, 2006

I would look up into your lifeless eyes and wave, like this. Can you and your associates arrange that for me, Mr. Morden?

Avenging Dentist posted:

Why would you not check the length first? Aside from avoiding potential crashes, it'd be faster in most cases.

If the surrounding code is working properly then they should both be the same length already, but I'll stick an assert in and see if that catches anything.

Edit: Caught it! Messed up a conditional statement earlier on, one of the arguments was of size zero. I need to be more assertive.

DoctorTristan fucked around with this message at 09:46 on Sep 15, 2009

Avenging Dentist
Oct 1, 2005

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

showdown posted:

Quick question for anyone who uses scope guards:

How would we know? In addition to not telling us your compiler, you haven't even shown us the implementation of the scope guard you're using. If you want to find out, make a reduced test case and check the assembler output.

The Red Baron
Jan 10, 2005

Another question is why you're not just using RAII instead, since it'd remove the need for any explicit scope resource management

Avenging Dentist
Oct 1, 2005

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

The Red Baron posted:

Another question is why you're not just using RAII instead, since it'd remove the need for any explicit scope resource management

Scope guards are RAII for types you don't control.

showdown
Jul 10, 2001

Smellrose

Avenging Dentist posted:

How would we know? In addition to not telling us your compiler, you haven't even shown us the implementation of the scope guard you're using. If you want to find out, make a reduced test case and check the assembler output.

Oops. I guess it was wrong to assume everyone on the planet uses Visual Studio :toot:

I did say it was pretty much Loki SG, though

Good idea to check assembly, though, I tested it with Loki and it is in fact optimized to a no-op in release and runs correctly debug

The Red Baron
Jan 10, 2005

Avenging Dentist posted:

Scope guards are RAII for types you don't control.

I meant in the sense of encapsulating the RAII logic in a auto_handle class rather than having to use a scope guard each time

MagneticWombats
Aug 19, 2004
JUMP!
Is there a one stop shop (book/article/something) for RAII best practices? Because as soon as I feel like I know all there is to know about it, something keeps popping up.

HondaCivet
Oct 16, 2005

And then it falls
And then I fall
And then I know


Does anyone know anything about Boost.Asio? I am still working with a simple client/server socket pair from Boost's website. I've been asked to break everything up into classes so that they can be integrated into another program. However, I'm a bit stumped on how to deal with the client half of things. The "main" class needs two arguments to work. I can't figure out where they are coming from though. There isn't any obvious way that they are passed by the server half. The whole thing just works on its own when you keep them in two separate programs. What am I missing here?

Commander Keen
Oct 6, 2003

The doomed voyage of the Obsessivo-Compulsivo will haunt me forever

HondaCivet posted:

Does anyone know anything about Boost.Asio? I am still working with a simple client/server socket pair from Boost's website. I've been asked to break everything up into classes so that they can be integrated into another program. However, I'm a bit stumped on how to deal with the client half of things. The "main" class needs two arguments to work. I can't figure out where they are coming from though. There isn't any obvious way that they are passed by the server half. The whole thing just works on its own when you keep them in two separate programs. What am I missing here?

C++ command line arguments

HondaCivet
Oct 16, 2005

And then it falls
And then I fall
And then I know


Commander Keen posted:

C++ command line arguments

Cool, had no idea about those. I'm still new to programming. Thanks!

Fat Lou
Jan 21, 2008

Desert Heat? I thought it was Dessert Heat. No wonder it tastes so bad.

I am a dumb newbie when it comes to C++ and I must be looking in the completely wrong places, but I just cannot figure out how to input a txt of varying integers into a simple array in C++. I figured out how to have the program ask you the number of integers being inputted, but I need to have the program to work if the user does not know the actual number of lines in the file. The txt file would look similar to this...

1
4
2
5
7
3
5

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
code:
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
using namespace std;

int main()
{
    vector<int> v;
    ifstream file("file.txt");
    copy(istream_iterator<int>(file), istream_iterator<int>(),
         back_inserter(v));

    for(size_t i=0; i<v.size(); ++i)
        cout << "v[" << i << "] = " << v[i] << endl;
}

Fat Lou
Jan 21, 2008

Desert Heat? I thought it was Dessert Heat. No wonder it tastes so bad.

Thank you.

Kreez
Oct 18, 2003

Why does this:
code:
int main()
{
	double var=123.456;
	scanf_s("%f", &var);
	printf("%.3f\n", var);
	return 0;
}
result in this: (45 being an input)

code:
45
123.456
It works fine with %d, what am I missing?

I've been getting back into basic programming in something other than Matlab for the first time since first year undergrad, and it was going great until I needed to input floating point numbers.

I'm using Visual C++ 2008 Express (compiling as C) if that makes any difference.

If someone could point out what I'm missing I'd very much appreciate it!

Scaevolus
Apr 16, 2007

Kreez posted:

%f means float, not double. (use %lf)

Avenging Dentist
Oct 1, 2005

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

Scaevolus posted:

%f means float, not double. (use %lf)

EDIT: Oh, you mean scanf.

Kreez
Oct 18, 2003

Cool, %lf works great, thanks.

Odd that something basic like that would be left out of even the basic C syntax overview that exists in my textbook.

Scaevolus
Apr 16, 2007

Kreez posted:

Cool, %lf works great, thanks.

Odd that something basic like that would be left out of even the basic C syntax overview that exists in my textbook.
K&R is way better than your textbook, please read it.

w00tz0r
Aug 10, 2006

I'm just so god damn happy.
Posting this in here, since I'm using C and I didn't see a thread on win32.

I'm taking an intro to win32 course in college, and I can't figure out what the purpose of the cbSize member is in a struct.

I googled it, and there were a couple of suggestions like "used for different versions of a struct", and "used for a struct with variable length data", but I'm not sure of the official reason, though the second one seems to make a bit more sense to me.

Avenging Dentist
Oct 1, 2005

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

w00tz0r posted:

I googled it, and there were a couple of suggestions like "used for different versions of a struct", and "used for a struct with variable length data", but I'm not sure of the official reason, though the second one seems to make a bit more sense to me.

It's more the former than the latter. As new versions of Windows are released, Microsoft adds new members to structs, and the cbSize parameter helps them figure out which version of the API your binary uses for that particular function. That way they don't go accessing unallocated memory when trying to query the new members and potentially segfaulting or doing something completely random*.

* Not completely random.

w00tz0r
Aug 10, 2006

I'm just so god damn happy.

Avenging Dentist posted:

It's more the former than the latter. As new versions of Windows are released, Microsoft adds new members to structs, and the cbSize parameter helps them figure out which version of the API your binary uses for that particular function. That way they don't go accessing unallocated memory when trying to query the new members and potentially segfaulting or doing something completely random*.

* Not completely random.

Awesome, thanks. Our instructor mentioned it in class, and then asked us why we thought Microsoft did it.

This was followed by about 5 minutes of awkward silence and wrong answers as he waited for us to tell him, then he told us to find out on our own time.

FearIt
Mar 11, 2007
Hi CoC,

I have a C++ question specifically tied to the NetBeans IDE.
Is there a way to use my source files from one project in another project?
I do not wish to have multiple copies of the same file in several location, god forbid I need to change one thing from a single class, and now have to change it in every project that used it.

I've tried playing around with the project settings myself, and google does not seem to give me exactly what I'm looking for.

Any ideas goons?

Thank you,
FearIt

RussianManiac
Dec 27, 2005

by Ozmaugh

FearIt posted:

Hi CoC,

I have a C++ question specifically tied to the NetBeans IDE.
Is there a way to use my source files from one project in another project?
I do not wish to have multiple copies of the same file in several location, god forbid I need to change one thing from a single class, and now have to change it in every project that used it.

I've tried playing around with the project settings myself, and google does not seem to give me exactly what I'm looking for.

Any ideas goons?

Thank you,
FearIt

do what you would do in any other language - make a library out of your project that you want to reuse in another project. Of course you have to recompile the library every time you make changes to the source code.

Bruegels Fuckbooks
Sep 14, 2004

Now, listen - I know the two of you are very different from each other in a lot of ways, but you have to understand that as far as Grandpa's concerned, you're both pieces of shit! Yeah. I can prove it mathematically.
This is more rhetorical, but does dealing with ansi/unicode (and the various kinds of strings and poo poo in general) in C++ make anyone else want to cut their own dick off? My god I hate it so much.

Adbot
ADBOT LOVES YOU

HondaCivet
Oct 16, 2005

And then it falls
And then I fall
And then I know


Argh, I'm getting this weird nasty runtime error and I can't imagine what would be causing it. When I try to run my program, I get this scary window that says "Debug Assertion Failed!" and a bunch of stuff. The problem is in TimingTest.exe (something in Windows I assume) and it is mentioning an invalid null pointer. What sorts of things should I be looking for to figure this error out?

I'm pretty sure it's in here. (Warning: Boost.Asio ahead)

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