C Board  

Go Back   C Board > General Programming Boards > FAQ Board

 
 
LinkBack Thread Tools Display Modes
Old 10-11-2002, 07:37 PM   #1
Code Goddess
 
Prelude's Avatar
 
Join Date: Sep 2001
Posts: 9,661
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.
Prelude is offline  
Old 10-11-2002, 08:57 PM   #2
Just because
 
ygfperson's Avatar
 
Join Date: Jan 2002
Posts: 2,502
i wholeheartedly agree.
ygfperson is offline  
 

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:12 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22