Thread: Differnce between C and Java returning an Object reference/pointer

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Some posts from this thread were moved to: Java standard

    Quote Originally Posted by sumit180288
    Can you tell me what is the difference between these two codes:
    You can observe the difference yourself: in the former, name is a pointer that points to the first character of a string literal; in the latter, name is an array of 6 char.
    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

  2. #17
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    In Salem's example you are returning a pointer, which since C is pass by value, gets copied. That pointer points to a string literal which stays present for the life of the program.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #18
    Registered User
    Join Date
    Sep 2011
    Location
    India
    Posts
    27
    Moreover the array declaration
    Code:
    char a[]="Sumit";
    Here a also points to the string literal.
    What is the difference?

    Quote Originally Posted by AndrewHunter View Post
    In Salem's example you are returning a pointer, which since C is pass by value, gets copied. That pointer points to a string literal which stays present for the life of the program.

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sumit180288
    returning a string literal would be something like return "Sumit";
    Yes. However, returning a pointer to the first character in a string literal would have the same effect.
    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

  5. #20
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Be careful. It does not point to a string literal.

    The "Sumit" here is just one way of writing:
    Code:
     char a[] = {'S','u','m','i','t',0};
    Saving ones fingers does not change the semantics of variable storage. The array (meaning its contents) are stored locally in the scope of the nearest block.

  6. #21
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by sumit180288 View Post
    Moreover the array declaration
    Code:
    char a[]="Sumit";
    Here a also points to the string literal.
    What is the difference?
    Again, in that case, you are copying the character literal into a local array of bytes.

  7. #22
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    I would like to add that we are essentially "playing with fire" here. No comments made can be verified on all implementations, since Java doesn't have one. This entire conversation should be moved to the tech board.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #23
    Registered User
    Join Date
    Sep 2011
    Location
    India
    Posts
    27
    @whiteflags and @all dear members

    Can u tell me the difference in the mechanisms of memory storage in case of a string literal and the one mentioned by @whiteflags i.e.

    Code:
    char a[] = {'S','u','m','i','t',0};
    Is String literal not stored as a[] is stored?

    Quote Originally Posted by whiteflags View Post
    Be careful. It does not point to a string literal.

    The "Sumit" here is just one way of writing:
    Code:
     char a[] = {'S','u','m','i','t',0};
    Saving ones fingers does not change the semantics of variable storage. The array (meaning its contents) are stored locally in the scope of the nearest block.

  9. #24
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sumit180288
    Is String literal not stored as a[] is stored?
    What do you mean?

    EDIT:
    Oh, I think I understand. Let me just say this: the string literal has static storage duration. Its lifetime is for the entire program. A local array's lifetime, on the other hand, is only until its scope ends, unless it has say, static storage duration due to being declared static.
    Last edited by laserlight; 09-07-2011 at 02:35 AM.
    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

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char a[] = { "stuff" };
    That declares a local variable (assuming this in a function) with the name of a, that is an array of characters, whose size is determined by the number of characters provided as the initializer.
    Code:
    char *a = "stuff";
    This declares a variable named a that is a pointer to a character, and that points at a string literal.

    They are two different things.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #26
    Registered User
    Join Date
    Sep 2011
    Location
    India
    Posts
    27
    @laserlight.
    Nice thoughts there. Could you please tell me a link or a reference where i can get these nice facts about the scope of a literal.

    Quote Originally Posted by laserlight View Post
    What do you mean?

    EDIT:
    Oh, I think I understand. Let me just say this: the string literal has static storage duration. Its lifetime is for the entire program. A local array's lifetime, on the other hand, is only until its scope ends, unless it has say, static storage duration due to being declared static.

  12. #27
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sumit180288 View Post
    @Salem. Sorry to say i didn't see the pointer version. I thought u are asking about the array version.
    I am also not knowing why this verison works?
    Anyone plz clear this doubt.

    Regards
    Ok... all the way back to your first message...

    The Java version works because Java has a native string type that is "reference counted". That is the memory occupied by the string is not released if there is anything referring to it, like in your first example.

    However, C does not have a string type (despite some who argue to the contrary). C programs use character arrays to store groups of characters with a terminating null. We call it a string but it isn't really a string (in the Pascal/Basic/Java sense) at all. It is nothing but an array, that gets destroyed in the stack cleanup when the function exits, leaving you with a pointer to garbage.

    Why does the one in message 15 work? Because the literal string "Sumit" is not created or stored in the program stack area used by the function, therefore it's address remains valid after the function exits... that is, it doesn't get invalidated by the stack cleanup.


    This is one of the pitfalls of comparing languages... as they say "This ain't That"... and it's a mistake to try to understand one by knowing the other.

  13. #28
    Registered User
    Join Date
    Sep 2011
    Location
    India
    Posts
    27
    @Tater
    Correctly said... But i will one more argument in support.
    Every thing is an object in Java (including arrays) i.e. Memory for these objects are allocated dynamically out of heap.
    Hence as u mentioned It remains there until all references to that memory location dies out. (i.e. reference count becomes zero).
    This is internally performed internally using the GC (Garbage collector) which is a very nice functionality provided by Java and we dont have to worry about
    freeing up this dynamic heap memory. It is automatically managed by JDK.


    Memory allocated out of heap space will remain for a lifetime which is till program ends or if memory is freed forcefully.

    1. The memory which is allocated using malloc() etc function
    2. String literal. (Dynamic allocation not done but literals have static scope)
    etc.

    @ALL PLZ ADD ANYTHING ELSE WHERE MEMORY IS DYNAMICALLY ALLOCATED.

    Quote Originally Posted by CommonTater View Post
    Ok... all the way back to your first message...

    The Java version works because Java has a native string type that is "reference counted". That is the memory occupied by the string is not released if there is anything referring to it, like in your first example.

    However, C does not have a string type (despite some who argue to the contrary). C programs use character arrays to store groups of characters with a terminating null. We call it a string but it isn't really a string (in the Pascal/Basic/Java sense) at all. It is nothing but an array, that gets destroyed in the stack cleanup when the function exits, leaving you with a pointer to garbage.

    Why does the one in message 15 work? Because the literal string "Sumit" is not created or stored in the program stack area used by the function, therefore it's address remains valid after the function exits... that is, it doesn't get invalidated by the stack cleanup.


    This is one of the pitfalls of comparing languages... as they say "This ain't That"... and it's a mistake to try to understand one by knowing the other.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning object by value or smart pointer
    By mrpringle in forum C++ Programming
    Replies: 3
    Last Post: 08-29-2010, 02:13 AM
  2. Returning an object with a dynamic pointer
    By maxsthekat in forum C++ Programming
    Replies: 11
    Last Post: 09-16-2009, 01:52 PM
  3. trouble returning an object by reference
    By fisheromen1031 in forum C++ Programming
    Replies: 14
    Last Post: 02-19-2008, 11:10 PM
  4. Object reference not set to instance of an object
    By patricio2626 in forum C++ Programming
    Replies: 6
    Last Post: 10-26-2006, 08:12 AM
  5. ERRPR: Object reference not set to an instance of an object
    By blackhack in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2005, 05:27 PM

Tags for this Thread