473,588 Members | 2,474 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory overwritten with structures

Dear cpp-ians,

I have two structures:

struct metaSegment
{
vector <long double> mean;
bool full;
};

struct segment
{
segmentIter it_meta_segm;
};

and both are linked via the it_meta_segm:

typedef vector <metaSegment>:: iterator segmentIter;
In my program I have:

div_t divresult;
vector <metaSegment> X;
segmentIter itX;
segment *** Y;

for (int NbPixels = 0; NbPixels<10000; NbPixels++)
{
// create temporary segment
metaSegment tempMetaSegment ; // Create temporary metaSegment
divresult = div(counter,100 ); // Retrieve ...
row = divresult.quot; // ... row number and ...
column = divresult.rem; // ... column number.
for (int layer=0; layer<5; layer++)
{
tempMetaSegment .mean.push_back (image[layer][row][column]);
// Assign image[layer][row][column] values...
// ...(long double type) to vector <long double>...
// ... mean of tempMetaSegment
}
tempMetaSegment .full = false; // Assign full value
X.push_back(tem pMetaSegment); // Put tempMetaSegment in...
// ...the vector <metaSegment> X
itX = --X.end(); // Take the last element
Y[row][column]->it_meta_segm = itX; // of X and assign it to Y.
}

When I retrieve now the values of Y[row][column]->it_meta_segm->full,
then I see they do not fullfill my expection of being false (as I
assigned them).
I assume my values are overwritten somewhere in my memory, but I don't
see where I go wrong?

Any advice?

Kind regards and thank you very much in advance,
Stef

Jul 23 '05 #1
2 1453
steflhermitte wrote:
Dear cpp-ians,

I have two structures:

struct metaSegment
{
vector <long double> mean;
bool full;
};

struct segment
{
segmentIter it_meta_segm;
};

and both are linked via the it_meta_segm:

typedef vector <metaSegment>:: iterator segmentIter;
In my program I have:

div_t divresult;
vector <metaSegment> X;
segmentIter itX;
segment *** Y;

for (int NbPixels = 0; NbPixels<10000; NbPixels++)
{
// create temporary segment
metaSegment tempMetaSegment ; // Create temporary metaSegment
divresult = div(counter,100 ); // Retrieve ...
row = divresult.quot; // ... row number and ...
column = divresult.rem; // ... column number.
for (int layer=0; layer<5; layer++)
{
tempMetaSegment .mean.push_back (image[layer][row][column]);
// Assign image[layer][row][column] values...
// ...(long double type) to vector <long double>...
// ... mean of tempMetaSegment
}
tempMetaSegment .full = false; // Assign full value
X.push_back(tem pMetaSegment); // Put tempMetaSegment in...
// ...the vector <metaSegment> X
itX = --X.end(); // Take the last element
Y[row][column]->it_meta_segm = itX; // of X and assign it to Y.
You're holding onto an iterator value here. Since it's an iterator of
a vector, it can go bad (become invalid) after any 'push_back' operation
if that operation involves reallocation. I don't see any attempt to
prevent reallocation by reserving the size of the vector 'X', so your 'Y'
elements are going bad without you ever knowing about it.
}

When I retrieve now the values of Y[row][column]->it_meta_segm->full,
then I see they do not fullfill my expection of being false (as I
assigned them).
I assume my values are overwritten somewhere in my memory, but I don't
see where I go wrong?

Any advice?


If you know the size of 'X' beforehand, use 'X.reserve(thes ize)'. If you
don't know the size, store _indices_ and not iterators. If you can't do
that, change 'X' from 'vector' to 'list', the iterators of 'list' do not
go bad when you push_back.

V
Jul 23 '05 #2
Thank you very much Victor!

X.reserve(thesi ze) works perfectly. I did not now that reallocation can
make an iterator go bad.

Jul 23 '05 #3

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

Similar topics

1
3341
by: Grisha Golberg | last post by:
Hi all, I am trying to move a database from one machine to another. I backed up in offline mode, then I do a restore (without rolling forward), which finishes successfully but with warning 2529 (what's that?) After that db2syscs hogs pretty much all available memory on the machine whenever I attempt to connect to the database, with no end in sight.
15
1586
by: berthelot samuel | last post by:
Hi, I'm trying to develop an application for modeling 3D objects from Bezier patches, but I have a memory allocation problem. Here are my structures: typedef struct _vector3 { union { struct {
5
3585
by: Migrators | last post by:
1)How is the memory structure organised in C. i.e., the way in which the stack and heap memory are used in C. 2) What will be the value of EOF.
5
1898
by: mikegw | last post by:
Hello all. I am currently using an implementation of sysV shared memory. The entire shared memory is allocated is one continuous block of which I get the pointer to the head, everything should be done as offsets from this. At the moment I have two data structures, a head to a linked list which in it contains the pointer to the first element in the linked list( this is redundant as far as I can work out) and the list itself. The data...
9
2422
by: CptDondo | last post by:
I am working on an embedded platform which has a block of battery-backed RAM. I need to store various types of data in this block of memory - for example, bitmapped data for control registers, strings for logging, and structures for data points. I want to use one function to read data from this block and one function to write data, for example: sram_read(OBJECT_IDENTIFIER) would return a pointer to the appriate object and
4
1279
by: Anil Aksay | last post by:
I need to put a breakpoint when a particular memory location is written. To be more specific I want to know when 0 is written onto that memory location. Is there a way to do it in vc7? Regards, Anil.
11
3764
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
94
4682
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring that I found inside of a string. Any ideas?
18
2619
by: MajorSetback | last post by:
I am using the Redhat version of Linux and GNU C++. It is not clear to me whether this is a Linux issue or a C++ issue. I do not have this problem running the same program on Windows but tweaking the C++ code appears to fix the problem on Linux. I have a static array that is declared privately in one class and a structure that is declared publicly in another class. The program uses both classes. I was getting core dumps and traced...
0
7929
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
8357
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
7987
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6634
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5398
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
3847
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...
1
2372
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
1
1459
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1196
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.