Dy Memory allocation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bruce prasad
    New Member
    • Sep 2006
    • 3

    Dy Memory allocation

    (1) How to correct the below dynamic mem allocation of 4 integer pointers

    int *p[4];
    p = (int* [])malloc(4*sizeo f(int*));



    (2)
    int x = 10, y = 30, z=40;
    p[0] = &x;

    Quest:
    Pointer p[0] (in heap memory) contains the address of x which is allocated in the data memory/segment. So is it possible to hold data memory/segment address in a heap memory pointer.
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    1. Malloc allocates a void pointer, and you have to cast it. You are allocating memory for 4 data of type (int*), yet casting it to type (int* []). It would make sense if those two data types agree, would it not?

    That being said, int* and int[] cause the same code to be generated for parameter passing. Likely, it's the case that int* and int[] are equivalent data types. Therefore, cast it to int pointer or int array, but not both. Since you already account for the four entries in the array, 4*sizeof(int*), you had better allocate memory for the integer pointers then.

    int* p[4] = (int*)malloc(4* sizeof(int*));

    2. All pointers are stored in the heap. An int pointer can point to an int whether it is stored on the stack, the heap, or data memory.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by D_C
      1. Malloc allocates a void pointer, and you have to cast it. You are allocating memory for 4 data of type (int*), yet casting it to type (int* []). It would make sense if those two data types agree, would it not?

      That being said, int* and int[] cause the same code to be generated for parameter passing. Likely, it's the case that int* and int[] are equivalent data types. Therefore, cast it to int pointer or int array, but not both. Since you already account for the four entries in the array, 4*sizeof(int*), you had better allocate memory for the integer pointers then.

      int* p[4] = (int*)malloc(4* sizeof(int*));

      2. All pointers are stored in the heap. An int pointer can point to an int whether it is stored on the stack, the heap, or data memory.
      1. Sorry but not quite right for the variable

      int *p[4];

      then p[0] has type int * and p takes the type for of a pointer to the type of 1 of it's entries.

      i.e. in int s[4]; s[0] has type int, s has type int *

      so p has type int **

      There for the allocation should be

      int *p[4];
      p = (int **)malloc(4*siz eof(int*));

      or you will get a warning for error on different pointer types.

      2. Again not quite right but in your defense right on a large majority of platforms. I recently worked on an embedded platform where you had to specify if your pointers where pointing a RAM (stack, heap, data segment) or ROM (consts). An given pointer could only point at 1 of these 2 types.

      Comment

      • bruce prasad
        New Member
        • Sep 2006
        • 3

        #4
        As per your advice for the creation of dynamic allocation of an array of 4 int pointers as follows

        int *p[4];
        p = (int **)malloc(4*siz eof(int*));
        I am getting the following error "Modifiable lvalue required for assignment operator" Please correct me.

        Comment

        • D_C
          Contributor
          • Jun 2006
          • 293

          #5
          Actually, after testing it on my computer, it wouldn't let me mix array and pointer notation, it said ANSI forbids it. I had to change it to
          Code:
          int** p;
          for the other line to compile. I should mention that each compiler is different. Some compilers are more open-minded than more strict ones.

          Comment

          Working...