#include<iostream.h>
int main(){
int n;
cout<<"enter no. of columns/rows of square matrix"<<endl;
cin>>n;
int *p=new int[n][n];
return 0;
}
allocating memory from heap
Page 1 of 19 Replies - 6146 Views - Last Post: 25 December 2008 - 12:52 PM
#1
allocating memory from heap
Posted 25 December 2008 - 08:58 AM
why is this not possible? i want to obtain the no. of rows and then allocate memory from the heap. if there's no alteration that can be done to this, what is the alternative?
Replies To: allocating memory from heap
#2
Re: allocating memory from heap
Posted 25 December 2008 - 09:41 AM
This is an example in C (see code comments):
#include<iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
int n = 0;
cout<<"enter no. of columns/rows of square matrix"<<endl;
cin>>n;
int **p;
p = (int**)malloc(n*sizeof(int*)); //makes p[n], where n is the number of pointers, C++ requires cast
for(int i = 0; i < n; i++)
{
p[i] = (int*)malloc(n*sizeof(int)); //for each p[n]we make the second dimension (# of ints 'n')
}
int a = 0;
for (int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
p[i][j] = a++; //asign some values, never take actions on uninitialized dynamically allocated arrays
}
}
for (int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cout << p[i][j] << " "; //print them out
}
cout << "\n";
}
for (int i = 0; i < n; i++)
{
free((int*)p[i]); //free up the array of pointers, must free each one otherwise memory will leak
}
free((int**)p); //free the handle to the multi dimension array
system("pause");
return 0;
}
#3
Re: allocating memory from heap
Posted 25 December 2008 - 09:54 AM
whats a cast? I'm not supposed to use C nor am i aware of it.
can't new and delete be used?
can't new and delete be used?
#4
Re: allocating memory from heap
Posted 25 December 2008 - 09:59 AM
It can. You just can't do it like you did. My snippet was to illustrate how to do it in C++ by giving an example in C. Not subtle enough? 
If you don't know what a typecast is why are you making a program with a dynamically allocated multi dimensional array? :Just curious...
//In C++ Exclusively
#include<iostream>
using namespace std;
int main()
{
int n = 0;
cout<<"enter no. of columns/rows of square matrix"<<endl;
cin>>n;
int **p;
p = new int*[n]; //make an array of ints, p points to them
for(int i = 0; i< n; i++)
{
p[i] = new int[n]; //each pointer now points to 'n' amount of ints
}
int a = 0;
for (int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
p[i][j] = a++; //assign some values, never take actions on uninitialized dynamically allocated arrays
}
}
for (int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cout << p[i][j] << " "; //print them out
}
cout << "\n";
}
for (int i = 0; i < n; i++)
{
delete((int*)p[i]); //free up the array of pointers, must delete each one otherwise memory will leak
}
delete [] p; //delete the handle (its an array so use brackets)
system("pause");
return 0;
}
If you don't know what a typecast is why are you making a program with a dynamically allocated multi dimensional array? :Just curious...
This post has been edited by KYA: 25 December 2008 - 10:01 AM
#5
Re: allocating memory from heap
Posted 25 December 2008 - 10:12 AM
I'll have to preface this by saying that I loathe multidimentional arrays. While they're often simple conceptually, the implementation is rarely worth it.
To dynamically allocation a 2d array, you essentially have to make a "jagged" array
That's the most basic example. You'll probably want to read more online.
Good luck.
Edit: oops, KYA beat me. Interesting, both syntaxes work. C++ is fun for that.
To dynamically allocation a 2d array, you essentially have to make a "jagged" array
int n = 3;
// First, the 1d array.
int *p1d;
p1d = new int[n];
// Nice, simple, conceptually clean. You have pointer to a block of n ints
// 2d is more difficult
int **p2d;
// nice syntax, huh?
// This first bit isn't so bad
*p2d = new int[n];
// you've allocated the first dimension
// even though it says int, it's really a list of pointers.
// we still need to allocate the actual storage
// and we need to do that for each n
for(int i=0; i<n; i++) {
p2d[i] = new int[n];
}
That's the most basic example. You'll probably want to read more online.
Good luck.
Edit: oops, KYA beat me. Interesting, both syntaxes work. C++ is fun for that.
This post has been edited by baavgai: 25 December 2008 - 10:15 AM
#6
Re: allocating memory from heap
Posted 25 December 2008 - 11:16 AM
thnx a lot kya and baavgai..
hey..i wasnt aware of the word "typecast"
i was just trying it out with the knowledge of 1D array..
hey 1 doubt...wen you declare a pointer, it has to be defined then and there right?
how can you give
int*p;
p=.....; ????????
hey..i wasnt aware of the word "typecast"
i was just trying it out with the knowledge of 1D array..
hey 1 doubt...wen you declare a pointer, it has to be defined then and there right?
how can you give
int*p;
p=.....; ????????
This post has been edited by dan_ram: 25 December 2008 - 11:17 AM
#7
Re: allocating memory from heap
Posted 25 December 2008 - 11:31 AM
Nope, although its good practice to do so immediately if not immediately following:
However if you aren't going to use it for a while, initalizing it to zero (effectively null) is also good:
int* p; //perfectly legal
However if you aren't going to use it for a while, initalizing it to zero (effectively null) is also good:
int* p = 0;
#8
Re: allocating memory from heap
Posted 25 December 2008 - 12:01 PM
KYA, on 25 Dec, 2008 - 08:41 AM, said:
This is an example in C (see code comments):
#include<iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
int n = 0;
cout<<"enter no. of columns/rows of square matrix"<<endl;
cin>>n;
int **p;
p = (int**)malloc(n*sizeof(int*)); //makes p[n], where n is the number of pointers, C++ requires cast
for(int i = 0; i < n; i++)
{
p[i] = (int*)malloc(n*sizeof(int)); //for each p[n]we make the second dimension (# of ints 'n')
}
int a = 0;
for (int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
p[i][j] = a++; //asign some values, never take actions on uninitialized dynamically allocated arrays
}
}
for (int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
cout << p[i][j] << " "; //print them out
}
cout << "\n";
}
for (int i = 0; i < n; i++)
{
free((int*)p[i]); //free up the array of pointers, must free each one otherwise memory will leak
}
free((int**)p); //free the handle to the multi dimension array
system("pause");
return 0;
}
Now hold on a second there KYA...there's no such thing as iostream in C. (at least as far as I'm aware.) Nor is there cin or cout...again, as far as I'm aware. I think yuo're a bit confused.
stdio.h and printf(), scanf() perhaps?
________________________________________________
@dan_ram:
When you declare a pointer, it's perfectly legal like KYA said to just do this -> int* p;, however, when you initialize it to something...you can initialize it 2 different ways.
int *p; // again...perfectly legal int a = 5; // regular int variable p = &a; // make the pointer 'p' point to the variable 'a' *p = 10; // make the value pointed at by 'p' equal to 10 cout << a; // I believe this will be 10...however, I'm not COMPLETELY sure.
Hope this helps!
This post has been edited by Locke37: 25 December 2008 - 12:07 PM
#9
Re: allocating memory from heap
Posted 25 December 2008 - 12:13 PM
dan_ram, on 25 Dec, 2008 - 12:16 PM, said:
int*p;
p=.....; ????????
p=.....; ????????
This is entirely correct. You have no clue what *p contains. I'd prefer this syntax.
int *p = NULL;
What you've done is declared a pointer and then assigned null to that pointer. However, in most cases, you can declare a value without assigning it anything. While I generally prefer to put a value in, pointers are a little confusing.
Here, it's clear that the address is being assigned to pointer p:
int n = 3; int *p; p = new int[n]; // setting the address
Here, it's doing some tricks:
int n = 3; int *p = new int[n];
Put another way,
int n = 3; // you can do this int *p = &n; // you can do this int *p; p = &n; // you can't do this int *p; *p = &n;
Confusing, no?
#10
Re: allocating memory from heap
Posted 25 December 2008 - 12:52 PM
Whoops, I should have said: C style functions, barring I/O processes.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote






|