Thread: Trying to create a simple array program

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I just wanted to add my two cents in here and say that although you are clearly proud of your code in post #4, you did say that you wanted corrections:

    Quote Originally Posted by cyberjupiter
    It works but there's one problem. The buffer should not accept more than 10 chars. For example, if I input "123456789ab", 'b' should be omitted because it exceeds 10 chars(I might be wrong here, please correct me). But it does not work like that.
    And the information that you needed to know is that while the code may appear to be doing the right thing, it is only doing so incidentally. If you enter a particularly long string, the characters beyond the 9th one are written into the 10th, which is one element off the end of the array. While you have gotten lucky writing there, (and refused to listen when this was pointed out to you) all it took me was some particularly careless pasting of input and your program crashed.

    Code:
    C:\Users\jk\Desktop\sandbox>gcc -std=c99 -ggdb -o sandbox.exe sandbox.c
    
    C:\Users\jk\Desktop\sandbox>gdb sandbox
    GNU gdb (GDB) 7.9.1
    Copyright (C) 2015 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-w64-mingw32".
    Type "show configuration" for configuration details.
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>.
    Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.
    For help, type "help".
    Type "apropos word" to search for commands related to "word"...
    Reading symbols from sandbox...done.
    (gdb) run
    Starting program: C:\Users\jk\Desktop\sandbox\sandbox.exe
    [New Thread 12116.0x1130]
    [New Thread 12116.0x5e4]
    This is a very long string let's see how we do. Does the program still work if I do not care about theoretical limits? fjmafj;ioawfjka'ifjakwmfFAO;JKFK M;JfwemfE;JKMFKOefk,EFKF,AMFAF';POKLFAMFkfawjfnawrfjaw49irt94ujfrkfwaokf
    
    Program received signal SIGSEGV, Segmentation fault.
    0x000000000040165a in main () at sandbox.c:27
    27                          storage[i + newidx] = buffer[i]; //this code will always store buffer[] into the last newidx value of storage[]
    (gdb) q
    A debugging session is active.
    
            Inferior 1 [process 12116] will be killed.
    
    Quit anyway? (y or n) y
    Like everyone says, this does segfault somewhere.

    So in short, I think you tested your attempt a little too lightly and got happy when the results were what you wanted. Generally, this is a poor approach to debugging -- you should trust code that you write the least of all.

    If you really want to truncate lines that the user enters, then there is no reason to write anything in excess of what you want to keep anywhere. It is worth doing that correctly instead of risking a segmentation fault.

    Further, in regard to writing a program to copy smaller arrays into bigger arrays, I have to agree with stahta01 and Asymptotic that the attempts so far are a bit wonky. At the very least, what would be the most helpful, especially for studying, I think, is an approach that is similar to what fgets() and strcpy() might do. This is because you would see it a lot in other places, mostly. The basic implementation for fgets() and strcpy() is both simple and tested. Plus, the sooner you get comfortable with what the standard C library can do and how it works, the better of a programmer you will be. So here is something you can study from. Some people would be happier with code that isn't broken up into functions, but let me tell you based on personal experience, this was way easier to debug.
    Code:
    C:\Users\jk\Desktop\sandbox>more sandbox.c
    #include <stdio.h>
    char * getBuffer(char *buffer, int n) {
       int i = 0;
       int ch;
       while (i < n - 2 && (ch = getchar()) != '\n' && ch != EOF) {
          buffer[i++] = ch;
       }
    
       /* detect longer lines and truncate them */
       if (ch != '\n' && ch != EOF) {
          while ((ch = getchar()) != '\n' && ch != EOF)
             /* intentionally blank */;
       }
    
       /* add back the \n we would have discarded */
       if (ch == '\n')
          buffer[i++] = ch;
    
       if (ch != EOF) {
          buffer[i] = '\0';
          return buffer;
       }
       else {
          return NULL;
       }
    }
    
    int copy(char *storage, char *buffer) {
       int i;
       for (i = 0; (storage[i] = buffer[i]) != '\0'; i++)
          /* intentionally blank */ ;
    
       storage[i] = '\0';
       return i;
    }
    
    int main(void)
    {
       char buffer[BUFF_SIZE] = "";
       char storage[STORAGE_SIZE] = "";
       int cursor = 0;
       int m;
    
       for (m = 0; m < STORAGE_SIZE / BUFF_SIZE; m++) {
          if (getBuffer(buffer, BUFF_SIZE) != NULL) {
             cursor += copy(&storage[cursor], buffer);
          }
          else {
             break;
          }
       }
    
       puts(storage);
       return 0;
    }
    
    
    C:\Users\jk\Desktop\sandbox>gcc sandbox.c -ansi -DBUFF_SIZE=10 -DSTORAGE_SIZE=1000 -o sandbox.exe
    
    C:\Users\jk\Desktop\sandbox>sandbox
    test1
    test2
    test3
    0123456789ab
    This is a much larger string, let's see how we do.
    ^Z
    test1
    test2
    test3
    01234567
    This is
    I hope this helps, and in the future, try to extend a little more patience (belief?) when someone tries to tell you that something is wrong.
    Last edited by whiteflags; 09-23-2017 at 02:32 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 09-03-2013, 12:20 PM
  2. create a simple program using linked list
    By tillu in forum C++ Programming
    Replies: 1
    Last Post: 08-31-2011, 12:53 AM
  3. Simple Array Program
    By Chemeng11 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2011, 08:09 PM
  4. create and populate create bidimensional array
    By darkducke in forum C Programming
    Replies: 0
    Last Post: 12-03-2010, 07:06 AM
  5. create a dll for C++ simple program and call from VB
    By motiz in forum C++ Programming
    Replies: 1
    Last Post: 01-09-2008, 04:54 AM

Tags for this Thread