"The memory could not be "read". "

I am using Dev-C++ 4.9.9.2 compiler and I was messing around with pointers.
When I write this small code in C:

[code]
#include
int main()
{
char *str;
printf("%s",str);
}
[/code]

I get the following error message:
Application Error:
"The instruction at "0x78011f41" referenced memory at "0x00000002". The memory could not be "read". "

The most interesting thing is that when I declare also two int variables like this:

[code]
#include
int main()
{
int a,b;
char *str;
printf("%s",str);
}
[/code]

the error message does not appear anymore.
Also when I declare only one int variable I get the error message.
When I declare the string pointer out of the main function like this:

[code]
#include
char *str;
int main()
{
printf("%s",str);
}
[/code]

I don`t get the error anymore too.

Is this a bug of the Dev-C++ compiler or of the Win2K/XP Operating System ?
Or is it a rule of memory management of the C language?

Thanks in advance.

Comments

  • : [code]:
    : #include
    : int main()
    : {
    : char *str;
    : printf("%s",str);
    : }
    : [/code]:

    [color=Purple]You declared a pointer but did not initialize it. This means str points to a random memory location - this happens to all automatic/local uninitialized pointers. when printf() tries to see what is pointed to by str, the OS restricts it because that time str pointed to some memory location not given to your program for use. or most likely the OS kernel or some other program is using it. you cannot read other process' memory. note that this location is completely random. it may change in different computer/operating system or even other time when you run the program.
    [/color]
    : [code]:
    : #include
    : int main()
    : {
    : int a,b;
    : char *str;
    : printf("%s",str);
    : }
    : [/code]:

    [color=purple]Here, still str is pointing to some random memory location. it is just luck that this time may be str[0] is a NULL byte. or may be str[0] is a NULL byte and str is not pointing to some memory already allocated by some other process. so you didn't see any error message.[/color]

    : [code]:
    : #include
    : char *str;
    : int main()
    : {
    : printf("%s",str);
    : }
    : [/code]:

    [color=purple]every global variable is initialized with 0. so str[0] = NULL in this case. and printing a NULL byte doesn't harm since printf stops whenever it sees a NULL byte while printing a string.

    BE AWARE! always used properly initialized pointers.[/color]
    [hr][purple]~Donotalo()[/purple]
  • Donatolo thank you for your time and help. Anyway I knew that I can`t use uninitialised pointers declared inside a function scope.
    I was just curious why it happens that when I declare two or more other int variables the pointer can be read. And when I declare exactly only one or none other variables, the pointer can`t be read.
    So my question is: Why exactly only two or more variables ?
  • Look at the following main functions:
    [code]
    int main()
    {
    char *str;

    printf("%s", str);

    return 0;
    }
    [/code]
    [code]
    int main()
    {
    int a;
    char *str;

    printf("%s", str);

    return 0;
    }
    [/code]
    [code]
    int main()
    {
    int a, b;
    char *str;

    printf("%s", str);

    return 0;
    }
    [/code]
    [code]
    int main()
    {
    int a, b, c;
    char *str;

    printf("%s", str);

    return 0;
    }
    [/code]
    i compiled and ran the programs in Visual Studio 2008 both in debug mode and release mode. each of the programs crash in debug mode. in release mode they showed random character, or sometimes nothing at all. whatever the output or runtime behaviour of your uninitialized pointers, that is completely random. you see, i'm not getting the exact output/behaviour as yours.

    one thing i want to mention, there is a difference in the following two declarations:

    code 1:
    char *str;

    and

    code 2:
    int a;
    char *str;

    when you declare variables, memory is allocated for them in a stack. the variables are pushed onto the stack in the same sequence as they are declared. so in the code 1 above, str is at the bottom of the stack, where in code 2, variable a is at the bottom of the stack and str is at the top of the stack. the memory is pointed to by str in code 1 and code 2 are different. again, this memory is random and may change when you restart your computer or use your RAM heavily and re run the program. that is why you are getting different behaviour when you are declaring variables before str.
    [hr][purple]~Donotalo()[/purple]
  • : : [code]: :
    : : #include
    : : char *str;
    : : int main()
    : : {
    : : printf("%s",str);
    : : }
    : : [/code]: :
    :
    : [color=purple]every global variable is initialized with 0. so str[0]
    : = NULL in this case. and printing a NULL byte doesn't harm since
    : printf stops whenever it sees a NULL byte while printing a string.
    :
    : BE AWARE! always used properly initialized pointers.[/color]
    : [hr][purple]~Donotalo()[/purple]
    [color=Blue]Something is not right with your explanation. str is a global pointer, so it should be NULL (try debugger if you do not believe it), so accessing a first symbol like str[0] already should give an error, because you are reading from address zero.[/color]
  • : : : [code]: : :
    : : : #include
    : : : char *str;
    : : : int main()
    : : : {
    : : : printf("%s",str);
    : : : }
    : : : [/code]: : :
    : :
    : : [color=purple]every global variable is initialized with 0. so str[0]
    : : = NULL in this case. and printing a NULL byte doesn't harm since
    : : printf stops whenever it sees a NULL byte while printing a string.
    : :
    : : BE AWARE! always used properly initialized pointers.[/color]
    : : [hr][purple]~Donotalo()[/purple]
    : [color=Blue]Something is not right with your explanation. str is a
    : global pointer, so it should be NULL (try debugger if you do not
    : believe it), so accessing a first symbol like str[0] already should
    : give an error, because you are reading from address zero.[/color]


    He is confusing the NULL pointer for a null-termination. str = NULL, str[0] = garbage. Printing a null pointer is undefined behavior. A smart compiler will print the next "(null") or similar, but that isn't required by the standard.
  • : : : [code]: : :
    : : : #include
    : : : char *str;
    : : : int main()
    : : : {
    : : : printf("%s",str);
    : : : }
    : : : [/code]: : :
    : :
    : : [color=purple]every global variable is initialized with 0. so str[0]
    : : = NULL in this case. and printing a NULL byte doesn't harm since
    : : printf stops whenever it sees a NULL byte while printing a string.
    : :
    : : BE AWARE! always used properly initialized pointers.[/color]
    : : [hr][purple]~Donotalo()[/purple]
    : [color=Blue]Something is not right with your explanation. str is a
    : global pointer, so it should be NULL (try debugger if you do not
    : believe it), so accessing a first symbol like str[0] already should
    : give an error, because you are reading from address zero.[/color]

    oh right i messed up this thing. my explanation was wrong. thanks for correcting me. thanks to Lundin too, i did see the output "(null)" but didn't understand its meaning. now it is clear. :)
    [hr][purple]~Donotalo()[/purple]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories