If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

How to reference **ptr_xx to ptr_yy [xx][xx]

Hi All,

Good Morning to everyone here. :P

May i know anyone here can help to answer one of my question. I would like to know how to reference double dimension array with double dimension pointer. ( i think this may sounds silly to someone here )

I have tried to reference one dimension array with pointer without problem,

Re: How to reference **ptr_xx to ptr_yy [xx][xx]

First, look at "std::vector" which is much easier and safer to work with than raw C-style arrays. In any case, an array name always decays to a pointer to the first element of the array. Given your example then:

char xx[100][500];

What is stored in this array? Well, another array actually. That is, there are 100 elements in your array, each of which is an array of 500 chars. So, since the name "xx" decays to a pointer to the first element of the array as mentioned above, and that element is an array of 500 chars, then you're looking at this:

char (*yy)[500] = xx;

"yy" is a pointer to an array of 500 chars. The syntax is brutal but the underlying idea of an array that stores another array is elegant (and you can carry this idea to any number of dimensions). It takes a little practice to think about it this way (draw your elements out on paper to see) but it becomes self-evident after a short time.

Re: Re: How to reference **ptr_xx to ptr_yy [xx][xx]

Originally posted by Sef
First, look at "std::vector" which is much easier and safer to work with than raw C-style arrays. In any case, an array name always decays to a pointer to the first element of the array. Given your example then:

char xx[100][500];

What is stored in this array? Well, another array actually. That is, there are 100 elements in your array, each of which is an array of 500 chars. So, since the name "xx" decays to a pointer to the first element of the array as mentioned above, and that element is an array of 500 chars, then you're looking at this:

char (*yy)[500] = xx;

"yy" is a pointer to an array of 500 chars. The syntax is brutal but the underlying idea of an array that stores another array is elegant (and you can carry this idea to any number of dimensions). It takes a little practice to think about it this way (draw your elements out on paper to see) but it becomes self-evident after a short time.

Thanks Sef.

I managed to solve the prob with another approach this afternoon by using casting.

Btw, if i am require to declare a very huge variable , such as e.g. char xx[9000][9000], how could i done with it? The compiler hv no problem to compile . However, when comes to running the exection file, i enconter error such as "cannot write to 0x0xxxxx location". Is it the RAM memory size not enough ? What or how can i increase my memory space for my huge 2 dimension array??

Btw, if i am require to declare a very huge variable , such as e.g. char xx[9000][9000], how could i done with it? The compiler hv no problem to compile . However, when comes to running the exection file, i enconter error such as "cannot write to 0x0xxxxx location". Is it the RAM memory size not enough ? What or how can i increase my memory space for my huge 2 dimension array??

Million thanks and BestRegards,
James Goh

No, no, no!!! The program will compile but you will run into execution problems! This will not work because the types are incompatible. On most systems the a char[2][2] array will allocate 4 bytes in succession. A char** is a pointer and occupies 4 bytes each. Each element of y will now point to some other memory location. Let's assume that x is initiallized with all zero values and occupies the memory 0x1000 to 0x1003. We then have the following situation:

Doubly dereferencing y will now lead you to an access violation on most systems. This is likely your problem with the "cannot write to 0x0xxxxx location" error. Sef is absolutely correct. If you must work with C-style arrays, then I suggest using this approach (which is ultimately the same as Sef's just a little more clear to the unexperienced programmer):

This will now work and is perfectly legal. Now about your other question about allocating large arrays: You will experience a stack overflow. Such arrays need to be allocated dynamically with new or malloc(). Search the C++ FAQ or this forum -- you will find whole discussions on this topic.

Oh, I know why your code seems to work. That is because you re-assign the pointers to point to the constant strings. But your program certainly does not do what you think it does. Try inseting the following lines at the end of your program:

Code:

printf("%c", x[0][0]);
printf("%c", x[1][0]);

You'll see that you print junk values. See my previous post as to why!

The original software was written in 16 bit Turbo C in early 90s, and my task is to convert it from 16 to 32 bit application . Borland C was choosen because it can almost directly convert the Turbo C 16 bit program code into 32 bit program code ( OWL was used).

The new application is now written in Borland C version 5.02, it will dynamically explicit loads DLL file written in VC++ version 6 ( as i more familar with VC++, i wrote those addition function in VC and dynamically call by Borland C application).

It can be compiled and run, however, it will prompt error when "I guess" the program is accessing the memory. This error was not seen in the old Turbo C 16 bit applicaiton. I have the error prompt of "The Instruction at "0x0xxxxxx" referenced memory at "x0x0xxxxxx". The memory couldn not be "written".

I trace the program during debug mode and notice it prompt this error during memory declaration when using Malloc and API HGlobalAlloc function. Thus, i convert most of the Malloc and HglobalAlloc API into static memory declartion, e.g. char xx[yy]. However, there are some Malloc funtion that are declared to allocate very huge memory location in the program, and i cannt convert it into char xx[yy][zz] as i will run into memory error if the size is too big.

From your point of view, what /where goes wrong in my program , and how could i remedy or get ride of the error prompt? Thanks ..

From your point of view, what /where goes wrong in my program , and how could i remedy or get ride of the error prompt? Thanks ..

Sorry for not responding to you sooner (following my first post). Others have already commented however though I haven't read them at length. Kevin Hall for instance was absolutely right to correct your (incorrect) use of casting to eliminate the problem. The way I showed you in my first post was the correct way given your original question (I think Kevin also showed a variation of this using typedefs). Other techniques for creating multidimensional arrays also exist as well, including arrays of pointers. You really would be better off using "std::vector" though as I originally suggested. I think the real issue here however is that you need to brush up on arrays and pointers in general. Forums like this are really geared to answer specific questions, not to provide a complete synopsis on larger topics like arrays. The bottom line is that you shouldn't be coding these things if you don't fully understand them which is self-evident now. Many full tuturials exist on the subject all over the web and in your favorite C++ book. I suggest reading them

Sef, he's converting a Turbo C program from the early 90's. I had that compiler and templates were not supported well. I know it did not support the STL (I don't even know if the STL was widespread or even created at that point). I do agree he needs to learn how to deal with pointers correctly and that the STL would be better for all new programs. Upgrading an old program might be even more of a mess than just correcting the use of pointers though.

James, if you attach your code to your next message and tell us what line # (and file if there are more than one file) that causes you problems, we might be able to help more. Outside of that, I do recommend getting some books and/or finding some tutorials.

Originally posted by KevinHall
Sef, he's converting a Turbo C program from the early 90's. I had that compiler and templates were not supported well. I know it did not support the STL (I don't even know if the STL was widespread or even created at that point). I do agree he needs to learn how to deal with pointers correctly and that the STL would be better for all new programs. Upgrading an old program might be even more of a mess than just correcting the use of pointers though

Agreed. I only mentioned "vector" so at least he would be aware of it for the future (if he can't apply it now). The fact that he would "solve" the problem by casting though even after a correct solution was provided (which he probably didn't fully understand), and that he's unware of the repercussions of declaring a 9000x9000 array (nor how to do it using "new" or even "malloc"), clearly indicates he's in deep water but doesn't know how to swim. He needs to move back to the shallow end and learn the basic strokes before we can really help him (and certainly before he attempts to modernize whatever he's working on)

Originally posted by Sef
Agreed. I only mentioned "vector" so at least he would be aware of it for the future (if he can't apply it now). The fact that he would "solve" the problem by casting though even after a correct solution was provided (which he probably didn't fully understand), and that he's unware of the repercussions of declaring a 9000x9000 array (nor how to do it using "new" or even "malloc"), clearly indicates he's in deep water but doesn't know how to swim. He needs to move back to the shallow end and learn the basic strokes before we can really help him (and certainly before he attempts to modernize whatever he's working on)

hi Sef
I think you have mistaken that i disagree with your solution, no i am not. While i posted my problem in the forum this morning , i was also trying to resolve the array - pointer problem as well. I get to "solve" the problem in the afternoon before i come to know your solution. I would say that your solution is equally cool as Kevin' solution.

This is my 3rd project on C lanugage (infact the first time in C++ , the previous 2 project was on C 'console based' ) and I agree that i am not very good in programming with pointers, and need more readup in C++ language . However, at the mean time i need some quick solution to meet my date line.

Infact, i do know about Malloc and New method to do dynamic memory allocation, however, it just didnt work in my present Turbo C++ to Borland C++ project ( it works in 16 bit turbo C using the Malloc ). Thus, i have no choice but to convert the Dynamic allocation to static allocation, and it works! ( or maybe another wrong solution?)

Now my problem is on memory allocation for the huge memory size , e.g. xx[9000][9000]. (it prompt error in the Malloc or New allocation) , and i am going to implement your solution on monday.

Kevin, I will send the file to you or post it here on Monday as i am not in office today. Thanks
James

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.