# FAQ: Examples of good questions (General)

• 10-11-2002
Prelude
Examples of good questions
Fake Example questions:
These examples ask valid questions where the answer cannot be easily found in common resources (or at least I pretend they are, the linked list question has come up before here. So has the dupstr problem). Notice that all code given is short and formatted to be as readable as possible (this includes using the forum code tags as well as avoiding any cryptic syntax). The code given is also compilable as is and exhibits the problem. The question is asked in an intelligent and humble manner, not expecting everything to be done for the poster, but instead just pointers to get started on a solution. While there isn't an example of a question that uses file input, a small piece of the contents of said file should be posted as well so that anyone who replies will understand what formatting it has and can test solutions thoroughly, or the code should be modified to use constant values or user input if the problem isn't directly related to the file operations.

1)
I'm trying to create a linked list, but for some reason the changes I make when I try to add a node don't have any effect on the list itself. I've looked in a bunch of books and searched these boards already, but nothing I've found explains what is going on well enough to help me solve my problem. I've narrowed the problem down to my add function, but I can't figure out why changing what curr points to doesn't change head since curr points to memory used by head. I must not understand the relationship of the pointers, can anyone offer some advice to help me along? Here is my code simplified so that the problem can be seen more easily.
Code:

#include <iostream>

struct node { int i; node *next; };

{
node *curr;

while ( curr != 0 )
curr = curr->next;

}

int main()
{
node *n;

for ( int i = 0; i < 10; ++i ) {
n = new node;

n->i = i;
n->next = 0;

}

//print();
//destroy();

return 0;
}

Thank you.

2)
Hi there, I've been working on a function that copies a string and returns it to the calling function. So far everything works except for when I try to free the memory in the duplicated string. I can't find anything in my books, on this forum, or online that relates to my problem. Can anyone point me in the right direction or offer resources that explain this aspect of memory allocation? Here is my code, the string is duplicated just fine but I get a runtime error when the program reaches the call to free.
Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *dupstr ( char *old_str )
{
char *new_str = NULL;

new_str = (char *)malloc ( strlen ( old_str ) );

if ( new_str != NULL ) {
strcpy ( new_str, old_str );
}

return new_str;
}

int main ( void )
{
char *dup;
char str[] = "This is a test";

dup = dupstr ( str );

puts ( dup );
free ( dup );

return 0;
}

I have no doubt that I'm missing something with how I handle memory, but I haven't been able to determine what.

3)
Hello, I'm interested in Windows game programming, but I heard that a lot of resources (even books!) can be very bad. Can anyone help me to figure out which resources are high quality enough to get me started on the right track? I tried looking at books, but my lack of knowledge about the subject means I don't know if they teach the right things and there are just so many online tutorials that they can't all be good. Links would be nice, but some common idioms to search for in a resource would be just as helpful, if not more so.

-Prelude
• 10-11-2002
ygfperson
i wholeheartedly agree.