|
I have been adding to the mini program and discovered that the violation first shows up when I try to initialize a large array that has been dynamically allocated. All the dynamically allocated arrays, except the last 2 are intialized without a problem. I have checked the programming and it is the same for the last 2 arrays as for all the others (I copied and pasted then changed only the names). Could the access violation occur because of a lack of memory? If so how could I check?
If not any other ideas?
simaneal
|
|
|
|
|
simaneal wrote:
Could the access violation occur because of a lack of memory?
Doubtful, unless you are talking about several hundred MB. Most likely you are writing to an area that does not belong to your program. Sometimes the effect does not show up until further downstream. Show me the array(s) and how you are initializing them.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Here is the code for allocating memory usinf malloc and for intializing the arrays The problems arrizes when I try initializing the last 2 arrays allocated.
//allocation of dynamic memory forfirst of 20 sets (60 arrays total)
int *pix1;
if ((pix1 = (int*)malloc(array_size * sizeof(pix1)))== NULL) {
(void)printf("ERROR: Malloc failed");
(void)exit(EXIT_FAILURE); /* or return EXIT_FAILURE; */
}
int *pixm1;
if ((pixm1 = (int*)malloc(array_size * sizeof(pixm1))) == NULL) {
(void)fprintf(stderr, "ERROR: Malloc failed");
(void)exit(EXIT_FAILURE); /* or return EXIT_FAILURE; */
}
int *pixhigh1;
if ((pixhigh1 = (int*)malloc(array_size * sizeof(pixhigh1))) == NULL) {
(void)fprintf(stderr, "ERROR: Malloc failed");
(void)exit(EXIT_FAILURE); /* or return EXIT_FAILURE; */
}
//initialization of 1 of 20 sets
for(y=0; y
|
|
|
|
|
What is the value of array_size , and why multiply it by 4?
In the for loop, what is the value of ycoor ?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
The array_size is 117 (the number of elements in the array)
Is the 4 your talking about the size of an integer? Wouldn't you have to allocate enough memory for each element (117) at size int(4)?
Am I doing that wrong? If so how should it be done?
The ycoor value is 117, also the number of elements in the array.
Sorry about the code. Next time I will remember to tag it.
simaneal
|
|
|
|
|
simaneal wrote:
Is the 4 your talking about the size of an integer?
Yes, but you coded the size of a pointer. It just so happens that both are 4 bytes!
simaneal wrote:
Am I doing that wrong? If so how should it be done?
No, you're right, but it helps to be able to explain why you are doing something a certain way.
If you omitted this memory allocation and initialization code, would the problem (i.e., fscanf() crashing) persist?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
On the mini program if I eliminate the initialization the problem dissappears. I can not eliminate the initialization and allocation on the large program and still have it do what it was designed to do. I need the arrays to store my solution sets. If you have any other ways of storing and outputting large data sets of integers I would be happy to hear it.
simaneal
|
|
|
|
|
Since your code is only allocating about 1400 bytes from the heap, I doubt very seriously that's the problem. You might want to comment out just the initialization code and see what that does. If it starts working, that would indicate that the initialization code is writing past one of the three arrays, and messing with memory that fscanf() is later using. Are there any other places that write to pix1 , pixm1 , or pixhigh1 ?
Also, why are the printf() and exit() calls preceeded with void ?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
pix1, pixm1 and pixhigh1 are used in several other places. There values change based on several calculation results.
the printf and exit codes use voids because that is how I was shown to do it. Is there a better way? Is that wrong?
simaneal
|
|
|
|
|
Followig the local variables using breaks most of the arrays are initialized ok, but their values change (to cxx0030:Error:expression can not be evaluated)latter when they are not being called. I have also noticed that a variable used in a for loop changed when it should not have it went from 116 (which is what it should be) to 116547629.
Can you think of anything that could cause these changes?
simaneal
|
|
|
|
|
simaneal wrote:
I have also noticed that a variable used in a for loop changed...to 116547629.
Nothing special about that number.
simaneal wrote:
Can you think of anything that could cause these changes?
Yes, something like:
for (int x = 0; x < 117; x++)
{
int &r = x;
int *p = &x;
r = 116547629;
*p = 116547629;
}
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
I am tring to fill the buffer with text then output that information to a text file. Then line
int nBytesToWrite = FillBuffer(1024);
Gives me an error "FillBuffer: identifier not found"
Am I missing a header? I am using C++ .net to compile.
|
|
|
|
|
Is FillBuffer() a method of BinaryReader ? If so, where's the object at?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Here is the code for my prog.
#include <iostream>
#include <iomanip>
#include <fstream>
#include "afx.h"
using namespace std;
int main(int argc, char* argv[])
{
int nSizeLimit = 1000;
int nBytesWritten = nSizeLimit + 1;
CFile outFile;
while(int i=0<10000)
{
if(nBytesWritten > nSizeLimit)
{
CString strFileName;
strFileName.Format("LittleFile%d.txt", nBytesWritten++);
if(outFile.m_hFile != CFile::hFileNull)
outFile.Close();
outFile.Open(strFileName, CFile::modeWrite | CFile::shareDenyWrite);
nBytesWritten = 0;
}
char buf[512];
int nBytesToWrite = FillBuffer(1024);
if(nBytesToWrite == 0)
break;
outFile.Write(buf, nBytesToWrite);
nBytesWritten += nBytesToWrite;
}
return 0;
}
|
|
|
|
|
Andrew Admire wrote:
while(int i=0<10000)
What is the purpose of this? Initialize i and compare it to 10000 in two separate statements like:
int i = 0;
while (i < 10000)
... That aside, the variable i is never incremented.
Andrew Admire wrote:
outFile.Open(strFileName, CFile::modeWrite | CFile::shareDenyWrite);
What if the file does not already exist? Don't you want to create it, too?
Andrew Admire wrote:
char buf[512];
What does this variable do? It's not being assigned anything.
Andrew Admire wrote:
int nBytesToWrite = FillBuffer(1024);
I already asked about FillBuffer() . You are using it incorrectly. Why is it even there?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
This is a sample code someone gave me. It should create multiple files. So, for the fillbuffer part... I have no idea. The char buf array, I think it holds the words before sending it to file. As for the file... yes if it does not exist I would like to create it.
|
|
|
|
|
Andrew Admire wrote:
The char buf array, I think it holds the words before sending it to file.
Where do these words come from?
Andrew Admire wrote:
As for the file... yes if it does not exist I would like to create it.
Then you'll also need to use CFile::modeCreate .
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
I'm trying to create a LM database using text files.
|
|
|
|
|
I'm not familiar with an LM database. Is that:
London Medical
Literary Machine
List Manager
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
|
hi, i have two classes in my program: class1 and class2. if i want to have them both include pointers to each other, how do I do that? one class must always be declared before the other. here is some example code::
main.cpp
#include "class1.h"
void main() {
class1 test;
}
class1.h
#pragma once
#include "class2.h"
class class1 {
private:
class2 *child1;
class2 *child2;
public:
class1() {
child1 = new class2();
child1->Init();
child2 = new class2();
child2->Init();
}
};
class1.cpp
#include "class1.h"
class2.h
#pragma once
#include "class1.h"
class class2 {
private:
class1 *parent;
public:
void Init(class1 *the_parent) {
parent = the_parent;
}
};
class2.cpp
#include "class2.h"
Here is a snapshot of compiler output:
Compiling...
class2.cpp
class1.h(5) : error C2143: syntax error : missing ';' before '*'
class1.h(5) : error C2501: 'class1::class2' : missing storage-class or type specifiers
class1.h(5) : error C2501: 'class1::child1' : missing storage-class or type specifiers
class1.h(6) : error C2143: syntax error : missing ';' before '*'
class1.h(6) : error C2501: 'class1::class2' : missing storage-class or type specifiers
class1.h(6) : error C2501: 'class1::child2' : missing storage-class or type specifiers
main.cpp
class2.h(5) : error C2143: syntax error : missing ';' before '*'
class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers
class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers
class2.h(7) : error C2061: syntax error : identifier 'class1'
class1_1.cpp
class2.h(5) : error C2143: syntax error : missing ';' before '*'
class2.h(5) : error C2501: 'class2::class1' : missing storage-class or type specifiers
class2.h(5) : error C2501: 'class2::parent' : missing storage-class or type specifiers
class2.h(7) : error C2061: syntax error : identifier 'class1'
one class's signature must be declared before the other, but they both reference each other.. how can this be done?
|
|
|
|
|
class1.h
#pragma once
#include "class2.h"
class class2;
class class1 {
private:
class2 *child1;
class2 *child2;
public:
class1()
{
child1 = new class2();
child1->Init();
child2 = new class2();
child2->Init();
}
};
Cleek | Image Toolkits | Thumbnail maker
|
|
|
|
|
thanks so much! first time i got an answer in a forum in only 2 minutes
|
|
|
|
|
You need to use forward declarations.
Try this:
<br />
#pragma once<br />
<br />
class class2;<br />
<br />
class class1<br />
{<br />
private:<br />
class2* child1;<br />
class2* child2;<br />
<br />
public:<br />
class1()<br />
{<br />
child1 = new class2(this);<br />
child2 = new class2(this);<br />
}<br />
};<br />
<br />
#pragma once<br />
<br />
class class1;<br />
<br />
class class2<br />
{<br />
private:<br />
class1* parent;<br />
<br />
public:<br />
class2(class1* the_parent) : parent(the_parent)<br />
{<br />
}<br />
};<br />
--
jlr
http://jlamas.blogspot.com/[^]
|
|
|
|
|
that appeared to solve my problem, but now i have a new one.
i get error C2512, no default constructor available, but there is a public constructor clearly defined with no parameters for both classes. this error occurs in the constructor for struct1. here is the code:
main.cpp
#include "class2.h"
void main() {
struct1 *config;
config->item = new class2();
config->item->Init(config);
}
struct1.h
#pragma once
#include "class2.h"
class class2;
struct struct1 {
private:
class2 *item;
public:
struct1() {
item = new class2(); /* ERROR OCCURS HERE while
compiling main.cpp and class2.cpp */
}
};
class1.h
#pragma once
class class1 {
public: class1(){}
};
class2.h
#pragma once
#include "class1.h"
#include "struct1.h"
struct struct1;
class class2 : public class1 {
private:
struct1 *cfg;
public:
class2() {}
void Init(struct1 *data) {
cfg = data;
}
};
struct1.cpp
#include "struct1.h"
class1.cpp
#include "class1.h"
class2.cpp
#include "class2.h"
|
|
|
|