Thread: Alocates allocation memory for a multi-level pointers

  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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 09:34 AM
  2. env deep in multi-level make
    By Kennedy in forum Linux Programming
    Replies: 0
    Last Post: 02-15-2010, 02: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, 09:57 AM
  5. dynamic memory allocation and returning pointers
    By sballew in forum C Programming
    Replies: 7
    Last Post: 11-03-2001, 03:21 PM