473,472 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Seg fault: invalid pointer error

3 New Member
i have a query ! the following code gives a segmentfault ! i tried debug mode and it says Invalid pointer please help !

Expand|Select|Wrap|Line Numbers
  1. class image
  2. {
  3.         public:
  4.         int **matrix;
  5.         int height,width;
  6.         int pixelmax;
  7.         image(int height,int width,int pixelmax)
  8.         {
  9.                 this->height = height;
  10.                 this->width = width;
  11.                 this->pixelmax = pixelmax;
  12.                 init_matrix();
  13.         }
  14.         void init_matrix()
  15.         {
  16.                 matrix = (int **)malloc(sizeof(int *)*width);
  17.                 for(int i=0;i<width;i++)
  18.                         matrix[i] = (int *)malloc(sizeof(int)*height);
  19.         }
  20.         ~image()
  21.         {
  22.                 if(matrix!=NULL)
  23.                 {
  24.                         for(int i=0;i<width;i++)
  25.                         {
  26.                                 free(matrix[i]);
  27.                         }
  28.                         free(matrix);
  29.                 }
  30.         }
  31. };
what is the problem ??
Oct 17 '11 #1
4 1776
Banfa
9,065 Recognized Expert Moderator Expert
Line 16/17: you allocate an array of width size and then accesses it for height items
Oct 17 '11 #2
Puneeth poduri
3 New Member
Oh ok but so far i have tested the code for a square matrix i.e. width = height
Oct 17 '11 #3
Puneeth poduri
3 New Member
The changes to the code have been made, the problem still persist. when i debug the program fails once my image reference goes out of scope . i.e. when the destructor is called. once i try to remove 'free(matrix[i])' it fails i.e. it says the matrix[0] is an invalid pointer .
Oct 17 '11 #4
weaknessforcats
9,208 Recognized Expert Moderator Expert
Please post your main(). The problem may be in how you use your class.
Oct 17 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: FKothe | last post by:
Hello together, the program below shows a behavior i do not understand. When compiled with the HX-UX11 c-comiler ( version B.11.11.04 ) v2.p in function test_it0 points to an invalid adress and...
3
by: JerryW | last post by:
I uninstalled/reinstalled .NET 2003 with no errors. When I try to create a Visual C# ASP.NET Web Application I get the error: "The Web server reported the following error when attempting to...
4
by: c language | last post by:
Hi All, I have written a C++ program, it can be compiled (in UNIX) but when I run it, I usually gives the following errors: free(): invalid pointer 0x51d4a0! free(): invalid pointer 0x51d460!...
6
by: KWienhold | last post by:
I'm currently working on a project in C# (VS 2003 SP1, .Net 1.1) that utilizes IStream/IStorage COM-Elements. Up to now I have gotten everything to work to my satisfaction, but now I have come...
2
by: Bill_DBA | last post by:
I have the following stored procedure that is called from the source of a transformation in a DTS package. The first parameter turns on PRINT debug messages. The second, when equals 1, turns on the...
1
by: mahiapkum | last post by:
hello all i have a code which looks fine when reviewed but when the application has a long run say for example of 2 days it gets exit, because of glibc error and the sample code is as follows: while...
0
by: P. Adhia | last post by:
Hello, I was wondering if anyone is successfully using using Python(2.5)+DB2+pydb2. I get an error in all situations. It seems that this problem might be limited to python 2.5. A quick Google...
0
by: aahsampath | last post by:
I have a main web page and when I click on a link on it, it will popup another modal screen to select some datas(data can select using check boxes and there is a select all function also) and it will...
3
by: Renzr | last post by:
I have a C++ package which works very well in the 32-bit Linux-like OS. However, it will lead to a "*** glibc detected *** ./ex2: munmap_chunk(): invalid pointer" in 64-bit (Fedora 7-64), when it...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.