Sponsored Content
Top Forums Programming Segment fault for C++ program when return vector Post 302922824 by Corona688 on Tuesday 28th of October 2014 01:24:24 PM
Old 10-28-2014
This is the error:

Code:
for (unsigned i = str.size()-1; i >= 0 ; --i)

An unsigned number is never going to be less than zero, making this an infinite loop. Some compilers will warn you about this.

That said, this code looks suspect in a number of ways. Why are you using strings as the input and vector as the output -- why not vector or string for both? Why return giant vectors since it means it'll be copying them again and again and again and again and again? Pass one in as a reference.

Above all, why use boost? You will be chained to that depleted uranium lawn gnome forever. There's plenty of ways to initialize a map.

Code:
const char *i[]={"AG", "TC"};
for(int n=0; i[0][n]; n++)  { m[i[0][n]]=m[i[1][n]]; m[i[1][n]]=m[i[0][n]]; }

Or don't even use a map at all:

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

static const unsigned char map[256]={
        ['A']='T',      ['T']='A',      ['G']='C',      ['C']='G'       };

int main(int argc, char *argv[])
{
        int n;
        const char *input="ATCGTTTCCC";
        char *output=strdup(input);

        for(n=0; input[n]; n++) output[n]=map[input[n]];

        printf("%s = %s\n", input, output);
}

Vectors and maps have made this problem more difficult, not less.

Last edited by Corona688; 10-28-2014 at 02:49 PM..
This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
XkbAllocCompatMap(3)						   XKB FUNCTIONS					      XkbAllocCompatMap(3)

NAME
XkbAllocCompatMap - Allocate a new compatibility map if you do not already have one available SYNOPSIS
Status XkbAllocCompatMap ( xkb, which, num_si ) XkbDescPtr xkb; unsigned int which; unsigned int num_si; ARGUMENTS
- xkb keyboard description in which to allocate compat map - which mask of compatibility map components to allocate - num_si number of symbol interpretations to allocate DESCRIPTION
xkb specifies the keyboard description for which compatibility maps are to be allocated. The compatibility map is the compat field in this structure. which specifies the compatibility map components to be allocated (see XkbGetCompatMap). which is an inclusive OR of the bits shown in Ta- ble 1. Table 1 Compatibility Map Component Masks --------------------------------------------------------------- Mask Value Affecting --------------------------------------------------------------- XkbSymInterpMask (1<<0) Symbol interpretations XkbGroupCompatMask (1<<1) Group maps XkbAllCompatMask (0x3) All compatibility map components num_si specifies the total number of entries to allocate in the symbol interpretation vector (xkb.compat.sym_interpret). XkbAllocCompatMap returns Success if successful, BadMatch if xkb is NULL, or BadAlloc if errors are encountered when attempting to allocate storage. STRUCTURES
typedef struct { KeySym sym; /* keysym of interest or NULL */ unsigned char flags; /* XkbSI_AutoRepeat, XkbSI_LockingKey */ unsigned char match; /* specifies how mods is interpreted */ unsigned char mods; /* modifier bits, correspond to eight real modifiers */ unsigned char virtual_mod; /* 1 modifier to add to key virtual mod map */ XkbAnyAction act; /* action to bind to symbol position on key */ } XkbSymInterpretRec,*XkbSymInterpretPtr; DIAGNOSTICS
BadAlloc Unable to allocate storage BadMatch A compatible version of Xkb was not available in the server or an argument has correct type and range, but is otherwise invalid SEE ALSO
XkbGetCompatMap(3) NOTES
Note that symbol interpretations in a compatibility map (the sym_interpret vector of XkbSymInterpretRec structures) are also allocated using this same function. To ensure that there is sufficient space in the symbol interpretation vector for entries to be added, use XkbAl- locCompatMap specifying which as XkbSymInterpretMask and the number of free symbol interpretations needed in num_si. X Version 11 libX11 1.2.1 XkbAllocCompatMap(3)
All times are GMT -4. The time now is 02:03 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy