Thread: Set of questions.

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    151

    Set of questions.

    I have a set of questions that may be asked for my exams. Can anyone tell me the solutions to this?

    Q1)
    Code:
    What is the problem with the following copy?
    char *s1 = “hello”;
    char *s2;
    strcpy(s2,s1);
    I know it is a segmentation fault, but why"?

    Q2)
    What are the two common problems of working with dynamic memory
    allocation and pointers?

    Q3)
    Explain top-down and bottom-up debugging techniques.

    Q4)
    Assume function static void f(void) whose definition is in body.c
    and its prototype is in body.h. If I include body.h in another C file, myprog.c,
    having the main function and calls function f, and try to run gcc body.c
    myprog.c –o myprog, should I expect any error message (assume that all
    programs are syntactically correct)? If yes, the error belongs to which step of
    compilation (i.e. preprocessing, linking, etc.)? Why?

    I know there will be an error. But why?


    Pleas reply.

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    The pointer s2 is just that, a pointer, it has no memory to store anything in.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Q1) when you use
    Code:
    char *junk = "Some text";
    compiler will allocate space to hold that text ("Some text") and put address of it into "junk". It can mark this space as const (so program crashes if it modifies it), or it can allocate is as usual (on stack?) so you can modify it, it depends on compiler (options?). But the point is that there's something somewhere allocated so s1 is valid pointer. s2 points to who knows what (probably NULL), and isn't associated with any allocated memory, so using that pointer is "being brave"
    Last edited by rasta_freak; 08-07-2008 at 03:11 PM.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    Q4) when function has "static" 'decoration' it means it's linkage is static to that source/object file - only code in .c file where it's defined can use it, and it's symbols aren't exported to linker so linker can't resolve 'requests' from other object files to call that function. Static function is local to that .c file. Is exists to ensure that locality, so noone else can see/use that function. If you write your version of printf(), and use it in only one .c file, but don't make it static, all other printf()s from other modules might get replaced with that function (depends on linking order, maybe linker will pick printf() from libc, maybe from your module).

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    151
    I did not understand the explanation for the 4th question.

  6. #6
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Quote Originally Posted by Ron View Post
    I did not understand the explanation for the 4th question.
    Do you know what 'static' means?

  7. #7
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Quote Originally Posted by Ron View Post
    I did not understand the explanation for the 4th question.
    I am very new to programming, but I have read a little about static. I believe what rasta_freak is saying is that the static function is usable only by the file it was defined in. Another linked file cannot call for use that function.

    Like I said, I am extremely new...so someone please let me know if I am right or wrong as I decided to write simply to see if I have actually understood this concept.

    Also, a linked file could still call a function from the first file that used said static function, right?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Dogmasur View Post
    I am very new to programming, but I have read a little about static. I believe what rasta_freak is saying is that the static function is usable only by the file it was defined in. Another linked file cannot call for use that function.
    Right.
    Quote Originally Posted by Dogmasur View Post
    Also, a linked file could still call a function from the first file that used said static function, right?
    If you mean that something in file2 called a function in file1 (which itself called a static function in file1), then yes.

  9. #9
    Registered User Dogmasur's Avatar
    Join Date
    Jul 2008
    Posts
    72
    Yeah...I understood something..hhaha.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Ron View Post
    Q2)
    What are the two common problems of working with dynamic memory
    allocation and pointers?
    Probably checking for NULL and memory leaks. But there are many possible problems.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    One being forgetting to de-allocate it after you have allocated it the required memory when the program has finished.
    Double Helix STL

  12. #12
    Registered User
    Join Date
    Aug 2008
    Posts
    2

    Need Malloc for s2

    s2 need to be malloc as it need some space to copy s1
    For anythin on C programming tutorials find here :
    http://c4tutorials.blogspot.com

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Using pointers without allocating them first is a common problem. Indeed, this is a very common error.
    http://cpwiki.sourceforge.net/Common...llocating_them
    There are more too, you can study and learn to avoid.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Using pointers without allocating them first is a common problem. Indeed, this is a very common error.
    http://cpwiki.sourceforge.net/Common...llocating_them
    There are more too, you can study and learn to avoid.
    Well, I'd say that's common only in beginners programs. I wouldn't classify any of my colleagues or the software engineers at the third parties that out company contracts to do work as beginners, but they still make occasional mistakes of:
    1. Not checking that allocation was successful before using the allocated memory [this usually happens when the scenario is slightly more complex than just calling malloc or new].
    2. Not freeing the memory after it is no longer needed.
    3. Use of memory after it has been freed.

    So whilst beginners may not realize the need for allocation (and thus we see it quite often here), but the three issues listed above are certainly much harder to keep track of and fulfill at all times.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course, I was referring to a common error seen on the boards. Yes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  2. C help for network animator
    By fastshadow in forum Tech Board
    Replies: 7
    Last Post: 03-17-2006, 03:44 AM
  3. Find all possible subset of a given set
    By Downnin in forum C++ Programming
    Replies: 7
    Last Post: 11-09-2005, 02:03 PM
  4. Program help
    By Trebor in forum C++ Programming
    Replies: 4
    Last Post: 06-04-2002, 03:19 PM