473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How to deallocate a matrix

The program always crashs where the memory is deallocated. Any one could
help me? Thanks!

#include <iostream>

void main()
{
//Allocate
int **a=new int* [4];
for(int i=0;i<10;++i)
a[i]=new int [10];
for(int i=0;i<4;++i)
for(int j=0;j<10;++j)
a[i][j]=100*i+j;

for(int i=0;i<4;++i)
for(int j=0;j<10;++j)
std::cout<<a[i][j]<<std::endl;

//Deallocate
for(int i=0;i<4;++i)
delete [] a[i];
delete [] a; //crash at here!

}
Jul 22 '05 #1
4 2088
In article <ca***********@news.ntust.edu.tw>, Wei-Chao Hsu wrote:
The program always crashs where the memory is deallocated. Any one could
help me? Thanks!

#include <iostream>

void main()
Should be int main()
{
//Allocate
int **a=new int* [4];
for(int i=0;i<10;++i)
Is 10 the correct number here?
a[i]=new int [10];
for(int i=0;i<4;++i)
for(int j=0;j<10;++j)
a[i][j]=100*i+j;

for(int i=0;i<4;++i)
for(int j=0;j<10;++j)
std::cout<<a[i][j]<<std::endl;

//Deallocate
for(int i=0;i<4;++i)
delete [] a[i];
delete [] a; //crash at here!
It probably has something to do with the first for loop writing to
memory it doesn't own.
}


A tip:
#include <vector>
#include <iostream>

int main()
{
//allocate
std::vector<std::vector<int> > a(4, std::vector<int>(10));
for (int i=0; i < a.size(); ++i)
for (int j=0; j < a[i].size(); ++j)
a[i][j] = 100*i+j;
for(int i=0;i<4;++i)
for(int j=0;j<10;++j)
std::cout<<a[i][j]<<std::endl;

//deallocation: automatic
}

--
Robert Bauck Hamar
Jul 22 '05 #2
Ian
Wei-Chao Hsu wrote:
The program always crashs where the memory is deallocated. Any one could
help me? Thanks!

#include <iostream>

void main()
{
//Allocate
int **a=new int* [4];
for(int i=0;i<10;++i)
a[i]=new int [10];
Should that be for(int i=0;i<4;++i) ?

Ian
for(int i=0;i<4;++i)
for(int j=0;j<10;++j)
a[i][j]=100*i+j;

for(int i=0;i<4;++i)
for(int j=0;j<10;++j)
std::cout<<a[i][j]<<std::endl;

//Deallocate
for(int i=0;i<4;++i)
delete [] a[i];
delete [] a; //crash at here!

}

Jul 22 '05 #3
Wei-Chao > > //Allocate
int **a=new int* [4];
for(int i=0;i<10;++i)


Is 10 the correct number here?


It should be 4. Thanks!
Jul 22 '05 #4
Robert Bauck Hamar <ro**********@ifi.uio.no> writes:
A tip:
#include <vector>
#include <iostream>

int main()
{
//allocate
std::vector<std::vector<int> > a(4, std::vector<int>(10));
for (int i=0; i < a.size(); ++i)
for (int j=0; j < a[i].size(); ++j)
a[i][j] = 100*i+j;
for(int i=0;i<4;++i)
for(int j=0;j<10;++j)
std::cout<<a[i][j]<<std::endl;

//deallocation: automatic
}


Hmmh, your tip seems good when dealing with two dimension arrays. I'm wondering
whether there're some similar methods dealing with three, four, five,
.... dimension arrays. Huh?

--
William Xuuu
Humans remove heyyy_ from the email address to reply.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
5
by: Jason | last post by:
Hello. I am trying to learn how operator overloading works so I wrote a simple class to help me practice. I understand the basic opertoar overload like + - / *, but when I try to overload more...
20
by: mariano.difelice | last post by:
Hi, I've a big memory problem with my application. First, an example: If I write: a = range(500*1024) I see that python process allocate approximately 80Mb of memory.
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
3
by: kony | last post by:
Hi there, I would much appreciate your help with the following problem. Below is the code that uses a hash_map. I want to release all the memory occupied by the hash_map for other use. Apparently...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
2
by: DarrenWeber | last post by:
Below is a module (matrix.py) with a class to implement some basic matrix operations on a 2D list. Some things puzzle me about the best way to do this (please don't refer to scipy, numpy and...
0
by: DarrenWeber | last post by:
# Copyright (C) 2007 Darren Lee Weber # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free...
2
by: rijaalu | last post by:
I am designing a matrix class that performs addition, multicpication, substraction and division. When ever i complie the code it shows an error. include <iostream> using namespace std; class...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.