Comments needed 
Author Message
 Comments needed

An access violation error happens as a result of this code, is it because
that gets trys to store the information at a
offset of zero?
if not may i get an explanation.
 thx js

#include<stdio.h>

int main(void)
{

  char *str = NULL;

gets(str);

printf("%s",str);

return 0;

Quote:
}



Sun, 02 Jan 2000 03:00:00 GMT  
 Comments needed

Quote:

>An access violation error happens as a result of this code, is it because
>that gets trys to store the information at a
>offset of zero?
>if not may i get an explanation.

Whether it is 0 or not is implementation defined. It would seem you
initialized str, so perhaps there is no undefined behavior. Also, gets
could test to see if it got passed a null pointer, so your program
could conceivably have died in the call to printf. The main point is
str should be set to point to valid storage, which it does not. (I am
also curious if a pointer set to null is still considered uninitialized.)

Quote:
>#include<stdio.h>

>int main(void)
>{

>  char *str = NULL;

>gets(str);

>printf("%s",str);

>return 0;
>}

--
Craig

Manchester, NH
So what? I got my hands dirty to achieve our goal. -- Paul Mesken


Mon, 03 Jan 2000 03:00:00 GMT  
 Comments needed


Quote:
> An access violation error happens as a result of this code, is it because
> that gets trys to store the information at a
> offset of zero?
>   char *str = NULL;
> gets(str);

Jayson, the memory starting at location '0' doesn't belong to you, so you
cannot put information there. If you want to use memory, you must ask for it
either with an array definition or with dynamic allocation. Also, gets() is a
terrible function, since you cannot control how many chars are input. It is
much better to use fgets(str, len, stdin);

char str[128];
fgets(str, sizeof(str), stdin);

or

#include <stdlib.h>

char *str = malloc(128);
if(str)
{
   fgets(str, 128, stdin);
...
   free(str);

Quote:
}



Mon, 03 Jan 2000 03:00:00 GMT  
 Comments needed



Quote:

>>An access violation error happens as a result of this code, is it because
>>that gets trys to store the information at a
>>offset of zero?
>>if not may i get an explanation.

>Whether it is 0 or not is implementation defined. It would seem you
>initialized str, so perhaps there is no undefined behavior.

Trying to write through a pointer that doesn't point to a valid object
(such as a null pointer) results in undefined behaviour.

Quote:
>Also, gets
>could test to see if it got passed a null pointer, so your program
>could conceivably have died in the call to printf. The main point is
>str should be set to point to valid storage, which it does not. (I am
>also curious if a pointer set to null is still considered uninitialized.)

Yes, it is initialised. If it weren't then accessing the value of the
pointer at all (e.g. for comparison against another pointer) would result
in undefined behaviour.

--
-----------------------------------------


-----------------------------------------



Mon, 03 Jan 2000 03:00:00 GMT  
 Comments needed

#include<stdio.h>

int main(void)
{
/* You tried to assign NULL to a memory location  where *str points to,
but  */
/* no memory location was assigned to it.  This is what could be
done:       */

char *str;

if((str=malloc(81))==NULL)
        {
        fprintf(stderr, "Shit, out of memory");
        exit(1);
        }

gets(str);
printf("%s",str);

return 0;

Quote:
}



Mon, 03 Jan 2000 03:00:00 GMT  
 Comments needed

Another thing:
        *str does point to a memory location, you just don't know where.  This
is your access violation.  You should not write to a memory location
that does not belong to your program.  You could wipe out part of your
operating system.  :-(



Mon, 03 Jan 2000 03:00:00 GMT  
 Comments needed

Quote:

>#include<stdio.h>
>int main(void)
>{
>/* You tried to assign NULL to a memory location  where *str points to,
>but  */
>/* no memory location was assigned to it.  This is what could be
>done:       */
>char *str;

[The original code was "char *str = NULL;"]

and obviously does not assign NULL to a memory location where *str
points to. Memory _is_ assigned to str, and the above statement is
a valid initialization, just as in "int fred = 2;".

Wrong diagnosis, right cure:

Quote:
>if((str=malloc(81))==NULL)
>    {
>    fprintf(stderr, "Shit, out of memory");
>    exit(1);
>    }

Kurt

--
| Kurt Watzka                             Phone : +49-89-2180-6254



Mon, 03 Jan 2000 03:00:00 GMT  
 Comments needed

Quote:
> #include<stdio.h>

> int main(void)
> {

>   char *str = NULL;

> gets(str);

> printf("%s",str);

> return 0;
> }

Hi,
 When you have a pointer, you MUST always
allocate space for it before you can use
it.  There is two ways that you can do this:
        1).  You can assume that there is
             a length limit on your string, let
             say 20 bytes (you can have more),
             then you allocate for your string:
             it's something like this:
                str = (char *) malloc(num_of_char * sizeof(char));
                Where num_of_char is the maximum of characters that
                your pointer can hold.
             After this point, you have a valid string.
        2).  You can declare an temp array, and read a string
             into your temp array, and then allocate space needed
             to hold your string for your pointer, and assign your
             pointer pointing to the string that you just read in.
             it's something like this:
                char    temp_str[max_length];
                char    *str;

                gets(temp_str);
                str = (char *) malloc(strlen(temp_str) + 1);
                strcpy(str,temp_str);

                where max_length is the maximum number char you can
                have.
                You need <string.h> for strcpy
                + 1 is for the null byte.

Good luck,



Mon, 03 Jan 2000 03:00:00 GMT  
 Comments needed

Quote:

>Another thing:
>    You should not write to a memory location
>that does not belong to your program.  You could wipe out part of your
>operating system.  :-(

Ahh, don't you mean 'program loader'?

Operating systems provide protection schemes which prevent exactly this problem.

--

``Not only is UNIX dead, it's starting to smell really bad.'' -- rob



Tue, 04 Jan 2000 03:00:00 GMT  
 Comments needed

# I am also curious if a pointer set to null is still considered uninitialized

Of course it is initialized. Initialized (IMHO) means "has been assigned a
value which I can tell" as opposed to "indeterminate, which I can't tell",
like in unitialized automatic variables.

Null pointers may be sanely passed to a lot of functions (fflush,
realloc, system, ...) Null pointers are conceptually pointers
guaranteed not to point to any other object. This property makes
them so useful as an "out-of-band" indicator for pointers.

Regards,

        Jens
--
Jens Schweikhardt  http://www.uni-stuttgart.de/People/schweikhardt/home.html
SIGSIG -- signature too long (core dumped)



Tue, 04 Jan 2000 03:00:00 GMT  
 Comments needed

Quote:


># I am also curious if a pointer set to null is still considered uninitialized

>Of course it is initialized. Initialized (IMHO) means "has been assigned a
>value which I can tell" as opposed to "indeterminate, which I can't tell",
>like in unitialized automatic variables.

That was a somewhat confused post from me. As Lawrence pointed out,
they must be initialized or you couldn't compare them to anything
without invoking undefined behavior.

Quote:
>Null pointers may be sanely passed to a lot of functions (fflush,
>realloc, system, ...)

That is true, in 7.1.7 we read

    "Each of the following statements applies unless explicity
     stated otherwise in the detailed descriptions that follow.

[That is an important clause which tripped me up when I first began
repling to your post.] :-)

    If an argument to a function has an invalid value (such as a
    value outside the domain of the function, or a pointer outside
    the address space of the program, or a null pointer), the
    behavior is undefined."

So, some library functions can be called with a null pointer *if
explicitly stated* in their description.

I know that the above applies to the standard library, but is it ok
to call your own functions with a null pointer? It seems if you had
a function that validated a region of memory, handling getting tossed
a null pointer would be expected behavior. The distinction would be
it *is* ok to call non standard library functions with null pointers,
however, about all you could do with them is test for NULL.

[Refering back to my origional post...]
gets, for example, expects to get a pointer to some valid storage. By
setting a pointer to NULL, it is initialized -- but it has not been set
to point to valid storage. (Mixing those two concepts up is what lead
to my silly statement about a NULL pointer perhaps being unitialized.
*Yes*, it is initialized, but *no*, it doesn't point to anything. *That*
is the actual state of affairs.)

--
Craig

Manchester, NH
So what? I got my hands dirty to achieve our goal. -- Paul Mesken



Tue, 04 Jan 2000 03:00:00 GMT  
 Comments needed


[zap]

# >Null pointers may be sanely passed to a lot of functions (fflush,
# >realloc, system, ...)
#
# That is true, in 7.1.7 we read
#
#     "Each of the following statements applies unless explicity
#      stated otherwise in the detailed descriptions that follow.
[zap]
#     If an argument to a function has an invalid value (such as a
#     value outside the domain of the function, or a pointer outside
#     the address space of the program, or a null pointer), the
#     behavior is undefined."
#
# So, some library functions can be called with a null pointer *if
# explicitly stated* in their description.
#
# I know that the above applies to the standard library, but is it ok
# to call your own functions with a null pointer?

It definitely should be.

# It seems if you had
# a function that validated a region of memory, handling getting tossed
# a null pointer would be expected behavior. The distinction would be
# it *is* ok to call non standard library functions with null pointers,
# however, about all you could do with them is test for NULL.

Do you mean that I'm getting undefined behaviour with

#include <stdio.h>

void foo (void *p)
{
  if (p == NULL) {
    printf ("Got NULL (%p), core meltdown sequence initiated.\n", (void *)p);
  else {
    printf ("Got something non-NULL (%p), all engines running.\n", (void *)p);
  }

Quote:
}

and a call to foo (NULL)?

        Jens
--
Jens Schweikhardt  http://www.uni-stuttgart.de/People/schweikhardt/home.html
SIGSIG -- signature too long (core dumped)



Wed, 05 Jan 2000 03:00:00 GMT  
 Comments needed

Quote:


># So, some library functions can be called with a null pointer *if
># explicitly stated* in their description.
>#
># I know that the above applies to the standard library, but is it ok
># to call your own functions with a null pointer?

>It definitely should be.

># It seems if you had
># a function that validated a region of memory, handling getting tossed
># a null pointer would be expected behavior. The distinction would be
># it *is* ok to call non standard library functions with null pointers,
># however, about all you could do with them is test for NULL.

>Do you mean that I'm getting undefined behaviour with

>#include <stdio.h>

>void foo (void *p)
>{
>  if (p == NULL) {
>    printf ("Got NULL (%p), core meltdown sequence initiated.\n", (void *)p);
>  else {
>    printf ("Got something non-NULL (%p), all engines running.\n", (void *)p);
>  }
>}

>and a call to foo (NULL)?

There are a number of possible issues here. I would say, *assuming it
is ok to call printf with a null pointer*, implementation defined
behavior kicks in and printf must dutifully represent the null pointer.

If a null pointer gets abused in some manner inside of a function,
and you call that function with a null pointer, that would result in
whatever consequences being incurred when you called the function,
including undefined behavior. You could argue that the offending
behavior occurs in the function, and not with the call. If you declare
a pointer as null and then use it in a bad fashion, is the declaration
invoking undefined behavior, or the pointer use? Clearly it is ok to
declare a pointer as being null. However, if the very next statement
hands it off to gets, you have problem with the call to gets, not the
declaration.

So, blah(NULL) could be proven to invoke undefined behavior with some
source code analysis tool, but the language would accept the call as
valid (I think this is what you are wondering my feelings about).

--
Craig

Manchester, NH
So what? I got my hands dirty to achieve our goal. -- Paul Mesken



Thu, 06 Jan 2000 03:00:00 GMT  
 Comments needed

Quote:

>Another thing:
>        *str does point to a memory location, you just don't know where.

*str isn't a pointer. There's no reason why str must point to a memory
location. NULL may simply be mapped to an internal value that means
"invalid pointer".

Quote:
> This
>is your access violation.  You should not write to a memory location
>that does not belong to your program.  You could wipe out part of your
>operating system.  :-(

That is possible, if it lets you.

--
-----------------------------------------


-----------------------------------------



Thu, 06 Jan 2000 03:00:00 GMT  
 Comments needed



Quote:
>If a null pointer gets abused in some manner inside of a function,
>and you call that function with a null pointer, that would result in
>whatever consequences being incurred when you called the function,
>including undefined behavior. You could argue that the offending
>behavior occurs in the function, and not with the call. If you declare
>a pointer as null and then use it in a bad fashion, is the declaration
>invoking undefined behavior, or the pointer use? Clearly it is ok to
>declare a pointer as being null. However, if the very next statement
>hands it off to gets, you have problem with the call to gets, not the
>declaration.

Yes, a bug in the code could cause undefined behaviour somewhere else e.g.:

     if (ptr == NULL) {   /* Oops that should have been != */
        *ptr = 'X';
     }

The bug is in the first line but any undefined behaviour will occur in the
second.

--
-----------------------------------------


-----------------------------------------



Thu, 06 Jan 2000 03:00:00 GMT  
 
 [ 19 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Need comments on commenting program

2. need clarification of standard re // comments

3. P2P Name-Space Project Need Comments

4. SNI: need cc option to accept c++ comments

5. FORTRIX-C comments and experience needed

6. Need comments on using C libraries

7. Mircosoft Certified MFC/VC++ 5.0 classes: Need comments!!

8. Change C++ comments to C-comments

9. Multi-Line Comments Nested in Single Line Comments

10. changing C++ comments // to C comments /*

11. comments on comments

12. changing C++ comments // to C comments /*

 

 
Powered by phpBB® Forum Software