473,386 Members | 1,823 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,386 software developers and data experts.

Dynamic memory allocation - memory corruption error

Hi there,

I have a problem with dynamic memory allocation. I know that it would have been easier to use vectors methods, but i want to know what i do here wrong.

This is one of my methods in t_Item class - I use it to store Item Objects (which are classes too).

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

class t_Item {
public:
t_Item();
Allocate_Item();

private:
Item **collection;
int item_number;
};

t_Item::Allocate_Item()
{

if(item_number == 0)
{
collection[item_number] = new Item;
item_number++;
}
else
{

// creating buffer - temporary space where ill hold pointers to already existing Items

Item **buffor;
buffor = new Item*[item_number];

for(int yz=0; yz <= item_number; yz++)
buffor[yz]=NULL;

//Copying pointers from collection to buffor
for ( yz=0; yz < item_number; yz++)
{
buffor[yz] = collection[yz];
}

// Here is the problem - when i try to delete 'collection' memory corruption error occurs when i skip it it will proceed further without any problems but thats not the way it should be. WHen i remove it, program runs without any problems, but but isnt that memory leak ?
delete [] collection;


//Creating bigger collection - to store another Item

collection = new Item*[item_number];
for(yz=0; yz <= item_number; yz++)
collection[yz]=NULL;

// Copying pointers from buffor to collection, which can now have one Item more, we create after copying

for (yz=0; yz<item_number; yz++)
{
collection[yz] = buffor[yz];
}
collection[litem_number]=NULL;

// here we create new item - function is correct. We add one item more
collection[item_number]= new Item;
item_number++;

// Again the same problem - when i try to delete 'buffor' memory corruption error occurs
delete [] buffor;
}
}

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Anyone knows where is the problem ??

BTW1: This is my first post so HELLO WORLD to all programing maniacs out there

BTW2: Sorry for my english - i know its very very bad.... :/
Nov 19 '06 #1
4 2771
Help me :)
Nov 20 '06 #2
I will really really appreciate help in this task
Nov 20 '06 #3
Banfa
9,065 Expert Mod 8TB
The problem stems from the declaration

Item **collection;

and how you are using it. Sometimes you use it as though it were a Item **, sometime as though it were Item *.

What construct are you trying to produce? A dynamic array with 2 indexes (in which case Item ** is correct) or a dynamic array with 1 index (in which case collection should be Item *).

The actual reason you are getting the errors is that in on place you allocate by

collection = new Item;

and then you

delete[] collection.

i.e. you allocate a single instance but when you delete you indicate that you allocated an array.

For the current code I think

collection = new Item;

should be

collection = new Item *[1];
Nov 21 '06 #4
Collection is a pointer to dynamic array of pointers to Objects - Items. I need it that way because i want to place all objects on the heap (not in the operational memory) and i want to be able to reallocate dynamically size of a table where i hold pointers to objects(collection).

In constructor I'm declaring that variable for the first time - i forgot to copy/paste it.

Expand|Select|Wrap|Line Numbers
  1. t_Item::t_Item()
  2. {
  3.     liczba_item=0;
  4.     zbior = new Item*[liczba_item];
  5.     cout << "n\ninicjalizacja tablicy\n" << this;
  6. }
  7.  
The solution you subimted isn't working too... for any other ideas i will be very thankfull?
Nov 21 '06 #5

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

Similar topics

6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
5
by: RoSsIaCrIiLoIA | last post by:
why not to build a malloc_m() and a free_m() that *check* (if memory_debug=1) if 1) there are some errors in bounds of *all* allocated arrays from them (and trace-print the path of code that make...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
10
by: eyh5 | last post by:
Hi, My C code (running on Soalris Unix) has some "segmentation fault" that I wish to use purify to do it. I poked around the web, and found some information about adding some lines in a Makefile...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
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...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.