subscript notation with pointer to pointer fails 
Author Message
 subscript notation with pointer to pointer fails

Does anyone know why the following causes a GPF? Thanks

#include <stdlib.h>

int main(void) {

     int **l;
     int* n = new int(5);
     *l = (int*) malloc(1 * sizeof(int*));
      l[0] = n;

      return 0;

Quote:
}

----
Posted via http://www.*-*-*.com/ - the FREE public USENET portal on the Web
Complete SEARCHING, BROWSING, and POSTING of text and BINARY messages!


Tue, 14 Oct 2003 00:41:14 GMT  
 subscript notation with pointer to pointer fails


Quote:
> Does anyone know why the following causes a GPF? Thanks

> #include <stdlib.h>

> int main(void) {

>      int **l;
>      int* n = new int(5);

Because 'new' is not valid C?

--
- Mark A. Odell
- Embedded Firmware Design, Inc.
- http://www.embeddedfw.com



Tue, 14 Oct 2003 00:45:10 GMT  
 subscript notation with pointer to pointer fails


Quote:


>> Does anyone know why the following causes a GPF? Thanks

>> #include <stdlib.h>

>> int main(void) {

>>      int **l;
>>      int* n = new int(5);

>Because 'new' is not valid C?

Isn't that a syntax error (or possibly a constraint violation) requiring
a diagnostic?  Of course, if an executable is produced and run, that
invokes undefined behavior and a GPF is one of the possible results...

Actually, though, I think the OP's question has a C answer if the C++
memory allocation is ignored:  the next line of code (the first one
that got snipped here) tries to assign through l without pointing it at
anything first, so it tries to write to whatever address the garbage
(indeterminate value, actually) in l happens to point at.  This is
also undefined behavior, but this particular undefined behavior usually
produces some kind of invalid access trap (in this case, a GPF) under
systems with strong memory protection.

dave

--

Since we both seem suddenly to be terribly reasonable people, why don't
we keep it that way?
                                    --Richard Heathfield in comp.lang.c



Tue, 14 Oct 2003 01:07:53 GMT  
 subscript notation with pointer to pointer fails


Quote:
> Does anyone know why the following causes a GPF? Thanks

> #include <stdlib.h>

> int main(void) {

>      int **l;

Declares a variable `l` that will point to a pointer to `int`. `l` is
presently uninitialised (ie junk).

Quote:
>      int* n = new int(5);

I think you mean `int *n = malloc(5 * sizeof(*n));`; that `new` stuff
isn't part of C.

Quote:
>      *l = (int*) malloc(1 * sizeof(int*));

Oops! You've just tried to store through the junk pointer `l`. All bets
are off.

You need to set `l` to something sensible. For example:

    int finally = 42;
    int *middle = &finally;
    int **l = &middle;

or malloc to taste.

[rest deleted]

--
Hextris Hedgehog



Tue, 14 Oct 2003 03:59:31 GMT  
 subscript notation with pointer to pointer fails

Quote:
>Does anyone know why the following causes a GPF? Thanks

>#include <stdlib.h>

>int main(void) {

>     int **l;
>     int* n = new int(5);
>     *l = (int*) malloc(1 * sizeof(int*));
>      l[0] = n;

>      return 0;

>}

Compiling MAIN.C:
Error MAIN.C 7: Undefined symbol 'new'
Error MAIN.C 7: Declaration syntax error
Warning MAIN.C 8: Possible use of 'l' before definition

As long as you use a C compiler, your code is safe! Hehe!

--
-hs-    "spaces, not tabs" email: emdel at noos.fr
CLC-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
ISO-C Library: http://secure.dinkumware.com/htm_cl/
FAQ de FCLC : http://www.isty-info.uvsq.fr/~rumeau/fclc



Tue, 14 Oct 2003 04:42:54 GMT  
 subscript notation with pointer to pointer fails
Quote:

> Does anyone know why the following causes a GPF? Thanks

> #include <stdlib.h>

> int main(void) {

>      int **l;
>      int* n = new int(5);

               ^^^^
          Your C compiler vomited when it saw this undefined
          identifier.

Quote:
>      *l = (int*) malloc(1 * sizeof(int*));

        l = malloc(1 * sizeof *l);
        /* and check the return value from malloc */
Quote:
>       l[0] = n;

>       return 0;

> }



Tue, 14 Oct 2003 05:59:17 GMT  
 subscript notation with pointer to pointer fails

Quote:

> Does anyone know why the following causes a GPF? Thanks

> #include <stdlib.h>

> int main(void) {

>      int **l;
>      int* n = new int(5);

<Type> <variable> = new <Type> is only valid in C++
...sorry :O)

Quote:
>      *l = (int*) malloc(1 * sizeof(int*));
>       l[0] = n;

>       return 0;

> }

> ----
> Posted via http://www.etin.com - the FREE public USENET portal on the Web
> Complete SEARCHING, BROWSING, and POSTING of text and BINARY messages!

--
X~


Tue, 14 Oct 2003 06:31:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Definition of pointer on pointer fails

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

3. Pointer Arithmetic or Thingly Disguised Subscript

4. Error C2109: subscript requires array or pointer type

5. DWORD to Pointer or Index or Subscript

6. Pointer notation

7. Validity of simplified notation for calling functions from function pointers

8. Please Please Help! - pointer notation

9. Help - pointer notation please...

10. Pointer Notation Flaw

11. memory leak: pointer->pointer->pointer->struct

12. Pointer of Pointers was Pointer of arrays...

 

 
Powered by phpBB® Forum Software