C pointer on NT 
Author Message
 C pointer on NT

I got a problem of c pointer on NT. In a program, I
dynamically allocate the memory for the string pointer. I
free the memory for the allocated string pointer after its
use. Then, during exiting the function, to prevent the
string pointer is not freed for some error cases, I check
if the pointer is null or not. If the pointer is null,
when checking if the pointer is null, the program will
crash at run-time.

I wrote a small program (see below) and it works. If the
pointer is null, the program will crash at run-time when
the checking if the pointer is null. The compiler is vc
6.0.

To me, I think the program should not fail when checking
if the pointer is null or not, if the pointer is null.

Does anyone have any idea about this problem? Thanks for
your help in advance!

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main() {
        char *str;
        str = (char *)calloc(100, sizeof(char));

        strcpy(str, "abcd");
        printf("str = %s \n", str);

        free(str);
        str = NULL;

        /*str pointer is null.
        When checking if the pointer is null or not,
        the program will not crash at run-time for this
        small program.*/
        if (str != NULL ) {
                printf(" str != NULL \n");
                free(str);
                str = NULL;
        }
        else
                printf(" str is NULL \n");

        return 0;

Quote:
}



Tue, 11 Jan 2005 15:48:51 GMT  
 C pointer on NT
The problem is, that the call to free() doesn't set your pointer to NULL. So
it will allways be != NULL.
You should call free() without checking the pointer afterwards.

Max


Quote:
> I got a problem of c pointer on NT. In a program, I
> dynamically allocate the memory for the string pointer. I
> free the memory for the allocated string pointer after its
> use. Then, during exiting the function, to prevent the
> string pointer is not freed for some error cases, I check
> if the pointer is null or not. If the pointer is null,
> when checking if the pointer is null, the program will
> crash at run-time.

> I wrote a small program (see below) and it works. If the
> pointer is null, the program will crash at run-time when
> the checking if the pointer is null. The compiler is vc
> 6.0.

> To me, I think the program should not fail when checking
> if the pointer is null or not, if the pointer is null.

> Does anyone have any idea about this problem? Thanks for
> your help in advance!

> #include <stdlib.h>
> #include <stdio.h>
> #include <string.h>

> int main() {
> char *str;
> str = (char *)calloc(100, sizeof(char));

> strcpy(str, "abcd");
> printf("str = %s \n", str);

> free(str);
> str = NULL;

>         /*str pointer is null.
>         When checking if the pointer is null or not,
>         the program will not crash at run-time for this
>         small program.*/
> if (str != NULL ) {
> printf(" str != NULL \n");
> free(str);
> str = NULL;
> }
> else
> printf(" str is NULL \n");

> return 0;
> }



Tue, 11 Jan 2005 16:55:54 GMT  
 C pointer on NT
It works when I compile it with VC 7 (I don't have any machine with VC6 on
right now) both as a C+ and as a C++ program.

If you can read assembly, you can generate a listing with /FAs. Or you can
look in the de{*filter*} to what is happening. It would greatly surprise me that
6.0 generated bad code for this. What SP level is your 6.0 toolset?

Ronald Laeremans
Visual C++ compiler and libraries team


Quote:
> I got a problem of c pointer on NT. In a program, I
> dynamically allocate the memory for the string pointer. I
> free the memory for the allocated string pointer after its
> use. Then, during exiting the function, to prevent the
> string pointer is not freed for some error cases, I check
> if the pointer is null or not. If the pointer is null,
> when checking if the pointer is null, the program will
> crash at run-time.

> I wrote a small program (see below) and it works. If the
> pointer is null, the program will crash at run-time when
> the checking if the pointer is null. The compiler is vc
> 6.0.

> To me, I think the program should not fail when checking
> if the pointer is null or not, if the pointer is null.

> Does anyone have any idea about this problem? Thanks for
> your help in advance!

> #include <stdlib.h>
> #include <stdio.h>
> #include <string.h>

> int main() {
> char *str;
> str = (char *)calloc(100, sizeof(char));

> strcpy(str, "abcd");
> printf("str = %s \n", str);

> free(str);
> str = NULL;

>         /*str pointer is null.
>         When checking if the pointer is null or not,
>         the program will not crash at run-time for this
>         small program.*/
> if (str != NULL ) {
> printf(" str != NULL \n");
> free(str);
> str = NULL;
> }
> else
> printf(" str is NULL \n");

> return 0;
> }



Wed, 12 Jan 2005 00:50:41 GMT  
 C pointer on NT



Quote:
> It works when I compile it with VC 7 (I don't have any machine with VC6 on
> right now) both as a C+ and as a C++ program.

> If you can read assembly, you can generate a listing with /FAs. Or you can
> look in the de{*filter*} to what is happening. It would greatly surprise me
that
> 6.0 generated bad code for this.

Just fine on VC 6, SP 5
Quote:
> What SP level is your 6.0 toolset?

> Ronald Laeremans
> Visual C++ compiler and libraries team



Thu, 13 Jan 2005 03:38:52 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Newbie: separate big .cs file into small .cs files

2. NT crashes down using a lot of pointers

3. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

4. How to show/call Form2.cs from Form1.cs ?

5. Include code in other Cs files

6. Reuse of cs files, namespace, arch advice pls

7. word - automatic numbering/bold/underline/italics

8. How to Generate .cs file at Runtime

9. newbe/cs student, need help w/ code

10. Serial.cs

11. Compile CS source code using ICodeCompiler

12. Two CS files (using namespaces)

 

 
Powered by phpBB® Forum Software