| | |
Dynamic Mmeory Allocation with 2D Arrays
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hi all;
I've to use a dynamic array(2D) in my program in C.
I've read some tutorials about this and I understood its mental.But still there exist a few points which I didn't understand.
Here is a code:
Firstly: Does the **a mean that first * points the row and second * points the column???(If not what does it mean???)
Secondly: In the "a[x] = (int *)malloc(sizeof(int) * 3);" , malloc function returns an adress and as far as I can see it assigns a[x] to a[x] 's adress.
But when I print the a[x] value and it's adress; I saw it doesn't process like my idea which was indicated above.So what is the problem???
Thanks for your helps...
I've to use a dynamic array(2D) in my program in C.
I've read some tutorials about this and I understood its mental.But still there exist a few points which I didn't understand.
Here is a code:
c Syntax (Toggle Plain Text)
int main(){ int **a, x; a = (int **)malloc(sizeof(int) * 10); for(x = 0; x < 10; x ++) { a[x] = (int *)malloc(sizeof(int) * 3); } /* ... */ for(x = 0; x < 10; x ++) { free(a[x]); } free(a);
Firstly: Does the **a mean that first * points the row and second * points the column???(If not what does it mean???)
Secondly: In the "a[x] = (int *)malloc(sizeof(int) * 3);" , malloc function returns an adress and as far as I can see it assigns a[x] to a[x] 's adress.
But when I print the a[x] value and it's adress; I saw it doesn't process like my idea which was indicated above.So what is the problem???
Thanks for your helps...
Last edited by FoX_; Apr 14th, 2007 at 8:10 pm.
>Does the **a mean that first * points the row and second * points the column???
Almost. The first * points to a row, however the second * points to an individual "cell" if you will, of a row. It points to the integer.
>malloc function returns an adress and as far as I can see it assigns a[x] to a[x] 's adress.
It assigns the address of the freshly-allocated memory to a[x], yes.
>But when I print the a[x] value and it's adress; I saw it doesn't process like my idea which was indicated above.
What do you mean? a[x] just holds a pointer to the actual memory, you'll have to dereference the address contained inside a[x] if you want to print out the value.
Almost. The first * points to a row, however the second * points to an individual "cell" if you will, of a row. It points to the integer.
>malloc function returns an adress and as far as I can see it assigns a[x] to a[x] 's adress.
It assigns the address of the freshly-allocated memory to a[x], yes.
>But when I print the a[x] value and it's adress; I saw it doesn't process like my idea which was indicated above.
What do you mean? a[x] just holds a pointer to the actual memory, you'll have to dereference the address contained inside a[x] if you want to print out the value.
"Technological progress is like an axe in the hands of a pathological criminal."
•
•
•
•
a = (int **)malloc(sizeof(int) * 10); for(x = 0; x < 10; x ++) { a[x] = (int *)malloc(sizeof(int) * 3); }
a = malloc(sizeof(int*) * 10);A better way would have been:
c Syntax (Toggle Plain Text)
int main() { const int SIZE = 10; double **arr = (double**)malloc(SIZE * sizeof(*arr)); for(int i = 0; i < SIZE; ++i) { arr[i] = (double*)malloc(SIZE * sizeof(*arr[0])); } for(int i = 0; i < SIZE; ++i) for(int j = 0; j < SIZE; ++j) arr[i][j] = i * j + i + j; for(int i = 0; i < SIZE; ++i) { for(int j = 0; j < SIZE; ++j) cout << arr[i][j] << "\t"; cout << '\n'; } getchar(); return 0; }
I don't accept change; I don't deserve to live.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
•
•
•
•
const int SIZE = 10;
double **arr = (double**)malloc(SIZE * sizeof(*arr));
for(int i = 0; i < SIZE; ++i)
{
arr[i] = (double*)malloc(SIZE * sizeof(*arr[0]));
}
}[/code]
array. (this kind of array is called a jagged array in C#).
A true two dimensional array is rectangular; a contigous area of memory which contains I*J elements.
we simulate this in C by creating an array, every element of which is an array. for example,
C++ Syntax (Toggle Plain Text)
int a[50][30] ; // automatic or static storage class int (*b)[30] = malloc( sizeof( int[30] ) * 50 ) ; // dynamic storage class
Last edited by vijayan121; Apr 15th, 2007 at 10:44 am.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: dev-c++ and libtiff
- Next Thread: PHP 'explode()' in C++ ?
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






