init_ptr() is of type T* , and ptr is of type T*, i dont understand why this isnt working.
it worked before without templates, specifying the type of the pointer, so i can only assume my usage of templates is incorrect. can anyone shed any light on this?
even if i comment out all the code in the constructors except the Size = 0; or = s; it still segfaults.
i guess it has to be a problem with the template usage.
i thought that might be the problem as well, but it does it even if i set the size to something bigger than zero, it also does it if there is no pointer initiliazation at all.
If you weren't using it in the correct way it'd give compilation errors, not crashes. They're very picky.
if (loc > Size)
should be
if (loc >= Size)
but that may not be the problem either. We really need to see ALL the code.
Also:
T Dynamicarray<T>::get_val(int loc)
should be
T &Dynamicarray<T>::get_val(int loc)
That way, it doesn't copy the object. That can cause crashes when the object itself contains allocated things -- when the copy goes out of scope, it frees the resources for both, and the next time you use it, you're using data that's been freed then freeing data that's already been freed...
well i wrote another class using templates to see if there was something i was doing wrong there, and thats not the problem. im going to have to rework the Dynamicarray code all together.
alright well it seems that the problem isnt a problem with code, if i leave the class declaration and implementation in a cpp file with main() it works fine, if i put the class deff and imp in a .h and include it that way, thats when it simply dosnt work, the program hangs, or during certain conditions ill get the glibc detected double free or corruption error.
i am aware that when using templates that you must keep the definition and declaration in the same file, but as for what file it must be in i thought could be any.
I dunno. If you can't track down exactly where the crash is happening, it'll be very hard to tell anything... can you compile it with the '-ggdb' flag, then run it like this:
gdb ./program
gdb prompt> run
crash error message
gdb prompt> bt f
full backtrace printout
gdb prompt> quit
I also notice that, since you're using cout, your messages aren't necessarily getting printed when you think they are -- it buffers. If it crashes before the buffer is flushed, it won't get printed, even if the cout call happened first. Even explicit flushing doesn't seem to help that on some systems. Try fprintf instead:
I'm a C++ beginner programmer. I'm using C++ on Unix with the gcc version 3.4.5 20051201 (Red Hat 3.4.5-2).
I have the same error message with my simulation program. When this error happens then the simulation stops running with the message "Abort".
The simulation is supposed to run 50 replicates. This problem occurs randomly. Sometimes, it happened in the 32th and 37th replicates. Other time, it happened in the 3rd replicate.
So, I tried to debug the program by running the program step by step. Surprisingly, the program ran fine. No error.
Or when I tried to run the replicate that had this problem, then it ran fine, too. But not with 50 replicates in a row.
My friend told me maybe it had something to do with optimization. I even lowered the level of optimization from -O to -O0. It didn't help.
Could somebody educate me what is going on? Where should I look at?
youll have to post the code where the problem occurs, if you can determine where that is.
in my case, (i havent solved the problem yet) it seems like my system dosnt like templates in header files. my code runs fine with no problems if i have the class declared and implemented in the same file as main().
This is reproducible every time for me. It makes me think I'm missing something about malloc...
I started getting the error when I implemented the + operator.
g++ --version
g++ (GCC) 4.1.1 20061011 (Red Hat 4.1.1-30)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
main.cpp:
#include <iostream>
using namespace std ;
template <class T>
class MyStack
{
public:
MyStack(int = 10);
~MyStack() { delete [] stackPtr ; }
int push(const T&);
int pop(T&);
int isEmpty() const { return topnum == -1; }
int isFull() const { return topnum == size - 1; }
MyStack operator + (MyStack);
int getSize() { return size; }
private:
int size;
int topnum;
T* stackPtr;
};
The problem seems to be your member function definition:
MyStack operator + (MyStack);
It should use a reference of MyStack as the argument as follows:
MyStack operator + (MyStack&);
Without the &, a copy of MyStack object is made and it contains a pointer to the private data member T* stackPtr. When the copy of the MyStack goes out of scope in the member function, the destructor of the MyStack object copy is called and the stackPtr is deleted. This also deletes the stackPtr of the original object because your class does not define a copy constructor to make a deep copy of the stackPtr. Then when your original object's destructor is called later, it does "delete [] stackPtr" again on an already deleted pointer. Thus the error appears.
Hi Everybody;
I am struggling for a problem in my code.I couldn't understand the source of the problem.Could you please help me?
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
int main (void) {
DIR *dp;
struct dirent *ep;
char path[35] = "../";
char tempPath[35] = "../";
struct stat buf;
int result;
mode_t tempModeOfFile;
dp = opendir (path);
if (dp != NULL) {
while ( ep = readdir (dp) ){
printf("%s ",ep->d_name);
printf("%d -- %d\t",result,buf.st_size);
strncat(tempPath,ep->d_name,strlen(ep->d_name) );
printf("Step2:tempPath: <<%s>> \n",tempPath); //## ge�ici satr
if( lstat(tempPath,&buf) == -1){
//EX 3
printf("lstat error has occured.Program will terminated.");
return -8;
}
tempModeOfFile = buf.st_mode;
strcpy( tempPath,path);
}
(void) closedir (dp);
}else
perror ("Couldn't open the directory");
result = closedir(dp);
printf("\n closing directory: %d",result);
return 0;
}
In working directory, there are 20 files.This code reads these 20 files successfully, but after 20th file, it can not exit from the "while" loop, instead, gives error below :
you are asking your question in someone else thread, that is not best way to get answers
the code posted has several glaring holes, such as variables used before they are set, the closedir is called twice(?), the buffer sizes are making it prone to overflow. Is this a homework? Are you trying to learn dirent family functions?
Hello Mr Migurus;
This is a homework, but my aim is not understand the dirent family.In a part of my experiment, I have to use this.I had mistakenly close directory twice ,so it is source of problem.When remove the one of it , the problem had been solved.So I would like to appreciate you.
You had said:"you are asking your question in someone else thread, that is not best way to get answers". I couldn't understand what do you mean.
This is not my whole code ,this is a one section.So you can see variables used before they are set.
But in your message, you had mentioned about "the buffer sizes are making it prone to overflow".
Could you please describe this a bit comprehensively?
Thank you.