Thread: Trying to up my game: slightly more advanced questions

  1. #1
    Registered User
    Join Date
    Dec 2018
    Posts
    8

    Question Trying to up my game: slightly more advanced questions

    Hey, what's up

    So I've been writing less in C++ lately, and more in C; this is for a number of reasons, but the more I do it, the more I realize there's a lot I don't know (yet, lol). When I do any kind of searching for advanced C tutorials, I get a lot of C++/C#, and also a lot of Hello World stuff I learned years ago. So I was wondering:

    1. What is the best way to get user input? I understand how buffer overflows work (except that somehow hackers can use them to run arbitrary code - that part is still voodoo witchcraft to me lol), but there doesn't seem to be a simple straight answer to this. In C++ we have "streams" (stringstream, ifstream and ofstream), and I think that's a great solution, but in C? I've heard of something called a "Pascal String", where it's got a max of 255 characters and the first is the length of the string or something like that. In Windows, there is a "scanf_s" function, which is great but not cross-platform, so I'm not sure if there is a single standard way to do it.

    2. This could be related to #1, or maybe part of the solution: When and how would you want/need to allocate memory on the heap? I could Google malloc and free, find tutorials on it etc. so I'm not necessarily asking HOW... but up till now I've been able to do everything on the stack. I've built games in SDL, Allegro and others; I've written all kinds of command-line programs, etc.; but never once have I ever had to go there. So what are the scenarios where you would need to? What kinds of pitfalls should I be aware of?

    3. What is the go-to resource for these types of questions in the future? I have a copy of the K&R, which I know most hardcore C devs call "the Bible"; but most of what I've read in there is pretty basic stuff; granted, I haven't read it all from cover to cover (maybe it gets into this toward the end?)... but in C#/VC++ we have MSDN. Python has python.org, PHP has php.net, etc. There's even a cplusplus.com (or was it .org?) but I haven't found any equivalent for C. I would like to learn more about best practices, tricks and hacks and everything I can about this awesome language.

    Thanks!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I usually just use a fixed-size array as a buffer and transfer everything to a dynamic array. realloc() is awesome. I then use sscanf() on that, appending a "%n" at the end of the format to know how far ahead I should jump next.
    Devoted my life to programming...

  3. #3
    Guest
    Guest
    Quote Originally Posted by Geek on Skates View Post
    2. This could be related to #1, or maybe part of the solution: When and how would you want/need to allocate memory on the heap? I could Google malloc and free, find tutorials on it etc. so I'm not necessarily asking HOW... but up till now I've been able to do everything on the stack. I've built games in SDL, Allegro and others; I've written all kinds of command-line programs, etc.; but never once have I ever had to go there. So what are the scenarios where you would need to? What kinds of pitfalls should I be aware of?
    Stack memory is limited, so while you may have been able do without heap allocations so far, this doesn't apply to every situation. The libraries you have been using will have done multiple heap allocations to accomplish their functionality. Another reason for heap allocations is object lifetime. You may want to refer to an object throughout the program's lifetime, and pointing to one with temporary (local) scope isn't gonna work in that case.

  4. #4
    Registered User
    Join Date
    Dec 2018
    Posts
    8
    Hey, thanks for the great info! Could I bug you for some examples?
    * About the string thing, I've used fixed-sized arrays as buffers, but never messed with realloc (and actually never heard of it).
    * Now when you say "object" lifetime, what is an object in C? I kinda thought C was not object-oriented at all. Do you mean like a global variable that you want to access from multiple functions/files?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In this context an "object" is "a contiguous region of memory holding a value of some type". All objects have a lifetime, which depends on its storage duration and for objects with automatic storage duration, on its scope.

    Accessing a global variable from multiple files has more to do with linkage than lifetime, i.e., the global variable has external linkage, so it can be referred to outside of the translation unit (source file + headers) where it was defined.
    Last edited by laserlight; 12-09-2018 at 04:24 PM.
    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

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by Geek on Skates View Post
    * About the string thing, I've used fixed-sized arrays as buffers, but never messed with realloc (and actually never heard of it).
    Sure. realloc() can change the size of an array that has been previously allocated with malloc/calloc/realloc. Here's a simple example that takes a string from the user, without size limitations, in this case I use a single char as a buffer because it's easier:
    Code:
    char *entireLine;
    int lineSize, c; 
    
    lineSize = 0;
    entireLine = NULL;
    while ((c = getchar()) != '\n' && c != EOF) {
        entireLine = realloc(entireLine, ++lineSize);
        entireLine[lineSize - 1] = c;
    }
    In some systems, realloc() can fail and return NULL, but I tend to ignore that case unless I'm making bulletproof code.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. advanced C practice and questions.
    By Dave11 in forum C Programming
    Replies: 3
    Last Post: 03-21-2014, 05:22 PM
  2. Advanced (I think) pointer questions.
    By DanielB33 in forum C Programming
    Replies: 6
    Last Post: 06-14-2013, 12:39 PM
  3. Advanced connect four game
    By Ion Blade in forum C++ Programming
    Replies: 10
    Last Post: 07-28-2002, 07:52 AM
  4. Slightly complicated array&memory questions
    By Boksha in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2002, 11:35 AM

Tags for this Thread