Like Tree1Likes

Differnce between C and Java returning an Object reference/pointer

This is a discussion on Differnce between C and Java returning an Object reference/pointer within the C Programming forums, part of the General Programming Boards category; Some posts from this thread were moved to: Java standard Originally Posted by sumit180288 Can you tell me what is ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,265
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,866
    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
    22,265
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    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,866
    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
    22,265
    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 03:35 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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.
    sumit180288 likes this.

  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.

Page 2 of 2 FirstFirst 12
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, 03:13 AM
  2. Returning an object with a dynamic pointer
    By maxsthekat in forum C++ Programming
    Replies: 11
    Last Post: 09-16-2009, 02: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, 09: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, 06:27 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21