Thread: Realloc Quick Question.

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by killpoppop
    But as far as i can see no word is larger than 30 characters. But i'm obvious not on your wavelength =]
    Yes, I noticed that, so the problem lies elsewhere. Unlike MK27, I experienced a crash when running your code.

    The first thing I would do is to avoid dynamic memory allocation:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    void read( FILE *text )
    {
        int i = 0;
        int z;
        char temp[30];
    
        /* Reads in the words one at a time to an array */
        while( ( z = fgetc(text) ) != EOF )
        if( i < ( sizeof( temp ) - 1 ) && ( isalpha( z ) || z == '-' || z == '\'' ) )
        {
            temp[i] = toupper( z );
            i++;
        }
        else if( i != 0 )
        {
            temp[i] = '\0';
            printf( "%s %d\n", temp, strlen( temp ) );
            i = 0;
        }
    }
    
    int main( int argc, char *argv[] )
    {
    	FILE *text;
    	if( argc != 2 )
    	{
    		printf( "Incorrect Syntax" );
    	}
    	else
        {
    		text = fopen( argv[1], "r" );
    	    read( text );
        }
    	return 0 ;
    }
    Now, if you are to return a pointer from read() so that it can be linked into a linked list, you would need to use dynamic allocation instead of merely printing out the string (and in fact no printing should be done in read(), except maybe to print an error message).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #32
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by killpoppop View Post
    But i'm obvious not on your wavelength =]
    Maybe someone else can confirm what I just posted?
    edit: sorry laserlight.

    I guess that is strange. -Wall complained that strlen returns a size_t, not an int (to printf). (FC10/64)

    ps. I take that an MS Crash is just, like, a pop-up window???
    Last edited by MK27; 01-13-2009 at 11:32 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MK27
    I guess that is strange.
    Not really. Undefined behaviour is probably at work here, so it can crash on one machine and not on another.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #34
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by laserlight View Post
    Undefined behaviour
    There should be a special flashing red LED on the keyboard for this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #35
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    Thanks a lot laserlight. So if i want to use dynamic memory allocation i do it elsewhere rather than in the read function?

    Also could you explain
    Code:
    ( isalpha( z ) || z == '-' || z == '\'' )
    quickly please?
    Last edited by killpoppop; 01-13-2009 at 12:25 PM.

  6. #36
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by killpoppop
    So if i want to use dynamic memory allocation i do it elsewhere rather than in the read function?
    No, I think that it is correct to do it in the read function, and then later you let the linked list do the memory management. One approach is to make read() read in just one word. Another approach is to pass a pointer to the head of the linked list to read(), then read will append to the linked list while it reads in the words. The second approach is more like what you are doing now.

    Quote Originally Posted by killpoppop
    Also could you explain
    Do you understand what this code does?
    Code:
    if( ( ( 65 <= z ) && ( z <= 90 ) ) || ( ( 97 <= z ) && ( z <= 122 ) ) || ( z == 45 ) || ( z == 39 ) )
    My version does the same but is more readable since it can easily be read as: "if z is an alphabetic character, a dash or a single quote". With your version, a reader who does not have the ASCII table at his or her fingertips must look it up. (It also is less portable since it assumes ASCII, but that is more of a theoretical concern these days.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #37
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    Yep i got your code just never came across isalpha before, its awesome. I just misread '\'' as " \ " as my text editor puts apostrophe's extremely close together.

    Thanks for all the help =]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM