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
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.

Adbot
ADBOT LOVES YOU

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.

haveblue
Aug 15, 2005



Toilet Rascal

citsejam posted:

If I disable certain signals in a parent process with sigignore, will I be able to use them in a child process if I use sigset to re-enable them in the child's code?

That should work, yes. fork() gives the new process an independent signal mask (initialized to the current value of the parent's). At least, that's how it works in posix- I don't have a manpage for "sigignore" so I assume you're on something else.

haveblue fucked around with this message at 02:50 on Mar 2, 2008

haveblue
Aug 15, 2005



Toilet Rascal

Thug Bonnet posted:

No, that's what I'm asking! :) I'm just kind of curious, honestly. But how would the program know "where it is" in memory, how large it is, etc. Also, I assume it's not necessarily contiguous..

Each process's address space is identical*, so that information isn't really useful to anything other than the kernel. If such a query existed, it would return identical results for all processes on the computer. A program generally doesn't have or need the ability to know what physical addresses its virtual pages are at, especially because those may change literally from instruction to instruction under a modern virtual memory system.



*Unless ASLR is enabled, but again, that's only the OS's concern.

haveblue
Aug 15, 2005



Toilet Rascal

Entheogen posted:

I believe the OS allocates a chunk of RAM to the program and maps it using virtual memory. That is for program the memory may start at 0x0000 or something but then OS maps it to real memory. I am not quite sure.

Pretty much. Null, the stack, the default heap, and the application code all tend to show up at the same (virtual) address for all programs. The locations of loaded libraries are the same for each launched instance of a linked binary (but may be different for other, different programs).

Incidentally, this is what allows buffer overflow exploits to work and be portable- the locations of parts of the process image can be predicted across systems (and it's what ASLR is designed to break).

quote:

I think for C++ programs "how large it is" can grow as the program continues to allocate new memory on heap, so the OS must be adding to its virtual page. I know for java you have to actually set a maximum amount of heap memory you will use, but not for C++.

The largest possible address depends on which system you're on. On Win32 you get 2GB of virtual space; the largest valid pointer value is UINT_MAX/2. On 32-bit OS X and other Unixes you get somewhere between that and 4GB. On a 64-bit architecture the limit is so high it's not worth calculating.

haveblue fucked around with this message at 22:39 on Mar 10, 2008

haveblue
Aug 15, 2005



Toilet Rascal

xobofni posted:

Really stupid question here about pointers in C.

This code runs just fine.
code:
int main ()
{
    char a[] = "hello world";
    a[0] = 'b';
    return 0;
}
This code crashes.
code:
int main ()
{
    char *a = "hello world";
    a[0] = 'b';
    return 0;
}
I thought those declarations were exaclty the same, but apparently they are not. What am I missing here?

The first code copies the string constant into the variable-sized array a, the second creates a pointer to the string constant before attempting to change it. These compiled-in constants are read-only by default; if you're using GCC there's a flag you can use to allow writing to them.

haveblue
Aug 15, 2005



Toilet Rascal
Optimizations and debugging are pretty much mutually exclusive; the compiler will tie your code in knots behind the scenes. Especially if it uses aggressive inlining.

haveblue
Aug 15, 2005



Toilet Rascal
Yeah, sorry, I meant that its size was determined at compile-time rather than typing-time.

haveblue
Aug 15, 2005



Toilet Rascal

very posted:

A friend of mine thinks that the memory where function addresses are stored is getting clobbered somehow. Is that even possible? Would VC++ just let me do that? I put the code that breaks at the very beginning of my program and stepped through it with no other threads running, but it still goes to the wrong function.

I've also since rebuilt the library, but that didn't fix it either.

It wouldn't *knowingly* let you do that (not in C++, at least), but it can certainly get corrupted.

What relationship, if any, does the wrong function have with the correct one? Also, double-check the type of the pointer you're using to invoke the method.

haveblue
Aug 15, 2005



Toilet Rascal

graffin posted:

I'm using it in C++, in the context of a template for a binary tree based dictionary. The object is an element in a node. In the dictionary, I'm trying to test the element to determine if it is an int or string.

C++ doesn't have any facility for determining what basic type a symbol is, RTTI only works on classes.

Also, if your template class has to implement different functionality based on the type of the varying element, you may want to revisit its design.

haveblue
Aug 15, 2005



Toilet Rascal
That error means that there is no main function anywhere that gcc can find. It's trying to produce an executable and can't figure out where your code takes over once the process is created.

Where is main? Is there a path of includes leading to it from deck.cpp? If you want to produce an object file for linking later you have to use a different invocation of gcc.

haveblue
Aug 15, 2005



Toilet Rascal
I disagree. The author should determine dependencies and set them in stone once; the users should be able to bypass those just as they're bypassing whatever issues and complexities prompted them to use pre-existing code to address in the first place.



vvvvvvvv Uninitialized pointers?

haveblue fucked around with this message at 22:37 on Mar 26, 2008

haveblue
Aug 15, 2005



Toilet Rascal

TSDK posted:

TSDK's Law: Everyone is an idiot.

It doesn't matter how smart someone is, there is always some topic or opinion about which that person is an idiot.

Yeah, but you wouldn't think that for someone with his resume that topic would be C.

haveblue
Aug 15, 2005



Toilet Rascal

fret logic posted:

Where's a good place to start for programming simple graphics in C? Nothing fancy, just lines or pixels to get me started. I've tried to decipher the source code for nethack, as far as how they print the ASCII characters in different places, and it's a bit above me heh.

Manipulating a character display the way NetHack does isn't really the same as "lines or pixels".

The best way to quickly make something appear on the screen is probably OpenGL with GLUT. A program that opens a window and draws some simple shapes can come in under 30 lines. Once you're comfortable with that, you can leave GLUT behind to do your own context setup, and you're off and running.

haveblue
Aug 15, 2005



Toilet Rascal

TurtleBoy posted:

I am looking into programming a game and I had a question which google is proving useless to answer, possibly because I am asking it the wrong questions.

The art assets for this project I want to store in such a way that only the program can retrieve them(the artists are VERY protective of their work). By this I mean how most games you play don't have big folders of jpgs for everything, they have some sort of data file that is read when loading a level.

What is the best way to implement this?

The best way to implement it is zlib; the data files are just compressed directory trees.

You won't be able to stop people from taking the art if they really want it, so you may as well save yourself some work.

haveblue
Aug 15, 2005



Toilet Rascal

xgalaxy posted:

Can anyone recommend any good books on compiler / interpreter / language theory, lex and yacc, etc?

Thanks.

Compilers: Principles, Techniques, and Tools, aka the Dragon Book, is the gold standard.

haveblue
Aug 15, 2005



Toilet Rascal
Is your header protected by #ifdefs? That error means that the compiler passed over the same declarations twice.

haveblue
Aug 15, 2005



Toilet Rascal
...this is the thread for answering programming questions. Is that process really so complicated?

Yes, it's almost certainly the lack of that that is causing the problem. There's a point in your code where airport.h can be reached through two different include statements (one to airport.h itself, one to another file that also includes airport.h), so the compiler thinks it's been defined twice.

haveblue fucked around with this message at 05:19 on Apr 7, 2008

haveblue
Aug 15, 2005



Toilet Rascal
The OS X 10.5 manpage for fflush makes no mention of input streams; it only says the function will fail if the stream is "not open for writing".

haveblue
Aug 15, 2005



Toilet Rascal

6174 posted:

Would a function pointer to a swap routine be reasonable? If needed I can restructure things to make this easier.

This would be quite reasonable. You'd have the indices as its parameters and just have it perform the same swap on any of an arbitrary set of arrays.

quote:

Can anyone point me in the right direction?

You need the struct keyword before the type of the parameter (or a typedef statement for the structure definition).

haveblue fucked around with this message at 22:24 on Apr 14, 2008

haveblue
Aug 15, 2005



Toilet Rascal

almostkorean posted:

Can someone tell me why this doesn't work?

code:
 
  char operand;
  double *number;
  *number = atof( operand );
When I compile I get:
code:
calc.cpp:31: error: invalid conversion from ‘char’ to ‘const char*’
calc.cpp:31: error:   initializing argument 1 of ‘double atof(const char*)’
calc.cpp:31: error: cannot convert ‘double’ to ‘double*’ in assignment
I feel like this should work, but I can't figure it out.

Well, first, atof expects a string, not a single char (and it expects a null terminator, so don't just pass it &operand).

Second, once you get it to run, it's going to crash because you didn't allocate any memory for number to point to.

haveblue
Aug 15, 2005



Toilet Rascal
Your problem right now seems to be coming up with a way to organize the data you're gathering. Do you know about structures yet?

haveblue
Aug 15, 2005



Toilet Rascal

Jungle Bus posted:

I'm taking my first C class right now, and it's mostly going well, but there's one thing I can't figure out: how do I take keyboard input without waiting for the user to press enter (along the lines of "press any key to continue")? Am I missing something obscenely obvious?

getc()

haveblue
Aug 15, 2005



Toilet Rascal

Drx Capio posted:

How else would you implement a 3-Vector? :raise: It's called std::vector for a reason, you know!

Probably with something that doesn't have a ton of baggage related to dynamic resizing.

haveblue
Aug 15, 2005



Toilet Rascal
Filling in a 12GB range with unpredictable writes of single bytes? When did he say this project had to be finished by?

(And don't forget to throw in a targa header before you start writing pixels.)

haveblue
Aug 15, 2005



Toilet Rascal
%s expects a char*, not just a char.

(Next time, please try to give more information about the crash, at least the line number.)

haveblue
Aug 15, 2005



Toilet Rascal

Mustach posted:

There's not really a "should" here. All of these work for bypassing the struct namespace:
code:
typedef struct Node { ... } Node;
code:
struct Node { ... };
typedef struct Node Node;
code:
typedef struct Node Node;
struct Node { ... };
code:
typedef struct { ... } Node;

When using all except the last, make sure the struct's real name is different from the one created by the typedef.

haveblue
Aug 15, 2005



Toilet Rascal

Vanadium posted:

Why?

To avoid a conflict, that's how I remember learning it v:)v

I just tried it and apparently I have been misled all these years.

haveblue
Aug 15, 2005



Toilet Rascal

Soldat posted:

When I call longprim_check, I just went it to return true if the input is simply 'add1' or 'sub1'. Anything else I want it to return false. Right now it's returning true for everything. What glaringly obvious thing am I missing?

The strcmp function and the fact that most of your nested if statements do not have else cases. If you pass "adff" to primcheck_add it's just going to fall out in the middle, returning garbage (which would be interpreted as true unless it was 0). Your compiler should be warning you about this.

haveblue
Aug 15, 2005



Toilet Rascal
Looks like numMatches is not getting reset properly either. So it just adds the match onto all the matches from all previous trials.

haveblue
Aug 15, 2005



Toilet Rascal

Zombywuf posted:

No, but if you're ACKing packets it's usually the better option. But it seems Smackbilly has no say in the protocol so it's moot anyway.

Lots of games using UDP with a very thin reliable layer on top of it. It can be tweaked to better match their traffic patterns (small packets, no bulk transfers, compensation for loss at the application layer) than TCP.


vvvvvvvv Considering the subject, your analogy should be reversed :v:

haveblue fucked around with this message at 05:26 on May 26, 2008

haveblue
Aug 15, 2005



Toilet Rascal

Kei posted:

Is sendto() a non-blocking operation?

I ask because I'm trying to make a UDP-based application that buffers the packets at the application level rather than in the kernel because I want to be able to remove packets from the queue in certain circumstances.

My idea was to make the socket write buffer really small (the minimum being 2KB) so that only a packet or two would fit in there at a time. I would then use select() to wait for the buffer to open up enough for me to write a new packet.

However, it seems that select() returns immediately even if several packets have already been put in the socket buffer. Perhaps I'm just not going about this correctly. Any tips on achieving this would be greatly appreciated.

You can explicitly request a nonblocking socket from socket(), which will never block but instead cause socket calls to return an error immediately.

haveblue
Aug 15, 2005



Toilet Rascal

Avenging Dentist posted:

That's basically why they exist.

I thought they existed because until very recently compilers were not smart enough to detect good places to generate jump tables automatically and switch was how you explicitly requested one.

haveblue
Aug 15, 2005



Toilet Rascal

king_kilr posted:

I don't know how Clang does on template error messages, but one of the things I've heard, both from Brett Cannon on compiling CPython and from the Ars Technica review of Clang integration in XCode is that the errors are lightyears better.

The examples in the Ars review were brilliant. Highlight the exact token it choked on in the editor view? Yes, please.

haveblue
Aug 15, 2005



Toilet Rascal
Consider that Clang exists largely because it was easier to create a brand new toolchain with a different fundamental design than to add that and various other features to GCC.

haveblue
Aug 15, 2005



Toilet Rascal

bobbles posted:

Question: How can I compile c from the terminal in Snow Leopard gcc gives this...
-bash: gcc: command not found

OS X does not install dev tools by default, they may be present on the Snow Leopard DVD or if not can be downloaded from http://developer.apple.com .

haveblue
Aug 15, 2005



Toilet Rascal

Runaway Five posted:

Quick question, is the following okay in a sense it won't cause memory leaks?

Why are you using a generic pointer for an object you obviously know the type of?

haveblue
Aug 15, 2005



Toilet Rascal
That's one of those things that you pretty much need to explain why you're trying to do it so you don't get dismissed out of hand as "that's something that just shouldn't be done".

If you're trying to avoid a million search-and-replace operations, you could implement your alternative div with a different name and redirect references to it with a #define (and #undef at the end).

haveblue
Aug 15, 2005



Toilet Rascal
Like ShoulderDaemon said, that is not guaranteed to stay accurate through manipulation of the executable after the process is created. You can try to apply software protection to it like holding open a file descriptor to your executable but you will still trip over something really crazy and unavoidable like being launched off a network disk which subsequently times out and disappears.

Adbot
ADBOT LOVES YOU

haveblue
Aug 15, 2005



Toilet Rascal

Whilst farting I posted:

I understand how translation and rotation work, I think, now - I just don't understand why my code is not working. It'll be a lot easier for me to understand projection matrices though if I can get it moving besides manually changing the x coordinates of the quad.

For instance, I don't get why this code works, from Red Book chapter 3:

code:
draw_wheel_and_bolts()
{
   long i;

   draw_wheel();
   for(i=0;i<5;i++){
      glPushMatrix();
         glRotatef(72.0*i,0.0,0.0,1.0);
         glTranslatef(3.0,0.0,0.0);
         draw_bolt();
      glPopMatrix();
   }
}
But the code I posted above, which is virtually identical, does not.

code:
// called whenever the rectangle needs to move
void rectMove()
{
   // only animate when actually doing something
   if (rectStage != RECT_STATIC)
   {
        // commented out because the entire screen 
        // would go blank whenever it was called
        // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	

	glPushMatrix(); // saves the matrix as it is
	glLoadIdentity(); // loads the matrix as it is

        // moves the square 0.5 pixels to the right and up
        // and zoom out at a scale of -6
	glTranslatef(0.5f, 0.5f, -6.0f);

        // rotates on the x axis at -.75 units 
	glRotatef(-0.75f, 0.0f, 0.0f, 1.0f);

		glBegin(GL_QUADS);
			glColor3f(0.5, 1.0, 0.5);
		        glVertex3f(rectX1, 110.0, 0.0);
		        glVertex3f(rectX2, 110.0, 0.0);
		        glVertex3f(rectX2, 245.0, 0.0);
		        glVertex3f(rectX1, 245.0, 0.0);
		glEnd();
	glPopMatrix();
      }
}

-0.75 is a very small amount for a GL rotation; that number is supposed to be in degrees (and see how the example code uses 72 there). A much larger number is needed for the image to noticeably change.

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