Segment fault for C++ program when return vector


 
Thread Tools Search this Thread
Top Forums Programming Segment fault for C++ program when return vector
# 8  
Old 10-28-2014
A better translation of my program:

Code:
#include <iostream>
#include <string>
#include <ctype.h>

using namespace std;

// Making it global is important if you want unused elements
// to default to 0 and not garbage!
unsigned char m[256];

// Passing by reference avoids making a copy of a copy of a copy.
void revcomp(const string &input, string &output) {
        int n=input.length(), p=-1;

        output=input;
        while(n > 0) output[++p]=m[toupper(input[--n])];
}

int main(int argc, char *argv[])
{
        string str = argv[1], str2;
        // Do this once, and you can call revcmp as many times as you want without repeating it.
        m['A'] = 'T'; m['T'] = 'A'; m['C'] = 'G'; m['G'] = 'C';

        revcomp(str, str2);

        cout << str << " = " << str2 << endl;

        return 0;
}


Last edited by Corona688; 10-28-2014 at 06:41 PM..
This User Gave Thanks to Corona688 For This Post:
# 9  
Old 10-28-2014
Thanks!
Agree with what you said: I seriously think you should try and program without STL vector, string, and map for a while.
One thing new and I never found in any book I've read is Using char as subscript for char array m[256], m['A']; m['T']...;
Two more questions:
1) Any specific reason to have m[256] instead of m[4], which should be enough for this case?
2) in revcomp() function, what's the reason using output = input;. I would normally allocate new memory for output. (---That's the difference between pro and novice!)
Thanks!
# 10  
Old 10-29-2014
Quote:
Originally Posted by yifangt
One thing new and I never found in any book I've read is Using char as subscript for char array m[256], m['A']; m['T']...;
What is a character but a smaller integer? Smilie Single-character constants count as integers.

Quote:
Two more questions:
1) Any specific reason to have m[256] instead of m[4], which should be enough for this case?
4 is not enough: printf("%d %d %d %d\n", 'A', 'T', 'C', 'G'); prints 65 84 67 71 You will note these correspond exactly to the ASCII character table.

The array is a look-up table, like a Caesar cipher, big enough to cover the entire ASCII range. This is very simple to do and lets you translate with a single instruction.

It's also one reason it should be a global variable. Global variables are guaranteed to have any contents you don't initialize, set to binary 0. Not that we're using any other contents, but if garbage gets in, it's reassuring to know garbage won't come back out.

Quote:
2) in revcomp() function, what's the reason using output = input;. I would normally allocate new memory for output. (---That's the difference between pro and novice!)
Thanks!
The same reason I used strdup -- to make a new string of correct length. I don't really care what's in it as long as it's long enough. I think this is more efficient than calling string.push_back() 570 times.

But strings have a lot of properties of vectors, so I could also have done this:
Code:
void revcomp(const string &input, string &output) {
        int n=input.length(), p=-1;
        output.resize(n);
        while(n > 0) output[++p]=m[toupper(input[--n])];
}

...which is probably better, especially if input happens to be large. No point copying it then just overwriting it.

Last edited by Corona688; 10-29-2014 at 12:48 PM..
This User Gave Thanks to Corona688 For This Post:
# 11  
Old 10-29-2014
Single-character constants count as integers. I realized that after I posted my question. So the subscripts are still integers but in CHAR format as 'A', 'C', 'G', 'T' corresponding to 65, 67, 71 & 84. Although the other elements of the array are empty (or, 0), the sacrifice is worth of it. Am I right?
... same reason I used strdup -- to make a new string of correct length. Got it now!
Thank you so much again!
# 12  
Old 10-29-2014
Quote:
Originally Posted by yifangt
Single-character constants count as integers. I realized that after I posted my question. So the subscripts are still integers but in CHAR format
They are integers in integer format. (I double-checked -- they are the same size as integers.) They're no more or less numbers than 39 or 0xc0 -- none of those are how the machine really thinks of these numbers, just convenient human-readable representations.

The compiled program actually has no distinction between characters and numbers. In scripting languages and such, it's sometimes difficult to get the ASCII value of a character. In C, the ASCII value is all you ever have. Or arrays full of ASCII values.

Quote:
Although the other elements of the array are empty (or, 0), the sacrifice is worth of it. Am I right?
That's my opinion, yes. Since a map is stored as a list, it has to search one-by-one, which amounts to doing this:

Code:
char trans(char c) {
        if(c == 'C') return('G');
        else if(c == 'c') return('G');
        else if(c == 'G') return('C');
        else if(c == 'g') return('C');
        else if(c == 'A') return('T');
        else if(c == 'a') return('T');
        else if(c == 'T') return('A');
        else if(c == 't') return('A');
}

...Which doesn't look that fast to me.

I could make the array even faster by removing the toupper(), and just storing m['c']='G', etc in the array too.

The first program I showed you in this thread used designated initializers:

Code:
char m[256]={ ['A']='T' };

Which is especially nice since it lets you set the values in advance without having to tell it all 256 of them. But that turned out to not be C++ compatible so I dropped it.

Last edited by Corona688; 10-29-2014 at 01:46 PM..
This User Gave Thanks to Corona688 For This Post:
# 13  
Old 10-29-2014
I'd recommend filling the array with ' ' (space) or '?' characters instead of zero values. A zero value will cause your output string to be prematurely terminated if you get garbage input.
This User Gave Thanks to achenle For This Post:
# 14  
Old 10-29-2014
I'd recommend filling the array with ' ' (space) or '?' characters instead of zero values.
How to do it? Could you please post your code to fill up the rest 252 element of the array? Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Segment fault related to strlen.S

Hello, This function was copied into my code, which was compiled without error/warning, but when executed there is always Segmentation fault at the end after the output (which seems correct!): void get_hashes(unsigned int hash, unsigned char *in) { unsigned char *str = in; int pos =... (7 Replies)
Discussion started by: yifangt
7 Replies

2. Programming

Segment-fault handling for pthreads

Hi I have struggling a week to fix a program , in the begining i got SIGBUS , but after many attempts still the program gets SIGSEGV segment fault , In bellow i post the seg fault log + source codes. would really appreciate if experts help me to fix this segment fault error. any advice is... (2 Replies)
Discussion started by: pooyair
2 Replies

3. Programming

Segmentation fault in other systems C program

Hello everybody, I've been working on a program on my Linux box, after finished the code, i compile it with gcc -Wall option, so i can see what's wrong or unused. The Walll output shows nothing, so there are no loose ends on the program. I run the program on my system, and it works PERFECTLY.... (5 Replies)
Discussion started by: Zykl0n-B
5 Replies

4. Programming

why segment fault,

I always get segment fault, why? can sb help me and modify it, I have spend on much time on #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <string.h> #define MAX 10 pthread_t thread; void *thread1() { int *a; int i, n; ... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

5. Programming

Data segment or Text segment

Hi, Whether the following piece of code is placed in the read-only memory of code (text) segment or data segment? char *a = "Hello"; I am getting two different answers while searching in google :( that's why the confusion is (7 Replies)
Discussion started by: royalibrahim
7 Replies

6. Programming

Memory Fault (core dumped) in ttpy program

I´m writing this program in QNX , I`m kinda new to UNIX and programing in general, and when I try to run it it gives me the Memory Fault error. Can anyone help? #include <stdio.h> #include <fcntl.h> void main(void) {int a,ter; char buf; printf("a="); scanf("%d",a); ter=open... (6 Replies)
Discussion started by: GiganteAsesino
6 Replies

7. Programming

a strange segment fault about ltp-posix test

Hi all In the ltp-posix test,there is a case in open_posix_testsuite\conformance\interfaces\timer_gettime\speculative/6-1.c I run the above code,it will has a segment fault, if I modify it to below,it works well Anybody can tell me why? (1 Reply)
Discussion started by: yanglei_fage
1 Replies

8. Programming

Seg Fault Running AIX COBOL program

Hi some help read............ I'm getting a segmentation fault when I run an AIX COBOL/Db2 program. I initiate the program from the command line, but it hangs, and then when I press enter it generates a segmantation fault and produces a core dump. The box is running AIX software level ... (5 Replies)
Discussion started by: steve_f
5 Replies

9. Programming

Program received signal SIGSEGV, Segmentation fault.

Dear all, I used debugger from C++ and these are the message I got: Program received signal SIGSEGV, Segmentation fault. 0x00323fc0 in free () from /lib/tls/libc.so.6 (gdb) info s #0 0x00323fc0 in free () from /lib/tls/libc.so.6 #1 0x00794fa1 in operator delete () from... (5 Replies)
Discussion started by: napapanbkk
5 Replies

10. Programming

Segment Fault

When run it, segment fault. What is wrong? #include <stdio.h> #include <stdlib.h> const int max =20; //**************************************************** // Input Matrix //**************************************************** void inMatrixAA(int *AA, int row, int col)... (9 Replies)
Discussion started by: zhshqzyc
9 Replies
Login or Register to Ask a Question