Computer Science Canada

Why this dies

Author:  Tubs [ Thu Jan 19, 2006 9:16 pm ]
Post subject:  Why this dies

code:
#include <stdio.h>
#define PATH "/usr/tmp"

char *full_name(const char name[]);

int main()
{
  printf("Full name is %s\n", full_name("data"));
  system ("PAUSE");
  return (0);
}

char *full_name(const char name[])
{
  static char file_name[100];
  strcpy(file_name, PATH);
  strcat(file_name, '/');
  strcat(file_name, name);
  return(file_name);
}


Why does this program die a horribly gruesome death?

Author:  wtd [ Thu Jan 19, 2006 9:29 pm ]
Post subject: 

code:
#include <stdio.h>
#define PATH "/usr/tmp"

char *full_name(const char* name);

int main()
{
  printf("Full name is %s\n", full_name("data"));
  system("PAUSE");
  return 0;
}

char *full_name(const char* name)
{
  char* file_name = malloc(sizeof(char) * 100);
  strcpy(file_name, PATH);
  strcat(file_name, "/");
  strcat(file_name, name);
  return file_name;
}


What did I do differently? Smile

Author:  Tubs [ Thu Jan 19, 2006 9:46 pm ]
Post subject: 

Changed the parameter to a pointer and dynamically allocated memory for the variable?

Author:  Tubs [ Thu Jan 19, 2006 9:47 pm ]
Post subject: 

that code is giving me errors!!! line 19 makes a pointer from integer without a cast

Author:  wtd [ Thu Jan 19, 2006 9:55 pm ]
Post subject: 

Yes, returning a pointer to stack-allocated memory inside a function results in bad things happening.

I don't get any such error using GCC. Show me the exact code you were using.

Author:  Tubs [ Thu Jan 19, 2006 9:58 pm ]
Post subject: 

wtd wrote:
Yes, returning a pointer to stack-allocated memory inside a function results in bad things happening.

I don't get any such error using GCC. Show me the exact code you were using.


The one you posted.

Author:  wtd [ Thu Jan 19, 2006 10:04 pm ]
Post subject: 

Then I cannot help. The code I posted compiles flawlessly with GCC 3.4.2 (MinGW).

Author:  Tubs [ Fri Jan 20, 2006 1:01 am ]
Post subject: 

Ok, I got the code working. IDEs are foolish.

Another question: how would I go about making a function that finds a certain item in a linked list, given the number it is in order and a pointer to the beginning of the list? This is what I have so far:

code:
#include <stdio.h>
#include "coaster_info_t.h"

coaster_info_t find_nth_coaster (int *N, int index)
{

  coaster_node_t *current = *N;
  int i = 0;

  if (current != NULL && i != index)
    current = current->next;

  else if (current != NULL && i == index)
    return( current );

  else
    return ( NULL );
}

Author:  wtd [ Fri Jan 20, 2006 1:13 am ]
Post subject: 

Use a loop to follow the links?

Author:  Tubs [ Tue Jan 24, 2006 7:29 pm ]
Post subject: 

Yeah, thanks. New question if I may:

A sequence is defined by the following recursive relationship:

A(n) = A(n/2) + A(n/2) ; A(1) = 3

It is defined only for powers of 2 and you need only consider powers of two.

I don't think I know what they are asking here, this is my code so far:

code:
#include <stdio.h>

int A (int n);

int main(int argc, char *argv[])
{
  int n, x;

  printf ("n = ");
  scanf ("%d", &n);
  printf ("\n");

  x = A (n);
  printf ("A (n) = %.2f\n", x);
  system ("pause");

  return 0;
}

double A (double n)
{
  if (n == 1)
    return n;
  else
    n = A (n / 2) + A (n / 2);
}


Help greatly appreciated Smile

Author:  wtd [ Tue Jan 24, 2006 7:34 pm ]
Post subject: 

Look at that code really closely.

Now... remove all of the code inside the functions, leaving just the headers? See the problem?

Author:  Tubs [ Tue Jan 24, 2006 8:03 pm ]
Post subject: 

wtd wrote:
Look at that code really closely.

Now... remove all of the code inside the functions, leaving just the headers? See the problem?


Yeah, I was fooling around with the types because the question seemed to imply that that ints were ok, but there is division by 2 thrown in there. When I change it to double type it outputs 0, when it is an integer, 1. I'm still lost. Crying or Very sad

Author:  wtd [ Tue Jan 24, 2006 9:58 pm ]
Post subject: 

What they're saying is... when n is 1, A returns 3. For any other value of n, well... the question explains that. So, let's look at a breakdown of A(8).

code:
A(8)
A(4) + A(4)
(A(2) + A(2)) + (A(2) + A(2))
((A(1) + A(1)) + (A(1) + A(1))) + ((A(1) + A(1)) + (A(1) + A(1)))
A(1) + A(1) + A(1) + A(1) + A(1) + A(1) + A(1) + A(1)
3 + 3 + 3 + 3 + 3 + 3 + 3 + 3
24

Author:  Tubs [ Tue Jan 24, 2006 10:25 pm ]
Post subject: 

Thanks a lot wtd. You rock.

Author:  wtd [ Tue Jan 24, 2006 10:52 pm ]
Post subject: 

Glad I could help.

code:
(defun a (&optional (n 1))
  (cond ((= n 1) 3)
        ((> n 1)
         (let ((v (a (/ n 2))))
           (+ v v)))
        (t
         (let ((v (a (* n 2))))
           (+ v v)))))


Wink

Author:  Tubs [ Tue Jan 24, 2006 10:56 pm ]
Post subject: 

I have no idea what code that is.

Author:  wtd [ Tue Jan 24, 2006 11:12 pm ]
Post subject: 

It's Common Lisp. It's my way of just posting the answer. Wink

Author:  Tubs [ Tue Jan 24, 2006 11:22 pm ]
Post subject: 

Weird.

This function is still eluding me. (Recursion is not my fort'e) Any tips at all? Currently if a power of 2 is inserted it returns n.

code:
#include <stdio.h>

int A (int n);

int main(int argc, char *argv[])
{
  int n, x;

  printf ("n = ");
  scanf ("%d", &n);
  printf ("\n");

  x = A (n);
  printf ("A (n) = %d\n", x);
  system ("pause");

  return 0;
}

int A (int n)
{
  int ans;
  if (n == 1)
    ans = 1;
  else
    ans = A(n / 2) + A(n / 2);

  return ans;
}

Author:  wtd [ Tue Jan 24, 2006 11:29 pm ]
Post subject: 

code:
int A (int n)
{
  int ans;
  if (n == 1)
    ans = 1;
  else
    ans = A(n / 2) + A(n / 2);

  return ans;
}


Your use of a variable here is not a good habit to get into.

code:
int A (int n)
{
  if (n == 1)
    return 1;
  else
    return A(n / 2) + A(n / 2);
}


You're returning 1 here if n equals 1. Is that what the problem says should happen?

Author:  Tubs [ Tue Jan 24, 2006 11:37 pm ]
Post subject: 

Yeah, I thought the A(1) = 3 was just an example to show that you were on the right track. Also, I followed the format provided in my text book on how to do a recursive fibonacci sequency and it used an 'ans' variable.

Thanks again Very Happy

Author:  Martin [ Wed Jan 25, 2006 2:57 am ]
Post subject: 

As I have recently discovered

code:
if (1 == n)
is far superior to
code:
if (n == 1)


Which of the following will crash?

code:
if (n == 1)

code:
if (n = 1)

code:
if (1 == n)

code:
if (1 = n)

Author:  wtd [ Wed Jan 25, 2006 3:07 am ]
Post subject: 

Unfortunately this is sacrificing expressivity of the code to compensate for a [potential ambiguity in C++'s syntax.

It is vastly more natural to think "n == 1". Here "n" is emphasized because it is the subject. We're not interested in whether 1 equals "n". We're interested in whether "n" equals 1. Technically speaking, they're entirely equivalent. However, there is a linguistic factor in writing good code.

Author:  Martin [ Wed Jan 25, 2006 9:40 pm ]
Post subject: 

Well the easy solution is to start thinking in Japanese. Wink

Author:  Hikaru79 [ Wed Jan 25, 2006 10:47 pm ]
Post subject: 

Martin wrote:
Well the easy solution is to start thinking in Japanese. Wink


That'd be closer to "n 1 ==" Wink

Author:  Tubs [ Thu Jan 26, 2006 9:33 pm ]
Post subject: 

This code is telling me that data.name and next are not part of a structure or union, which is almost definately because I have some incorrect pointer usage somewhere, but I get all confuzzeled when it comes to double pointers. Any tips? (I would post the program in its entirety but it is a heirchy of many functions)

code:
#include "coaster_node_t.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

coaster_node_t *remove_duplicate_coasters( coaster_node_t **list, char namedelete[] )
{

  if (*list != NULL)
  {
    while (*list != NULL)
    {
      if (strcmp(*list->data.name, namedelete) != 0)
      {
        coaster_list_delete (&*list, namedelete);
      }
     
      *list->next;
    }
    return (*list);
  }
  else
  {
    printf ("List is empty.\n");
    return (*list);
  }
}

Author:  wtd [ Thu Jan 26, 2006 9:54 pm ]
Post subject: 

And without seeing the definition of these data structures, I'm supposed to tell you what about this code?

Author:  Tubs [ Thu Jan 26, 2006 9:58 pm ]
Post subject: 

Just wondering if there was any obvious misuse of pointer syntax. Here is the whole file:

Author:  Tubs [ Thu Jan 26, 2006 11:21 pm ]
Post subject: 

Misuse of pointer syntax such as not putting brackets around *list Confused
Fixed linker problem with having extraneous functions, now it dumps core after running, is'nt that great.

Author:  wtd [ Thu Jan 26, 2006 11:35 pm ]
Post subject: 

I've gotten spoiled. That level of pointer indirection is making my head hurt.

Author:  wtd [ Thu Jan 26, 2006 11:37 pm ]
Post subject: 

You have:

code:
*list->next;


In the function you pasted here.

This is in void context and does nothing. Why do you have it there?

Author:  Tubs [ Thu Jan 26, 2006 11:52 pm ]
Post subject: 

I thought it would select the next coaster for the delete function to work with. I've been doing this assignment for about 6 hours now so my head hurts too Sad

functions still to go:
- duplicate one node to another node
- duplicate a list to another list
- reverse a list

Author:  Tubs [ Fri Jan 27, 2006 12:00 am ]
Post subject: 

This may be my tiredness / burntoutedness setting in, but I really really appreciate the help wtd. You're the man.

Author:  wtd [ Fri Jan 27, 2006 3:12 pm ]
Post subject: 

Glad I can help even if only a little bit.

My suggestion... write one function at a time. Make sure that function works. Then move on.

Author:  Tubs [ Thu Feb 02, 2006 12:53 pm ]
Post subject: 

When using linked lists, how would you make a loop so that the current node is changed by a power of 2 in every iteration? (Creating a 'galloping search')

ex. currentnode->next // changed by one
currentnode->next->next // changed by two

Author:  wtd [ Thu Feb 02, 2006 2:10 pm ]
Post subject: 

Generalize psuedocode for dealing with linked list iteration.

code:
set "current" to first node in list

while current node's not null do
   access current node's stored value,
   and ...

   set "current" to the current node's pointer
   to the next node
end while loop

Author:  Tubs [ Thu Feb 02, 2006 3:11 pm ]
Post subject: 

This is a sorted list so I dont want a linear search, i want to increase the current node by a power of 2 in each iteration til the value of that node is greater than the target, in which case i run a linear search from the previous power of 2 to the current power of 2 until the node with the target is found.

Author:  Andy [ Thu Feb 02, 2006 3:32 pm ]
Post subject: 

have a for loop that increaes exponentially each time, then inside, have another for loop that goes from 1 to the first counter, and inside the second for loop, do your getNext stuff

Author:  Tubs [ Thu Feb 02, 2006 9:47 pm ]
Post subject: 

I'm getting there, but the list that I am supposed to use seems not to finish declaring itself. can anyone tell me whats wrong?

code:
#include <stdio.h>
#ifndef INT_TYPES
#define INT_TYPES

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

typedef struct
{
  int value;
}
int_data_t;

typedef struct int_node
{
  int_data_t data;
  struct int_node *next;
}
int_node_t;

// Prototypes.
void create_int_list( int_node_t **, int );
int_node_t *insert_int_list( int_node_t **, int_data_t * );
void print_int_list( int_node_t * );

#endif

int main(int argc, char *argv[])
{

  int_node_t *list;
  int_data_t something;
  something.value = 10;

  *insert_int_list ( &list, &something);
  create_int_list ( &list, something.value );
  print_int_list ( list );
  system("pause");
  return 0;
}

void create_int_list( int_node_t **list, int n )
{
  int i;
  int_data_t temp;

  for( i = n ; i >= 0 ; i-- )
  {
    // Insert the numbers in reverse order into the front of the list.
    temp.value = i;
    insert_int_list( list, &temp );
  }
}

int_node_t *insert_int_list( int_node_t **list, int_data_t *data )
{
  int_node_t *temp = NULL;

  temp = (int_node_t *)malloc( sizeof( int_node_t ) );

  if( temp != NULL )
  {
    temp->data = *data;
    temp->next = *list;
    *list = temp;
  }
  return temp;
}

void print_int_list( int_node_t *list )
{
  int_node_t *current = list;

  while( current != NULL )
  {
    printf( "%4d", current->data.value );
    current = current->next;
  }
  printf( "\n" );
}

Author:  wtd [ Thu Feb 02, 2006 10:24 pm ]
Post subject: 

code:
typedef struct int_node
 {
   int_data_t data;
   struct int_node *next;
 }
 int_node_t;


This is one of the more inane things I've ever seen.

code:
int main(int argc, char *argv[])
 {
 
   int_node_t *list;
   int_data_t something;
   something.value = 10;
 
   *insert_int_list ( &list, &something);
   create_int_list ( &list, something.value );
   print_int_list ( list );
   system("pause");
   return 0;
 }


Why are you dereferencing the return value of insert_int_list in a void context?

Author:  wtd [ Thu Feb 02, 2006 10:25 pm ]
Post subject: 

wtd wrote:
code:
typedef struct int_node
 {
   int_data_t data;
   struct int_node *next;
 }
 int_node_t;


This is one of the more inane things I've ever seen.


Errrr... I meant:

code:
typedef struct
 {
   int value;
 }
 int_data_t;

Author:  Tubs [ Thu Feb 02, 2006 10:26 pm ]
Post subject: 

Yeah i was wondering about that as well. i guess its just to get us used to declaring the structure and stuff.

Author:  Tubs [ Thu Feb 02, 2006 10:29 pm ]
Post subject: 

As for the dereferencing, I get the same error when i change it.

Author:  wtd [ Thu Feb 02, 2006 10:30 pm ]
Post subject: 

An additional note is that you should not, by any means, be casting the return value of malloc.

Author:  wtd [ Thu Feb 02, 2006 10:32 pm ]
Post subject: 

Tubs wrote:
As for the dereferencing, I get the same error when i change it.


Yes, it's likely not the source of the error, but it is worth pointing out.

Author:  Tubs [ Thu Feb 02, 2006 10:38 pm ]
Post subject: 

i only wrote the main of this stuff, the rest was supplied by my prof for this assignment. its kinda hard to do this when the supplied material doesnt work.

Author:  wtd [ Thu Feb 02, 2006 10:48 pm ]
Post subject: 

Indeed.

Author:  Tubs [ Thu Feb 02, 2006 10:55 pm ]
Post subject: 

So no idea why its not declaring the end of the list properly?

Author:  wtd [ Thu Feb 02, 2006 11:00 pm ]
Post subject: 

I have no idea why anything in that library is written the way it is.

Author:  Tubs [ Thu Feb 02, 2006 11:01 pm ]
Post subject: 

Yeah. Fuck the library i'm making my own damn list. Excuse my french.

Author:  Tubs [ Sun Feb 12, 2006 8:48 pm ]
Post subject: 

I cannot figure out why this program dies (no error messages on my IDE either Sad) Any help is greatly appreciated as always Smile

The program is supposed to take an integer, use the division by 2 method to determine the binary equivalent using stacks.

Author:  Tubs [ Sun Feb 12, 2006 10:26 pm ]
Post subject: 

Scratch that last post. Re-wrote program. Linked lists suck.

Author:  Tubs [ Thu Mar 30, 2006 9:55 pm ]
Post subject: 

code:

void median_qsort( int a[], int p, int q, int piv )  /*to sort the subarray */
{                          /* a[p:q] of array A into ascending order */
  int i, j;

  if( p < q )
  {
         /* Initially i and j point to the first and last items */
     i = p;
     j = q;
         partition( a, &i, &j, piv ); /* partitions a[p:q] into a[p:j] and a[i:q] */
         median_qsort( a, p, j, piv );
         median_qsort( a, i, q, piv );
  }
}


where piv is a value between 1 and 3 determining the location of the pivot (1 being the beginning, 2 being the middle and 3 being the end of the array)

code:

void partition( int a[], int *i, int *j, int piv )
{
  int pivot, temp;

  pivot = median (a[*i], a[*j - 1], a[SIZE / 2]);

  if ( piv == 1 )
  {
    if ( pivot == a[*j-1] )
    {
      temp = a[0];
      a[0] = pivot;
      a[*j-1] = temp;
    }
    else if ( pivot == a[SIZE/2] )
    {
      temp = a[0];
      a[0] = pivot;
      a[SIZE/2] = temp;
    }
  }
  else if ( piv == 2 )
  {
    if ( pivot == a[*j-1] )
    {
      temp = a[SIZE/2];
      a[SIZE/2] = pivot;
      a[*j-1] = temp;
    }
    else if ( pivot == a[0] )
    {
      temp = a[SIZE/2];
      a[SIZE/2] = pivot;
      a[0] = temp;
    }
  }
  else if ( piv == 3 )
  {
    if ( pivot == a[0] )
    {
      temp = a[*j-1];
      a[*j-1] = pivot;
      a[0] = temp;
    }
    else if ( pivot == a[SIZE/2] )
    {
      temp = a[*j-1];
      a[*j-1] = pivot;
      a[SIZE/2] = temp;
    }
  }

  print_list ( a, SIZE );

  do {
/* Find leftmost i such that a[i] >= Pivot.*/
        while( &a[*i] <= &pivot )
    {
          (*i)++;
    }
/* Find rightmost j such that a[j] <= Pivot.*/
        while( &a[*j] >= &pivot )
    {
                (*j)--;
        }
/* if i and j didn't cross over one another, swap */
        if (*i <= *j)
    {
                temp = a[*i];
                a[*i] = a[*j];
                a[*j] = temp;         /* a[i] and a[j] */
                (*i)++;               /* move i one space right */
                (*j)--;               /* move j one space left */
         }
  } while (*i >= *j); /* while the i and j pointers haven't crossed yet */
}


I was supplied the original code for quicksort and partition, and instructed to code the rest to make it median of three quicksort (main declares the piv variable). The median calculation works fine, as does the switching. The problem is that the program takes about 7 seconds between printing the list (print_list function) and it does not sort it at all. Any ideas where the error is? Any help is greatly appreciated!


: