![]() |
|
#1
|
|||
|
|||
Dynamic 2 dimensional array problemHi,
My existing code is like this : CPP / C++ / C Code:
If you can see that, the above code use static dimentional array for check. Do you have any idea about how to make/ declare check as a dynamic dimensional array ? Many thanks. |
|||
|
#2
|
|||
|
|||
Re: Dynamic 2 dimensional array problemIf I'm not mistaken
CPP / C++ / C Code:
I don't think that is what you intended. I don't think you want a block of space to store 100 arrays of 100 struct nodes either. Nope, structs like you show above are intended to be used in a "self referencing data structure" which is more often referred to as a "Linked List". For that you will want to do one allocation for each new node. You would use malloc() or calloc() for each allocation. The idea is to form a chain of nodes stored at different locations in memory and be able to access any of them by having each linked to the next simply by having each 'know' the address of the next. That is done by storing that address in the CPP / C++ / C Code:
There have been several good threads on this in the past couple of years. Use the "Search this Forum" link at the top-right of the main forum page. |
|
#3
|
||||
|
||||
Re: Dynamic 2 dimensional array problemQuote:
Something like this: CPP / C++ / C Code:
Output: Code:
...is how to make a basic linked list using dynamic memory allocations for your nodes. Something like this: CPP / C++ / C Code:
Output: Code:
...if you want to create an "array" where the dimension is allocated dynamically. MxB |
|
#4
|
|||
|
|||
Re: Dynamic 2 dimensional array problemThanks for your reply. Yes, I don't want to store 100 arrays as my input data is generated by a random graph. What I need is to have 2 dynamic dimentional array pointer with NODE type.
I believe, I should allocate memory using malloc function. Example: CPP / C++ / C Code:
The problem is, when n = 500, I manage to get my result, but when n >=500, a problem with segmentation errors occur here. Diff machine give me diff result as the input graph is generated by a random number. I don't want to have a static 2 dimensional array. I want to make it dynamic, but I have a problem with that. Please help. |
|
#5
|
|||
|
|||
Re: Dynamic 2 dimensional array problemThere are a couple of different ways to allocate 2D arrays.
1) Allocate an array of pointers to an array. So, if you wanted an array of integers of x rows and y columns, you would allocate it like: CPP / C++ / C Code:
2) Another way is to allocate it as just one large array. This saves space as we don't have to allocate space for the address of each array. It is done like so: CPP / C++ / C Code:
|
|
#6
|
|||
|
|||
Re: Dynamic 2 dimensional array problemThank you Fakepoo.
I like the first declaration, but it only works if variable array is a dimensional array. I am hoping that array is a pointer as well. CPP / C++ / C Code:
j is a pointer to a linked list and I want to perform delete operation at certain location (eg: array[3][5]). Therefore I need to declare j as a pointer. Or, do you have any other idea/suggestion? Is memory allocation for a two dimentional pointer type diff compare to a normal dimensional array? I have to do this as I don't want to have loop when performing delete operation. I just want to go straight away to the exact place to perform deletion. Many thanks for your kind help. |
|
#7
|
|||
|
|||
Re: Dynamic 2 dimensional array problemOK. So let me see if I understand what you are trying to do. You have a 2-D array. Each element of the array is a linked list? So, if you have a 10 x 10 array then you have 100 linked lists? If this is the case and you want each element of the array to be a node*, then:
CPP / C++ / C Code:
Now, as far as your definition of node....shouldn't it reference another node? Something like: CPP / C++ / C Code:
|
|
#8
|
|||
|
|||
Re: Dynamic 2 dimensional array problemThank you so much.
You have given me a very clear picture. I believe that I can do it. Many thanks. |
|
#9
|
||||
|
||||
Re: Dynamic 2 dimensional array problemQuote:
Instead of using malloc and then looping to null initialize, simply use calloc instead. You'll save cycles and code/typing. MxB |
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Convert row elements from two dimensional array to one dimensional arrays | ahmetemir | MS Visual C++ / MFC Forum | 0 | 01-Feb-2009 05:08 |
| where is the problem and can you fix it (php) | oggie | MySQL / PHP Forum | 8 | 14-Apr-2008 16:08 |
| My newbie two dimensional array problem | Zach | C++ Forum | 2 | 04-May-2006 23:21 |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 20:31 |
| problem reading to a dynamic array | noamfrie | C Programming Language | 9 | 02-Jan-2005 19:35 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The