Like Tree10Likes

my good friend Mr Seg Fault

This is a discussion on my good friend Mr Seg Fault within the C Programming forums, part of the General Programming Boards category; Originally Posted by std10093 Ok here is my approach Notice that i did not use realloc.When you think of an ...

  1. #16
    Registered User
    Join Date
    Jul 2012
    Location
    Australia
    Posts
    242
    Quote Originally Posted by std10093 View Post
    Ok here is my approach

    Notice that i did not use realloc.When you think of an approach that uses realloc, think twice.If not used smartly enough, it can be a very heavy operation.

    Hope my comments help
    That is scholarship winning code. A+

    Don't forget to free your memory.
    IDE: Code::Blocks | Compiler Suite for Windows: TDM-GCC (MingW, gdb)

  2. #17
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,650
    Quote Originally Posted by cfanatic View Post
    That is scholarship winning code. A+
    Haha, thanks , but forgetting to free my memory is a big mistake! Bravo for your observation

    Quote Originally Posted by cfanatic View Post
    Don't forget to free your memory.
    So right before return 0; (where main terminates at these two lines of code)
    Code:
    for(i = 0 ; i < answerCounter ; i++)
            free(answer[i]);
    Hmm.....

    If you notice we make one pass to print and one pass to free....Thus we execute these loops at aggregate 20 times... If we had as size of array 1000 instead of 10, we would execute these two 2000 times...
    So if we have as size of array answer the value n, we will do a pass of 2*n ... so do it in one pass to execute n times
    Code:
    for(i = 0 ; i < answerCounter ; i++)
        {
            printf("%s\n",answer[i]);
            free(answer[i]);
        }
    And because this n is how many times our while loop is going to be executed, we have to consider the above

    Remember that the time complexity of our algorithm is determined by the heaviest operation!
    cfanatic likes this.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-07-2007, 06:49 PM
  2. for a friend
    By Ajsan in forum C++ Programming
    Replies: 4
    Last Post: 02-25-2004, 10:04 AM
  3. friend function and friend classes...usage question??
    By actionbasti in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2003, 09:53 PM
  4. friend
    By laasunde in forum C++ Programming
    Replies: 3
    Last Post: 11-30-2002, 09:15 AM

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