Thread: Coding a check for array overflow? or how to have no input limit.

  1. #16
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Quote Originally Posted by MonkeyTennis View Post
    Yes we have been fought pointers, however I understand how they work but not how to use them properly, that's the problem with cs50 through edx, its very disjointed. Should I be allocating s with a pointer and malloc before setting input to s?
    What do you think that would gain for you?

    There are a couple of good reasons to dynamically allocate data:
    1. Large data structures, because generally in practice you can allocate more space with malloc() than would be permitted in a local array.
    2. Data that needs to persist outside of the scope in which it was originally allocated.
    3. Data whose size isn't known until runtime (with C99 this isn't absolutely necessary anymore).

    If there's no good reason to make a pointer and allocate memory yourself, don't do it. It adds complexity in that you need to guarantee that the memory is deallocated when no longer needed.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  2. #17
    Registered User
    Join Date
    Aug 2015
    Posts
    11
    Thanks for the great replies guys, learning a lot.

    the only reason I did sizeof(char) is because the lecturer said you should just incase someone runs the program on a different computer and it allocates memory sizes differently, that way it will allocate the correct amount for that computer?

    if with s != NULL I am checking the memory and not the string, how would I check the string please.

    also I'm very happy to take suggestions on a good C book.

    would defining a static array allow me to used free(array) as you do with malloc?

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    4,482
    the only reason I did sizeof(char) is because the lecturer said you should just incase someone runs the program on a different computer and it allocates memory sizes differently, that way it will allocate the correct amount for that computer?
    It doesn't matter what computer, operating system, or compiler you use size0f(char) is guaranteed to be 1 by the standard. But with anything other than a char your instructor is correct.

    if with s != NULL I am checking the memory and not the string, how would I check the string please.
    What do you mean by checking the string?

    would defining a static array allow me to used free(array) as you do with malloc?
    No, but you don't need to free a static allocated array. It will be freed automatically when it goes out of scope.

    Jim

  4. #19
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    699
    Quote Originally Posted by MonkeyTennis View Post
    if with s != NULL I am checking the memory and not the string, how would I check the string please.

    also I'm very happy to take suggestions on a good C book.
    I would initialize the string first to know it only contained '\0' bytes.
    Code:
    char s[50] = "";
    Then after getting the string using scanf() or better, fgets(), then you can check the length of the string using strlen(), and go on from there.

    As for a good book, choose one of the two I reccomend:

    C Programming, A Modern Approach, K.N. King Author
    Covers the C89 and C99 Standards, but not C11

    C Primer Plus, 6th Edition, Stephen Prata Author

  5. #20
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Quote Originally Posted by rstanley View Post
    ...
    As for a good book, choose one of the two I recommend:

    C Programming, A Modern Approach, K.N. King Author
    Covers the C89 and C99 Standards, but not C11

    C Primer Plus, 6th Edition, Stephen Prata Author
    For C Programming, A Modern Approach, by K.N. King, I believe only the second edition tries to fully cover C99. I might be wrong, but I think the first edition covered C++ stuff whereas in the second edition, the author replaced the C++ stuff with more C99 stuff. For a list of differences between the first and second edition, you can visit the authors site and look at his preface. It goes in depth at the differences. I think the 2nd edition would be a better choice than the first for someone trying to learn C.

    Preface to C Programming: A Modern Approach - Second Edition

  6. #21
    Registered User
    Join Date
    Mar 2014
    Location
    Corning, New York, USA
    Posts
    96
    Quote Originally Posted by Spork Schivago View Post
    For C Programming, A Modern Approach, by K.N. King, I believe only the second edition tries to fully cover C99. I might be wrong, but I think the first edition covered C++ stuff whereas in the second edition, the author replaced the C++ stuff with more C99 stuff. For a list of differences between the first and second edition, you can visit the authors site and look at his preface. It goes in depth at the differences. I think the 2nd edition would be a better choice than the first for someone trying to learn C.

    Preface to C Programming: A Modern Approach - Second Edition
    Boy do I feel stupid, you linked to the 2nd edition! Sorry about all that!

  7. #22
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    699
    Will make sure I say "2nd Edition" in the future! ;^)

  8. #23
    Registered User
    Join Date
    Aug 2015
    Posts
    11
    Book on order, cheers gents! I'll be sure to pour over it. It actually makes sense to initialize s like that first. Talking to people makes learning so much easier.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I check what causes a stack overflow?
    By MutantJohn in forum C++ Programming
    Replies: 11
    Last Post: 09-24-2013, 05:00 PM
  2. Limit input to 10 numbers
    By Qshine in forum C Programming
    Replies: 5
    Last Post: 02-11-2013, 09:19 PM
  3. time limit for user input
    By dudeomanodude in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 03:01 PM
  4. Overflow check
    By tezcatlipooca in forum C Programming
    Replies: 5
    Last Post: 12-23-2006, 12:04 PM
  5. How to check 32 bit limit
    By maven in forum C Programming
    Replies: 3
    Last Post: 09-28-2006, 02:47 AM