why i dong get accsesing null memory error stuff..

This is a discussion on why i dong get accsesing null memory error stuff.. within the C Programming forums, part of the General Programming Boards category; if at some point ,my string is "aa" and my pointer points to the first "a" then i go 2 ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    why i dong get accsesing null memory error stuff..

    if at some point ,my string is "aa"
    and my pointer points to the first "a"
    then i go 2 places fowrward
    so it goes "out of band"(java term)
    why i dont get an error in a compiler
    Last edited by transgalactic2; 03-24-2009 at 12:57 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    If your string is "aa", then it has a null terminator, so dereferencing a pointer to the char at index 2 is perfectly fine. Also, having a pointer that points one past the last element of an array is also perfectly fine, and is in fact useful for iterating over an array (to keep track of the end point).

    EDIT:
    Incidentally, "out of band" sounds more like a networking or telecommunications term than a Java term. You are probably thinking about "out of bounds", but that term is not specific to Java.
    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

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    no \0 char
    i defined it as
    char* st="aa";

    why i dont get out of bounds
    ??

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Quote Originally Posted by transgalactic2
    no \0 char
    i defined it as
    char* st="aa";
    Then it has a null terminator. Note that you should write it as:
    Code:
    const char* st = "aa";
    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. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    why const??
    so if i write it like this then it adds \0 in the end??

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,705
    >> (java term)
    >> why i dont get an error in a compiler

    Because C is not Java. when you program in C you must practice "safe driving" and debug with separate tools like valgrind.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Quote Originally Posted by transgalactic2
    why const??
    Because the pointer points to the first char of a string literal, and string literals may not be modified.

    Quote Originally Posted by transgalactic2
    so if i write it like this then it adds \0 in the end??
    Yes.
    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

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    there is no such thing as string in C we are talking about
    array of chars and the last char is \0

    so what you are saying is if i have
    Code:
    char *st={'a','c'};
    (which could be an array of chars)
    then we cant do
    *(st+1)='h';
    because its a const??

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Quote Originally Posted by transgalactic2
    there is no such thing as string in C
    You are mistaken. In C, a string is "a contiguous sequence of characters terminated by and including the first null character".

    Quote Originally Posted by transgalactic2
    so what you are saying is if i have
    That is not a string. That is a pointer to the first element of an array of two chars.

    Quote Originally Posted by transgalactic2
    then we cant do
    *(st+1)='h';
    because its a const??
    Yes.

    EDIT:
    Actually, that syntax is wrong. It seems that what it really does is convert 'a' to pointer to char, or something like that.
    Last edited by laserlight; 03-24-2009 at 01:28 PM.
    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. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i ran this code
    and it worked fine
    i didnt get an error
    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    
    	char str1[2]={'a','b'};
    	 
    	char* str=str1;
    	*(str+1)='h';
    		return 0;
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Quote Originally Posted by transgalactic2
    i ran this code
    and it worked fine
    i didnt get an error
    Why did you think you would get an error? Note that your pointer now points to the first element of an array that is modifiable.
    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

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    because you said
    "pointer points to the first char of a string literal, and string literals may not be modified."

    thats what i did
    i changed the first char too
    and it works fine
    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    
    	char str1[2]={'a','b'};
    	 
    	char* str=str1;
    	*(str)='h';
    		return 0;
    }

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,705
    Let me be painstakingly clear.

    If you have an array of char like
    Code:
    str = {'a', 'a'};
    and you do something like, *(str+1) = 'b'; there will be no error because you are in bounds. Accessing anything greater than the first index is out of bounds and is undefined behavior hence.

    Likewise when I said that C has no string type in another thread, I only said that out of frustration because you wouldn't allow people to explain the difference between pointers and arrays. There are certainly types that hold strings, but those types are not called "string". Try not to be too literal though. I'm sorry if I caused confusion.

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    laser light said:
    "pointer points to the first char of a string literal, and string literals may not be modified."
    so this code works because its not ending with \0
    ??
    Code:
    #include <stdio.h>
    #include <string.h>
    int main()
    {
    
    	char str1[2]={'a','b'};
    	 
    	char* str=str1;
    	*(str)='h';
    		return 0;
    }
    ?

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But in this case
    Code:
    {'a', 'b'}
    are not part of a string literal.

    Also note that not ALL compilers generate code that fail when accessing string literals - the standard only says that string literals mustn't be modified, not what actually happens - that is part of the "undefined land", which as I said in another thread "Makes Alice in Wonderland seem quite normal". It may crash, it probably won't give you a nice error message, and it may appear to work just fine (until you modify the right bit in the right way, e.g. concatenating a string, and then some other string that got overwritten is modified as a result).

    --
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Syntax Error??
    By Kennedy in forum C Programming
    Replies: 8
    Last Post: 09-06-2006, 11:04 AM
  3. Why am I getting these errors??
    By maxthecat in forum Windows Programming
    Replies: 3
    Last Post: 02-03-2006, 12:00 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. button 'message'
    By psychopath in forum Windows Programming
    Replies: 12
    Last Post: 04-18-2004, 09:57 AM

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