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

C++ buggy code - new/delete/ifstream/valgrind. Please help I'm a newbie!!

Hi there, I'm new to this forum & to programming in general, and am really stuck with a piece of buggy code. Can anyone help? I have several problems which may or may not be related. I have written a piece of code in Visual Studio and now have to get it to run under Linux. Here is the problematic section:
Expand|Select|Wrap|Line Numbers
  1. ifstream* seq_input = new ifstream(fname.c_str());
  2. if(seq_input->is_open()==false) {cerr << "File not found";exit (1);}
  3. int i, clen=2, pos, str;
  4. struct data *ndata;
  5. struct sequence *new_seq;
  6. ndata=new data;    
  7. ndata->maxlseq=0;
  8. ndata->nseq=0; 
  9. ndata->type=type;
  10. ndata->seqs=new sequence*;
  11. while(!seq_input->eof()) { 
    str = seq_input->get(); 
    if ((char)str == '>'){
  12. string name;
  13. getline(*seq_input, name);
  14. int namesizing=name.size();
  15. new_seq= new sequence;
  16. new_seq->seq=new int[clen];
  17. new_seq->name=new char[namesizing];
  18. new_seq->length = 0;
  19. new_seq->namesize=0;            
  20. new_seq->namesize=namesizing;
  21. ndata->nseq++;
  22. ndata->seqs[ndata->nseq]=new_seq;
  23. pos=0;
  24. }
  25. else if (str == ' ' || str =='\t' || str == '\n') ;  
  26. else if (ndata->type==1){
    pos++;
  27. switch(str) {
  28. case '?': case '-': 
  29. ndata->seqs[ndata->nseq]->seq[pos]=0;
  30. break;
  31. }
  32. ndata->seqs[ndata->nseq]->length = pos;
  33. }
  34. if (pos>(clen-10)) {
    clen *= 2;
  35. ndata->seqs[ndata->nseq]->seq = (int *) realloc(ndata->seqs[ndata->nseq]->seq, clen*sizeof(int));
  36. }
  37. if (ndata->seqs[ndata->nseq]->length > ndata->maxlseq) {
    ndata->maxlseq=ndata->seqs[ndata->nseq]->length;
  38. }
  39. ndata->seqs[ndata->nseq]->length-=1;  
  40. seq_input->close();  
  41. delete seq_input;
  42.  

Problem #1: Under Visual Studio, I am having problems deleting the pointer to seq_input. I have recently done some memory leak debugging and this is the only leak left. With this code the program crashes on delete seq_input with the error message 'unhandled exception/access violation'. This occurs on the core code line 'sizeNext=pNext->sizeFront'. Any ideas as to why this is happening?

Problem #2: Under Unix command line, the program crashes at the end of the for loop on the line pos=0 with the error message double free or corruption. I haven't deleted anything by this point, so have no idea how to fix this.

Problem 3: To investigate these problems further I tried to download Valgrind but I am having trouble running my (compiled under unix) program on it, it gives me the error message 'can only handle 32-bit executables'

Any suggestions on any of these problems would be hugely appreciated. PS. I know I'm not supposed to use realloc with new/delete. Do you think this is the problem? My coding capabilities don't stretch to working out how not to use it. If I use a vector class instead I can't get it into the structure where it needs to be!

Many thanks...... :)
May 11 '06 #1
3 6190
Banfa
9,065 Expert Mod 8TB
Problem #1: Under Visual Studio, I am having problems deleting the pointer to seq_input. I have recently done some memory leak debugging and this is the only leak left. With this code the program crashes on delete seq_input with the error message 'unhandled exception/access violation'. This occurs on the core code line 'sizeNext=pNext->sizeFront'. Any ideas as to why this is happening?
The posted code does not contain sizeNext=pNext->sizeFront but the other errors you have(probem 2) are so serious that you should fix them first as they are producing undefined behaviour (which is bad).

Problem #2: Under Unix command line, the program crashes at the end of the for loop on the line pos=0 with the error message double free or corruption. I haven't deleted anything by this point, so have no idea how to fix this.
The problem is with the code line

ndata->seqs[ndata->nseq]=new_seq;

you have new'd ndata->seqs as

ndata->seqs=new sequence*;

This is an array of 1 entries for seqs so in the first line if ndata->nseq is anything other than 0 you are writing to unallocated memory. You might be better off having a linked list, list or vector of sequences rather than trying to dynamically allocat this array, which you don't know your required size of.

You also do not protect against the case of pos going higher than clen which would also result in a write to unallocated memory.

Problem 3: To investigate these problems further I tried to download Valgrind but I am having trouble running my (compiled under unix) program on it, it gives me the error message 'can only handle 32-bit executables'
I have no experience with this utility but I would guess that maybe your system is 16bit, or can handle 16bit code and you have compiled using a compiler or compile options that produce 16 bit code. Check your compiler documentation.
May 11 '06 #2
Hi Banfa,
Thanks for your reply,

The posted code does not contain sizeNext=pNext->sizeFront
Sorry I should have been more clear, this code originates not from my code but from Visual Studio's SBHEAP.C file. I agree with you though that there is unpredictable behaviour going on and that that is the main priority.

The problem is with the code line

ndata->seqs[ndata->nseq]=new_seq;

you have new'd ndata->seqs as

ndata->seqs=new sequence*;

This is an array of 1 entries for seqs so in the first line if ndata->nseq is anything other than 0 you are writing to unallocated memory.
I forgot to mention that data and sequence are both defined as structures in my .h files:
Expand|Select|Wrap|Line Numbers
  1. struct sequence{
    char *name
  2. int namesize
  3. int length
  4. int *seq
  5. };
  6. struct data{
    int nseq
  7. int maxlseq
  8. struct sequence **seqs
  9. int type
  10. };
  11.  
I'm not sure whether this makes a difference to what you are saying about ndata->seqs being an array of one. I was under the impression that ndata->seqs and new_seq were the same type (structures), and this line maps one onto the other sequentially through the while loop, and size of new_seq determines the size of ndata->seqs. This code does work in visual studio so it isn't something immediately obvious that is wrong...but maybe unix just doesn't get this :o .
You might be better off having a linked list, list or vector of sequences rather than trying to dynamically allocat this array, which you don't know your required size of. You also do not protect against the case of pos going higher than clen which would also result in a write to unallocated memory.
I will do some more investigation into this, thank you.
I have no experience with this utility but I would guess that maybe your system is 16bit, or can handle 16bit code and you have compiled using a compiler or compile options that produce 16 bit code. Check your compiler documentation.
I am using the g++ compiler under Linux x86. Do you know how I could check whether it is producing 16 bit code? Sorry for all the ignorance :o - and thanks again for your help
May 11 '06 #3
Banfa
9,065 Expert Mod 8TB
Sorry been trying to get back to this thread but getting distracted by other things (babies, life etc :D)

Have you managed to fix your problems yet or do you still need some help?
May 15 '06 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: sachin bond | last post by:
The following code(in c++) is supposed to divide a file into 'n' different files. suppose i'm having a file called "input.zip". The execution of the following code should divide "input.zip"...
3
by: sachin bond | last post by:
this code does not work.... plz help...... void breaker() {
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
9
by: Robert Schneider | last post by:
Hi to all, I don't understand that: I try to delete a record via JDBC. But I always get the error SQL7008 with the error code 3. It seems that this has something to do with journaling, since the...
3
by: talefsa | last post by:
Hi, I'm trying to figure out the origin of a memory corruption in my code using valgrind. valgrind gave me a number of messages that didn't help much, the first invalid read/write message is as...
5
by: mastern200 | last post by:
I need to make a program where it can read from a file and write to it. But the problem is, all i can do is read from it and not write to it. This is the code i have so far: # include <iostream>...
11
by: rm | last post by:
There is a Linux forum that I frequent from time to time on which I mentioned a couple of scripts that I wrote. The editors of a small Linux magazine heard and found them interesting enough to ask...
3
by: SM | last post by:
I'm using simpleXML in PHP and i Can't get this small piece of code to work? Need help I have an XML that looks like this: <?xml version="1.0" encoding="utf-8"?> <VIDEO> <item>...
19
by: fx5900 | last post by:
Hi, i am trying to convert an .osm (openstreetmap) file into gml format and finally to shapefile given this wiki info http://wiki.openstreetmap.org/index.php/GML. I'm using windows and when i...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.