Alocates allocation memory for a multi-level pointers

This is a discussion on Alocates allocation memory for a multi-level pointers within the C++ Programming forums, part of the General Programming Boards category; good evening everybody, when i try to understand deep about constant pointer, pointer to constant, raise on with multi-level pointer, ...

  1. #1
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23

    Alocates allocation memory for a multi-level pointers

    good evening everybody, when i try to understand deep about constant pointer, pointer to constant, raise on with multi-level pointer, i got trouble.
    Code:
    #include <iostream>
    using namespace std;
    int main() {
      /*  const int*** a; // ->**a is a pointer level 1
      int** b; // ->*b is a pointer level 1
      **a = *b;
      */
      int x = 2;
      int* y = &x;
      int** z = &y;
      int*** t = &z;
      int**** k = &t;
      const int*** b = new const int**;
      **b = ***k;
      cout << "k before changed: " << ****k << ", " << "*y = x = " << *y;
    
      /*  const int**** a = &t;
      int*** b;
      ***a = **b;
      */
      //  a = b;
      return 0;
    }
    When i try to compile this code no error occur, but when runs executable file, output: segmentation fault.

    Thanks for any repy.

  2. #2
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23
    Dose nobody help me?. this code is just i want to test about multi-level pointer, i'm sorry about my bad english, but i really spent a lot of time for this problem. thanks a lot.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,602
    Be patient.

    >>**b = ***k;
    b points to a const int**, but the value pointed to is undefined, hence you can't dereference it.

    b --> undefined value
    *b = undefined value
    **b = undefined behavior (can't derefernce undefined value)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Give us chance!
    Response time can be anywhere between minutes and 24 hours, depending on who's online at the time. It's a 24 hour world, people come and go at all times.

    > **b = ***k;
    Before you do **b, you need to make *b actually point at something useful.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by PuKa
    when i try to understand deep about constant pointer, pointer to constant, raise on with multi-level pointer, i got trouble.
    I suggest that you limit yourself to two levels of indirection, which in fact would have been sufficient to demonstrate your problem. I won't begrudge you wanting to reach for a third level of indirection to test yourself, but honestly four levels of indirection is going overboard.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User PuKa's Avatar
    Join Date
    Oct 2011
    Posts
    23
    Thanks a lot. i understood, really beautiful , i feels crazy now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Allocation using Pointers
    By dhardin in forum C++ Programming
    Replies: 2
    Last Post: 11-21-2010, 08:34 AM
  2. env deep in multi-level make
    By Kennedy in forum Linux Programming
    Replies: 0
    Last Post: 02-15-2010, 01:36 PM
  3. Link List,Structures,Dynamic Memory Allocation,Pointers
    By Tanuj_Tanmay in forum C Programming
    Replies: 2
    Last Post: 04-24-2009, 08:55 PM
  4. Question about memory/allocation/pointers
    By Magos in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 08:57 AM
  5. dynamic memory allocation and returning pointers
    By sballew in forum C Programming
    Replies: 7
    Last Post: 11-03-2001, 02:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21