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
fret logic
Mar 8, 2005
roffle

Ari posted:

But why are you learning both C and C++ at once?

Heh, because I can't stop myself from wanting to mess with one or the other. So far it hasn't really been a big deal, but if it gets to the point where I can't separate the two, I'll stick with one.

Adbot
ADBOT LOVES YOU

more falafel please
Feb 26, 2005

forums poster

Ari posted:

The question comes up pretty often as to the de facto library of books for learning various programming languages and concepts. In my experience reading threads discussing them and reading several of the books named, I think the general goon consensus is that Eckel's Thinking in C++ is the best one to pick up C++. But why are you learning both C and C++ at once?

Also Koenig's Accelerated C++: http://www.amazon.com/Accelerated-Practical-Programming-Example-Depth/dp/020170353X

Professor Science
Mar 8, 2006
diplodocus + mortarboard = party

fret logic posted:

Heh, because I can't stop myself from wanting to mess with one or the other. So far it hasn't really been a big deal, but if it gets to the point where I can't separate the two, I'll stick with one.
Stick with C at first, because you will get confused between pointers and references and arrays and STL containers and new and malloc and all the things they do differently. Once you can look at C++ and see how it's built on top of C clearly, it will make a lot more sense.

fret logic
Mar 8, 2005
roffle

Professor Science posted:

Stick with C at first, because you will get confused between pointers and references and arrays and STL containers and new and malloc and all the things they do differently. Once you can look at C++ and see how it's built on top of C clearly, it will make a lot more sense.

Alright I'll do that. I'm probably going to pick up a paper copy of K&R today, was just surprised that it's still $50 at the bookstore.

edit: Woohoo bought it.

Oh, and I was thinking, if anyone else is learning C and wants a study buddy of sorts, I'd be down for it. I learn a lot better when I have someone to bounce concepts off of.

fret logic fucked around with this message at 04:38 on Feb 27, 2008

Mikey-San
Nov 3, 2005

I'm Edith Head!

fret logic posted:

Alright I'll do that. I'm probably going to pick up a paper copy of K&R today, was just surprised that it's still $50 at the bookstore.

K&R is so completely worth it, though. You'll be very glad you bought it. Chapter 5 of that book is a beautiful piece of technical writing, and probably the best tutorial on pointers I've seen.

fret logic
Mar 8, 2005
roffle

Mikey-San posted:

K&R is so completely worth it, though. You'll be very glad you bought it. Chapter 5 of that book is a beautiful piece of technical writing, and probably the best tutorial on pointers I've seen.

I don't doubt it. From what I've heard about it, and from what I've got through so far, it's great. It helps that I really enjoy programming and technical books, but yeah I like it so far :)

Mister Biff
Dec 26, 2006

Really stupid question about Visual Studio 2008:

I don't normally write Visual C stuff, but I found a command-line tool that I absolutely must have, and it was only available as source code. After a lot of rigamarole, I've managed to get a compiled .exe, but the file won't run on any other system!

The error is always the same: The system cannot execute the specified program.

I've tried installing the Visual C Runtime Libraries (for both 2005 & 2008) on my test machine, but it doesn't seem to make any difference. I know I'm probably being stupid, but I can't seem to figure out how to make this thing work.

P.S. -- I only have the following includes, which I don't think are anything out of the ordinary:
code:
#include <stdio.h>
#include <windows.h>
#include <lm.h>
#include <lmdfs.h>
I've also got NetAPI32.lib linked as a resource file, so that might be it, but I'm still baffled. Any thoughts? :confused:

FigBug
Apr 27, 2002

Lemon Party - Lest we Forget

Mister Biff posted:

Really stupid question about Visual Studio 2008:

Download dependency walker (http://www.dependencywalker.com/) on your test machine and load the .exe and see whats missing.

It's most likely something in the c runtime.

Go into project settings, C/C++, code generation and switch it the runtime from Multi-Threaded DLL to Multi-Threaded.

beuges
Jul 4, 2005
fluffy bunny butterfly broomstick

Mister Biff posted:

Really stupid question about Visual Studio 2008:

Since you say you've already installed the 2008 VC runtime libraries, check that you're compiling your app in release mode rather than debug mode. Doing a standalone install of the VCRT will probably only install the release versions, so check in event log to see why your app is failing to start - if it says it couldnt locate MSVCR90d.dll then you're running a debug build.

The best way to ensure your app will work on other systems is to add a new Setup project to your solution, and tell it use the output of your main project. The setup project will detect and include all the required dll's, including the version of the CRT that you're linking against, and will install it correctly on the target machine.

Mister Biff
Dec 26, 2006

Thanks to both FigBug and beunges!

Both tips turned out to be exactly what I needed. Thanks again.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
This is less a question and more a comment, but I discovered the following "feature" in typedefs today (don't ask how):
code:
struct base
{
	struct foo { int a; };
};

struct sub : public base
{
	typedef foo bar;
	struct foo { int b; };
};

int main()
{
	sub::bar x;
	x.a = 10; // Good
	x.b = 10; // Won't compile, argh!

	return 0;
}

fret logic
Mar 8, 2005
roffle
Ok this is kind of a stupid question but I can't figure out why this works the way it does. I'm working with the getchar() and putchar() functions in C, and I don't understand how this loop is working:

code:
#include <stdio.h>

main()
{
    int c;

    c = getchar()
    while (c != EOF) {
        putchar(c);
        c = getchar();
    }
}
The program does exactly what it's supposed to, though I changed the EOF to some random character because I'm not reading files with it so I don't know how to get EOF. But anyway, the part I don't understand is why it gives you back all the characters you inputted? If you put in "gently caress you computer", it will return "gently caress you computer" obviously. But why?

Especially when...
code:
main()
{
    int c;

    c = getchar();
    putchar(c);
}
Only returns the first character you enter. I thought that with the while loop, it does the first statement, you hit enter, then it does "putchar(c)", but I don't understand why it's holding the entire string of characters. Sorry if this is incredibly obvious

Scaevolus
Apr 16, 2007

fret logic posted:

Only returns the first character you enter. I thought that with the while loop, it does the first statement, you hit enter, then it does "putchar(c)", but I don't understand why it's holding the entire string of characters. Sorry if this is incredibly obvious

Use Control-D to send an EOF character. (In Linux, at least)

The reason it looks like it's holding the entire string in memory is because stdin is line-based -- nothing is sent to the program until you press Enter. You type in the line of text, and the program then processes that line, one character at a time.

Scaevolus fucked around with this message at 06:24 on Feb 28, 2008

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Avenging Dentist posted:

This is less a question and more a comment, but I discovered the following "feature" in typedefs today (don't ask how):
code:
struct base
{
	struct foo { int a; };
};

struct sub : public base
{
	typedef foo bar;
	struct foo { int b; };
};

int main()
{
	sub::bar x;
	x.a = 10; // Good
	x.b = 10; // Won't compile, argh!

	return 0;
}

Why the hell would it compile? x is a sub::bar, which is the same as a base::foo, so it doesn't have a b member. Only sub::foo, which is a completely unrelated struct, has a b member.

fret logic
Mar 8, 2005
roffle

Scaevolus posted:

Use Control-D to send an EOF character. (In Linux, at least)

The reason it looks like it's holding the entire string in memory is because stdin is line-based -- nothing is sent to the program until you press Enter. You type in the line of text, and the program then processes that line, one character at a time.

How is this different from the one without the loop? It'll let you type in an entire string, but only returns the first character. Are you saying that once you hit enter, it repeats the loop for each character in the line? I guess I'm not understanding how the loop affects the process.

Avenging Dentist
Oct 1, 2005

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

JoeNotCharles posted:

Why the hell would it compile? x is a sub::bar, which is the same as a base::foo, so it doesn't have a b member. Only sub::foo, which is a completely unrelated struct, has a b member.

Well yes, that's the point. The example is greatly simplified; the original involved a complicated chain of template instantiations and was non-obvious, especially since it broke in a completely separate area (the typedef'd template was instantiated with base::foo and all subsequent references to that template, typedef'd or not, used that instantiation).

Most of it is kinda obvious in retrospect, but template metaprogramming has a tendency to make obvious things not-so-obvious. :)

EDIT: I am sure it doesn't help that I have been unable to concentrate on anything for the last couple months. :(

Avenging Dentist fucked around with this message at 06:38 on Feb 28, 2008

Scaevolus
Apr 16, 2007

fret logic posted:

How is this different from the one without the loop? It'll let you type in an entire string, but only returns the first character. Are you saying that once you hit enter, it repeats the loop for each character in the line? I guess I'm not understanding how the loop affects the process.

Here's how it works:

You: Start program
Program: Initialize, getchar()
[Now the program is waiting for input, and is idle]
You: Input text, [enter]
[Now a line of input has been entered, and the program's getchar() finally returns a value]
Loop version:
--Program: Print character, getchar()
Without loop:
--Program: Print character, exit

fret logic
Mar 8, 2005
roffle
Ok now I feel really stupid, but it's still not clicking for me. If I rearranged the program to:

code:

main()
{
    int c;

    while (c != '.') {
        c = getchar();
        putchar(c);
    }
}
I'm in WinXP right now so I can't try it out, but I assume this wont be any different from the first loop program. So therefore, it will still do the same as the loop version, which is different from the non-loop version in that the entire line is grabbed and printed to the screen, instead of the first character. If this program here only prints the first character, then it has something to do with the getchar(c) outside of the loop, and the order of statements inside the loop, and I'm an idiot and need to go back over it and think harder.

edit: oops code is wrong in this one ^, fixed it, and tried on my laptop, it gives the same output as the first loop, so I'm still confused :/

fret logic fucked around with this message at 06:56 on Feb 28, 2008

Lexical Unit
Sep 16, 2003

code:
main()
{
    int c; // 1

    while (c != '.') { // 2
        c = getchar(); // 3
        putchar(c); // 4
    } // 5
}
Just take it easy and reason out each line.

1: c isn't initialized so you don't have any idea what's in c at the moment.

2: You're testing the value of c, but you don't know what c is. This is bad.

3: c becomes the return value of getchar(). getchar() will not return until the end of file or end of line is reached. So in your example let's say you type in a word like "stuff."

4: Whatever character c is, show it.

5: Go back to the top, and 2: since c isn't a "." (it's a "s") we continue to line 3 again.

3: c becomes the next character from stdin, you've already given the next character before, it's "t". getchar() won't wait for you to hit enter this time because it doesn't need to.

...

Eventually c becomes "." and we end the loop, having printed out "stuff" to stdout. The reason stdin works like this is because it's line-buffered, that's just how it works. Here's an ok discussion with some more information.

fret logic posted:

I'm in WinXP right now so I can't try it out
Can't you use Visual Studio Express — it's free to download.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Avenging Dentist posted:

Well yes, that's the point. The example is greatly simplified; the original involved a complicated chain of template instantiations and was non-obvious, especially since it broke in a completely separate area (the typedef'd template was instantiated with base::foo and all subsequent references to that template, typedef'd or not, used that instantiation).

Oh, I thought the "argh" was "argh C++ is so stupid why won't it let me do this" and not "argh I am so stupid how did I gently caress this up".

fret logic
Mar 8, 2005
roffle

Lexical Unit posted:

3: c becomes the return value of getchar(). getchar() will not return until the end of file or end of line is reached. So in your example let's say you type in a word like "stuff."

Lexical Unit posted:

3: c becomes the next character from stdin, you've already given the next character before, it's "t". getchar() won't wait for you to hit enter this time because it doesn't need to.

...

Eventually c becomes "." and we end the loop, having printed out "stuff" to stdout. The reason stdin works like this is because it's line-buffered, that's just how it works. Here's an ok discussion with some more information.

Thanks man, the bolded parts did it for me this time. That's what I was having so much trouble understanding, was that it kept an entire line in the memory, and that in the first one without the loop, it only printed out the first letter because I only called the putchar() function once. I'm going to do some more research on stdin now :)

Lexical Unit posted:

Can't you use Visual Studio Express — it's free to download.

Haha yeah I have it installed on here, but given that I haven't figured out how to use it, it annoys the ever living piss out of me. It's far too much labor for a simple C program like that, I am a freak for command line compiling now.

That Turkey Story
Mar 30, 2003

Avenging Dentist posted:

Well yes, that's the point. The example is greatly simplified; the original involved a complicated chain of template instantiations and was non-obvious, especially since it broke in a completely separate area (the typedef'd template was instantiated with base::foo and all subsequent references to that template, typedef'd or not, used that instantiation).

Are you saying that the base class was dependent on a template argument of the child? If so, on a compliant compiler you should have gotten an error at the typedef which would have probably saved you some trouble (since foo there isn't a dependent name you'd get error in the first phase before you even instantiate the template, but vc++ won't catch this because it incorrectly pushes everything off to instantiation). It always helps to use gcc instead of vc++ when doing template stuff.

Avenging Dentist
Oct 1, 2005

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

That Turkey Story posted:

Are you saying that the base class was dependent on a template argument of the child?

Not exactly, though I should probably update Cygwin and run my stuff through gcc at some point. A more complete example of my problem would be:

code:
#include <map>
#include <string>

template<typename T>
struct collection
{
	typedef std::map<std::string,typename T::foo> type;
};

struct base
{
	struct foo { int a; };
};

struct sub : public base
{
	typedef collection<sub>::type collection_type;
	struct foo : public base::foo { int b; };
};

typedef collection<sub>::type collection_type;

int main()
{
	collection_type coll;
	coll["butts"].b = 10; // Failure! But would work if I commented out the
	                      // typedef in sub (or moved it after sub::foo)
	return 0;
}

fret logic
Mar 8, 2005
roffle
What are some good websites besides Euler with stepping programming tasks that you don't have to know an entire language to do? Like ones you can do while learning a language

Space Duck
Oct 15, 2003
Not to appear that I've abandoned my own question, but I worked out the problem after the advice I got from this thread pointed me back in right direction.

I was messing around with daemonizing the program at the same time. I'd created the Log object, then called my daemonize function. The child process "lost" the Log object.

Moving the creation of the Log object after the daemonization (probably just moving it to after the forking of the process) solved the issue.

JoeNotCharles posted:

Is your log class flushing the stream?

Doesn't using endl trigger a buffer flush on it's own?

Contero
Mar 28, 2004

I'm interested in doing some windows API/mfc programming and a little bit of directX/openGL. VS Express is turning out to be a piss-poor tool for doing this (no api/mfc libraries, no resource editor). So aside from forking over $280 for a full version of VS, is there anyway I can actually get a tool that doesn't feel like it's fighting me at every step of the way?

I think I'm going to try finding my old copy of visual C++ 6 hidden away somewhere. I vaguely remember it having a resource editor and windows libraries.

Doc Block
Apr 15, 2003
Fun Shoe
You downloaded and installed the platform SDK, right? And followed Microsoft's instructions on how to set up VS Express to use it and have the ability to create Win32 apps, right? That should be all you need to utilize the Win32 API (Win32 API != MFC).

You would still lack a resource editor, but IMHO it would be far better to use VS Express and suffer without a resource editor than to go back to VS 6.0.

Contero
Mar 28, 2004

Yeah I've finally gotten the platform SDK set up. I figured out that I had downloaded the wrong SDK and got the proper link to the platform SDK. Everything is working better now except that I'm editing resource files by hand. At least I can compile the sample programs now.

Red Oktober
May 24, 2006

wiggly eyes!



I'm currently getting started in C, by using it to write a compiler of sorts (its for an assignment).

Much of the code is generated already by BNFC (from the grammar I've written) , but I need to edit it to traverse my tree and do stuff.

My question is what is a good development environment to start off in C?

I've been using eclipse for java, but the C version doesn't seem to be as usable.

What would you all recommend?

I'm using Mac OS X, if it matters.

haveblue
Aug 15, 2005



Toilet Rascal

Red Oktober posted:

I'm currently getting started in C, by using it to write a compiler of sorts (its for an assignment).

Much of the code is generated already by BNFC (from the grammar I've written) , but I need to edit it to traverse my tree and do stuff.

My question is what is a good development environment to start off in C?

I've been using eclipse for java, but the C version doesn't seem to be as usable.

What would you all recommend?

I'm using Mac OS X, if it matters.

Pretty much the only choice you have is Xcode, which is free once you sign up for a developer account on Apple's site (also free). You can also probably find it on your OS X install disc.

more falafel please
Feb 26, 2005

forums poster

Red Oktober posted:

I'm currently getting started in C, by using it to write a compiler of sorts (its for an assignment).

Much of the code is generated already by BNFC (from the grammar I've written) , but I need to edit it to traverse my tree and do stuff.

My question is what is a good development environment to start off in C?

I've been using eclipse for java, but the C version doesn't seem to be as usable.

What would you all recommend?

I'm using Mac OS X, if it matters.

I'd suggest taking this opportunity to learn about working without an IDE, especially since you're getting started with C. IDEs tend to abstract away a lot of important details about the C/C++ compilation model, which may come back to bite you later.

I would say to find a text editor that you like -- TextMate seems to be the favorite on OS X for just about everyone except the Vim users, or Vim if you want to be a real programmer^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H learn Vim. Then learn to use gcc and make, enough to write a simple Makefile.

Peanutmonger
Dec 6, 2002

more falafel please posted:

Then learn to use gcc and make, enough to write a simple Makefile.

Simple makefile?! Maybe at first, but if you were really into it, you'd go all out and use autotools. Vim (especially with ctags/etc) and autotools make a fantastic development environment.

ColdPie
Jun 9, 2006

Peanutmonger posted:

Simple makefile?! Maybe at first, but if you were really into it, you'd go all out and use autotools. Vim (especially with ctags/etc) and autotools make a fantastic development environment.

Do you have a good resource for more information on using autotools (esp. with vim)? I'd like to do something like that, but don't know where to start.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Peanutmonger posted:

Simple makefile?! Maybe at first, but if you were really into it, you'd go all out and use autotools. Vim (especially with ctags/etc) and autotools make a fantastic development environment.

You're insane. Autotools are ridiculously terrible to work with.

6174
Dec 4, 2004

JoeNotCharles posted:

You're insane. Autotools are ridiculously terrible to work with.

I have to agree. The reason for Autotools in the first place was to deal with shortcomings of the various implementations of Make and different compilers. Since those are relatively rare today there isn't a lot of reason to start a new project with Autotools. Even just using Make can get tedious on large projects. Modern, usable build controls systems exist such as CMake. In fact KDE semi-recently switched over to CMake.

Peanutmonger
Dec 6, 2002
ColdPie:
I found the sources I used from googling "autotools", there are some books that are free now (but old) that give good tips. There are some tutorials that give good basic forms of the files, and for configure.ac 'autoscan' can give good suggestions for macros. However, you're probably better off with a small makefile :)

JoeNotCharles posted:

You're insane. Autotools are ridiculously terrible to work with.

I probably am. I also probably haven't used autotools well enough to get out of the "honeymoon" phase of how wonderful it is that configure will discover everything for me and build a Makefile with all of the targets I've come to expect. I really enjoyed doing a "make dist" and copying that to my other desktop and building it. On the other hand, for things that won't leave your computer, I suppose a simple makefile should do very well. (I'm a little bitter about "simple makefile"s in packages I've downloaded that, from bit rot or whatever, ended with me rewriting the makefile so it would actually work)

6174:
cmake looks very promising. It's a little annoying that they chose the name cmake (since we have make, gmake, bmake), especially when it doesn't replace make, but that's okay. It also makes sense that KDE would choose it, since it loses all of the dependencies that autotools had, and they're heading for super cross-platformness. I don't have it installed currently (other than KDE4, the list of packages using it seems small), but maybe I'll test it out on one of my toy projects. Generating a VS project would be very nice...

Well, I originally felt silly for posting that comment, but it appears to have turned out reasonably educational.

6174
Dec 4, 2004

Peanutmonger posted:

6174:
cmake looks very promising. It's a little annoying that they chose the name cmake (since we have make, gmake, bmake), especially when it doesn't replace make, but that's okay. It also makes sense that KDE would choose it, since it loses all of the dependencies that autotools had, and they're heading for super cross-platformness. I don't have it installed currently (other than KDE4, the list of packages using it seems small), but maybe I'll test it out on one of my toy projects. Generating a VS project would be very nice...

If cmake isn't to your liking there are other options too such as SCons, and Boost.Jam. If you're using a language other than C or C++ the options also change, but I won't mention those since this is the C/C++ thread.

Basically it is pretty easy to find alternatives because Autotools isn't a good solution today. The main problem it used to solve is different today, and it has some screwy features, like the use of M4. M4 is just a pain in the rear end and asking a developer to become even somewhat familiar with it to build a program is just wrong.

more falafel please
Feb 26, 2005

forums poster

I say make and not autotools (or a better build system like CMake, SCons, or Boost.Jam) because it's simple and you're more likely to run into that, at least in the C/C++ world, than something else.

For a simple project with a few source files, a Makefile would look like this:
code:
# set variables for the compiler and flags
CC = gcc
CFLAGS = -g

# the space-separated list of source files
SOURCES = file1.c \
          file2.c \
          file3.c \
          main.c

# substitute .o for .c in $(SOURCES), get the list of object files 
OBJS = $(patsubst %.c,%.o,$(SOURCES))

# typing just "make" will make program
all: program

# program depends on all the objects and is built by the rule below
program: $(OBJS)
<tab> $(CC) $(CFLAGS) $(OBJS) -o program

# file1.o depends on file1.c and file1.h, $< will expand to file1.c and $@ to file1.o
file1.o: file1.c file1.h
<tab> $(CC) $(CFLAGS) -c $< -o $@

# same, but file2.o depends on file2.c, file2.h, and file1.h (assume file2.c includes file1.h)
file2.o: file2.c file2.h file1.h
<tab> $(CC) $(CFLAGS) -c $< -o $@

# same, but file2.o depends on file3.c, file3.h, and file1.h (assume file3.c includes file1.h)
file3.o: file3.c file3.h file1.h
<tab> $(CC) $(CFLAGS) -c $< -o $@

main.o: main.c file3.h
<tab> $(CC) $(CFLAGS) -c $< -o $@
Haven't tested that, but it should basically work.

haveblue
Aug 15, 2005



Toilet Rascal

more falafel please posted:

For a simple project with a few source files, a Makefile would look like this:

What about a project with 40 source files and multiple targets?

At some point you just want to let the computer take care of it.

Adbot
ADBOT LOVES YOU

ColdPie
Jun 9, 2006

HB posted:

What about a project with 40 source files and multiple targets?

At some point you just want to let the computer take care of it.

Not to mention traversing directories and making makefiles for each directory. Eclipse does a pretty good job of making makefiles, in my opinion.

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