Thread: FAQ: Examples of good questions (General)

  1. #1
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897

    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 *head;
    
    void add ( node *to_add )
    {
      node *curr;
    
      curr = head;
    
      while ( curr != 0 )
        curr = curr->next;
    
      curr = to_add;
    }
    
    int main()
    {
      node *n;
    
      for ( int i = 0; i < 10; ++i ) {
        n = new node;
    
        n->i = i;
        n->next = 0;
    
        add ( n );
      }
    
      //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
    My best code is written with the delete key.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    i wholeheartedly agree.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Good forum for computer hardware questions?
    By PJYelton in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 03-27-2005, 08:17 AM
  2. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  3. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  4. Welcome to the FAQ Board
    By kermi3 in forum FAQ Board
    Replies: 0
    Last Post: 11-01-2001, 10:33 AM