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

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(tempMetaSegment); // 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 1434
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(tempMetaSegment); // 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(thesize)'. 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(thesize) 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
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...
15
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
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
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...
9
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,...
4
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,...
11
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
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...
18
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...
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: 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
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.