473,725 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic memory allocation - memory corruption error

5 New Member
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).

xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx xxx

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

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

t_Item::Allocat e_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;
}
}

xxxxxxxxxxxxxxx xxxxxxxxxxxxxxx

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 2792
Tomassus
5 New Member
Help me :)
Nov 20 '06 #2
Tomassus
5 New Member
I will really really appreciate help in this task
Nov 20 '06 #3
Banfa
9,065 Recognized Expert Moderator Expert
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
Tomassus
5 New Member
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(collect ion).

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
8210
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 allocation to create object of that class ? 2. If it says "dynamic memory allocation", is it mean the following code : DestinationAddress* dest = new DestinationAddress(); // code 1
5
307
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 the error and exit) just when malloc_m and free_m start. (I use a 1100 static array for write the path (like a queue) of called function, operator, etc and write using '+' where is find the memory error) 2) if the pointer to free is alredy...
5
3758
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 believe the problem centers around my handling of strings, arrays, pointers and dynamic memory allocation. Here is the problem I'm trying to solve: I want to fill a list box with a list of Project Names from a database (in Palm this is more...
10
14043
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 file to use purify. However, my code is a rather simple single-source C program, and I didn't write a Makefile for it. I'm wondering if anybody can tell me which commands are to be entered at the Unix prompt to use purify. And, I don't know if...
8
3412
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 ? In nut shell, what is/are the realtion/s between the Memory Leak and Memory Corruption. Juts Theoritical Assumtion below:
11
3048
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
19085
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 is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks.
1
7970
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 compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
14
3827
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 this stored in stack. If yes whether it will be deleted on exiting from the function. is dynamic memory allocation needed for this purpose
0
8888
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6702
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3221
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 we have to send another system

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.