Thread: a couple of questions that have arrisen over the last 24 hours

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    a couple of questions that have arrisen over the last 24 hours

    rather than post 3 separate threads asking three single questions i thought i would group them altogether.

    1) what is the -> operator and when and where should i use it.?

    2) when and where should i use static it has been suggested by accomplished people on here to never use it but equally, other equally accomplished people have used it in their examples.

    3)what is malloc/calloc is it asking for more memory than has been set aside for the program or is it taking it from the memory already allocated. If it is the latter what is the difference between writing int my_array[n] where n has been entered by the use before that deceleration to using the appropriate malloc call. if malloc/calloc gets it from a separate area of memory can you do real damage by accidentally overwriting something important. in which case something best avoided until i really understand pointers and addresses

    many thanks
    coop

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by cooper1200
    1) what is the -> operator and when and where should i use it.?
    Given a pointer p to a struct type with a member foo, p->foo is equivalent to (*p).foo, so you would use it as syntactic sugar whenever you want to write (*p).foo.

    Quote Originally Posted by cooper1200
    2) when and where should i use static it has been suggested by accomplished people on here to never use it but equally, other equally accomplished people have used it in their examples.
    static is a storage class specifier. When using it to declare an object, it means that that object has static storage duration, i.e., its lifetime is the entire duration of the program, and it is actually initialised once prior to program startup. This can be problematic because it means that even though the object might be declared in a local context, it actually has global state, so you have to worry about the state being changed when you don't expect it (and consequently things like reentrancy and thread safety). A classic example would be trying to use strtok to parse a string, and while doing so, trying to use strtok again to parse a token extracted from the string, all in nested loops. Hence, it is wise to be cautious about declaring objects static, including static local variables.

    Besides specifying static storage duration, the static keyword also specifies that the identifier in the declaration has internal linkage. Linkage has to do with how the identifier refers to entities with respect to the translation unit in which it is declared. For example, if you declare a variable at file scope (i.e., a global variable), it automatically has static storage duration, but it has external linkage rather than internal linkage, so you can use the same variable name to refer to the same object from another translation unit. If you added static to the declaration, then it would have internal linkage, i.e., another file scope variable in another translation unit would refer to a different object. This turns out to be particularly advantageous in declaring helper functions since you basically make the helper functions local to the source file such that if you declare another helper function with the same name in another source file, there will be no name collision.

    Quote Originally Posted by cooper1200
    3)what is malloc/calloc is it asking for more memory than has been set aside for the program or is it taking it from the memory already allocated. If it is the latter what is the difference between writing int my_array[n] where n has been entered by the use before that deceleration to using the appropriate malloc call. if malloc/calloc gets it from a separate area of memory can you do real damage by accidentally overwriting something important. in which case something best avoided until i really understand pointers and addresses
    I would echo what someone else replied to you: don't worry. Generally, modern operating systems would have some kind of memory protection that makes it practically impossible for you to trash your system by abusing malloc calls: your program will likely just crash. You should be able to easily find online explanations for terms like "stack versus heap" with respect to malloc, and search for variable length array to understand the int my_array[n] thing.
    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

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    as regards to static decelerations have i understood this right
    Code:
    int  my_func(void)
    {
         static int a = 3;
    
        a += 2;
        return a;
    }
    int    main ()
    {
       int a = 5, b;
    
       b = my_func();
       printf("%d\n", b);  // would print out 7 not 5??
       // do some other stuff
    }
    sorry for the bad example
    coop

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    No, you got it wrong... it will print 5 (and this a declared in main is superfluous).

    Quote Originally Posted by cooper1200 View Post
    Code:
    int  my_func(void)
    {
      static int a = 3;
    
      a += 2;
      return a;
    }
    Here a is visible only inside my_func(), but it is a "global" variable (it's state exists as if the variable is global).
    This is different from:
    Code:
    // global static variable a
    static int a = 3;
    In this case a is global, but only to the current C file.

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    my bad i guess i should of continued with the program
    Code:
    int  my_func(void)
    {
         static int a = 3;
     
        a += 2;
        return a;
    }
    int    main ()
    {
       int a = 5, b;
     
       b = my_func();
       while (a>b)
      {
          printf(" a is %d", a);
      }
      printf(" b is no bigger than a");
    
    }
    the whole point of the example was trying to reiterate what laserlight said about declaring a variable as static was effectively declaring it as global (only one source file for this program) so re-declaring a is effectively a conflict like trying to declare two int x's
    coop

  6. #6
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    No, there is no conflict. The variable known as "a" inside "my_func" is a completely different variable from the one known as "a" inside "main". They just happen to have the same name.

    (By the way, the while loop will not iterate even once because in main both a and b are 5, and the expression "5>5" is false.)

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    thanks... it was just a silly example to demonstrate the scope of the static a i got it wrong i guess

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A couple questions...
    By Robbie in forum C Programming
    Replies: 10
    Last Post: 09-12-2011, 11:57 PM
  2. couple questions...
    By Rune Hunter in forum C# Programming
    Replies: 4
    Last Post: 10-03-2004, 10:57 AM
  3. Couple of questions
    By bobjohnson in forum C++ Programming
    Replies: 4
    Last Post: 07-29-2004, 09:43 AM
  4. A couple of Questions
    By johnnabn in forum C++ Programming
    Replies: 4
    Last Post: 02-24-2003, 10:10 PM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

Tags for this Thread