2
Crazed
7y

C++ gods, help me!

I'm passing a variable of type int** (an array of type int*[], whoss values arr of type (int *) new int[])

The function definition takes int ***arrayName and I am setting the values with *arrayName[i][j]. This works for all i=0, but when i>=1, I get a segfault!

I really dont know why or what this means, any ideas? :(

Comments
  • 1
    @runfrodorun
  • 1
    Int** is that not a pointer to an array of pointers?

    X[n][n] is an address in a continuous memory but a pointer to a pointer is not the same thing.

    See this post (my c skills is a bit dated but I do think they are different in how they address things.

    https://stackoverflow.com/questions...
  • 0
    @Voxera im honestly not sure, we barely started learning about pointers in class. This is a continuation of an assignment that the vast majority of my class failed.
  • 1
    @Crazed srsly? Your teacher is a douche.
  • 1
    @Crazed look it up or do some isolated testing. Its been almost 18 years since I did any serious C programming.
  • 0
    @projektaquarius he's an older indian man with broken english. We're reading a .dat file and creating a .ppm file with a greedy algorithm using procedural paradigm to do it all. The first aasignment was using a 1D array and just passing it around, now we're doing it with a 2D array and multiple paths... The class average for the last assignment was 35%, and that's with "if it compiles and runs mostly correctly its full credit"
  • 1
    A list of pointers does not have to address continuous memory since each pointer is allocated separately but an straight multi dimensional array is a continuous chunk of memory.

    How do you initialize the memory?
  • 0
    @Voxera initialized the array by goint through every time I create an inner array of type int[width] and setting all of those values to 0
  • 1
    @Voxera @projektaquarius I was able to get it running when putting my code from the function into main. Its not procedural, but this is due tomorrow and I have other classes I need to work with. Ill take a 90 over a 0 any day.
  • 1
    @Crazed ok but does [][] do the pointer lookup?
  • 1
    As I said, its been a long time, its just some gut feeling guiding my questions ;)
  • 0
    @Voxera I have absolutely no idea. I barely had and c++ exposure coming into this class. This is my first time working with pointers. I can access and modify my **array element via array[i][j] inside of main, attempting to pass it though I have no idea what or how to access it.
  • 3
    @runfrodorun glad someone with fresh knowledge came to the rescue ;)
  • 2
    @runfrodorun I can not give you enough ++. I have been googling for hours. Legitimate MVP over here.
  • 2
    @runfrodorun yeah I realized my mistake and pulled the comment. Did not want to spread misinformation.
  • 1
    @runfrodorun also C# is awesome embrace progress.
  • 0
    @runfrodorun thanks for the tip! Unfortunately im limited as thr notation was required by the instructions :(
  • 0
    @runfrodorun Yes,

    That was the first basic version I came in contact with on PC.

    Running on MS dos 2.11d ;) from 360kbyte floppy disks.
Add Comment