C++ :: Unexpected SIGSEG Abort When Working With Vector

Oct 9, 2013

I keep getting a segmentation error when ever I have the following code...

int main(void) {
//Section 1
unsigned long val = 12;
std::vector<unsigned long> vval;
for(unsigned long i = 0; i < 100; ++i) {
vval.push_back((unsigned long)0);

[Code] ....

Error: *** Error in `/home/alex/projects/bignum/build/bignum': free(): invalid pointer: 0x00007ffff75b5b88 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x80a46)[0x7ffff7274a46]

compiler is clang++ 3.2

It doesn't happen if I restructure it so that bignum::num is not a pointer to an std::vector<unsigned long>

View 10 Replies


ADVERTISEMENT

C++ :: Vector With Own Class Not Working?

Aug 19, 2013

I have a settings class and a settingItem class. The settings class has a vector of settingItems. The vector is not working:

error C2065: 'settingItem' : undeclared identifier
error C2923: 'std::vector' : 'settingItem' is not a valid template type argument for parameter '_Ty'

with code line:

vector<settingItem> settings;

View 8 Replies View Related

C++ :: Accessing And Working With Vector From Multiple Threads

Oct 20, 2014

I have a vector that I would like to access and work with from multiple threads. I have created an example below to illustrate the functionality that I would like to accomplish.

The goals are to be (1) high speed, (2) thread safe, and (3) *if possible* continue to use vectors as my larger project uses vectors all over the place and as such I would like to maintain that.

However, if I need to switch from vectors to something else then I am open to that as well.

The following example below compiles but crashes rather quickly because it is not thread safe.

How I can fix the example below which I can then apply to my larger project?

#include <string>
#include <vector>
#include <ctime>
#include <thread>
#include <iostream>
#include <random>
#include <atomic>
#include <algorithm>
enum EmployeeType {

[Code] ....

View 1 Replies View Related

C++ :: Unexpected Output In Example Function

Jan 21, 2015

I am currently trying to understand why this example for using an array as an argument in a function has a different result than what the lecture notes say it should be.

So supposedly sum should return with the value 28, but I get 27 no matter what. I also am not very good at reading and understanding what exactly the order of operations for this function are.

Code:
#include <iostream>
using namespace std;

int sum(const int array[], const int length) {
long sum = 0;
for (int i = 0; i < length; sum += array[++i]);
return sum;

[Code] ....

View 3 Replies View Related

C :: Unexpected Output With Char

Sep 14, 2013

I tried running the code below and i got an unexpected output

Code:

#include<stdio.h>
void main() {
char a='A';
while(a)

[Code] ....

The code is supossed to give an infinite loop but instead it terminates with a=0...I tried running it with some casting like this

Code:

#include<stdio.h>
void main(){
char a='A';
while((int)a)

[Code] ....

But the output was the same as before.why the code has this unexpected behaviour???

View 7 Replies View Related

C++ :: Unexpected SIGABRT At Return 0

Jun 24, 2013

I am working through Binary IO with objects and am running into a curious error.

The program executes perfectly until it hits the very end. At "return 0", the program fails. The debugger picks up SIGABRT when executing "return 0". This seems to me to indicate a deconstructor problem of some kind. However, I can't seem to find any deconstructor problems (I am fairly new at programming though).

If I comment out the following two lines:

"binaryio.read(reinterpret_cast<char *>(&studentNew1), sizeof(Student));"
"binaryio.read(reinterpret_cast<char *>(&studentNew2), sizeof(Student));"

then the program finishes exiting without error . . . . but the whole point is to be able to read from the binary file. With those two lines in the code, the program successfully reads from the file and outputs the objects to the console ,but fails at "return 0";

Here is the code:

#include <iostream>
#include <fstream>
#include "Student.h"
using namespace std;

void displayStudent(Student student) {
cout << student.getFirstName() << " ";

[Code] .....

View 5 Replies View Related

C++ :: Unexpected Output From Fprintf?

Feb 11, 2014

I have the following piece of code which should write the contents of my vetor to y file but I am getting a very weird output in my file.

for (int i=0;i<amount;i++) {
fprintf(pFileO,"Case #%d: ",i+1);
for (int j=0;j<words[i].size();j++) {

[Code].....

As you can see this doesn't make sene because the file should also contain the exact same things as the cmd outputs. What's going on here ?

View 1 Replies View Related

C++ :: Getting Unexpected Outputs For Bitfields And Floats

Aug 7, 2013

I'm getting unexpected output in 2 different cases. The 1st deals with bitfields. The C++ standard has this line about integral promotions:

An rvalue for an integral bit-field (9.6) can be converted to an rvalue of type int if int can represent all the values of the
bit-field; otherwise, it can be converted to unsigned int if unsigned int can represent all the values of the bit-field.

If the bit-field is larger yet, no integral promotion applies to it. If the bit-field has an enumerated type, it is treated as any other value of that type for promotion purposes.

This sounds like the value of a bitfield will always be treated as a signed int if the signed representation of the value will fit in the bits. This seems to hold true for my C compiler, but not my C++ compiler.

I tried storing a small negative value in a bitfield that has enough bits to store the sign bit and the value. But when I print out the bitfield, I always get a large number

In the example code below, I expect the output:

Code:
foo.x = -1
foo.y = -2
foo2.x = 31
foo2.y = 6
foo3.x = -1
foo4.x = 4294967295 But I get: Code: foo.x = 31
foo.y = 6
foo2.x = 31
foo2.y = 6
foo3.x = -1
foo4.x = -1 -------------------

The other issue I'm having is sort of similar. I'm trying to store 4294967295 into a float, but when I print it out, I get 4294967296. i've tried storing a few other large values like this and what's printed out is rarely the value I stored. I thought it might be because of some int to float conversion, so I tried 4294967295.0. Still no luck. Then I remember that defaults to a double so maybe that's the issue so I tried 4294967295.0f. Still no luck. Why can't I store the correct value here? I don't think it's an IEE format thing since I can use these values as floats on a calculator program.

The example code showing both issues is below.

Code:
#include <stdio.h>
typedef struct {
signed char x : 5;
signed char y : 3;
}my_struct_t;

[Code] .....

View 11 Replies View Related

C++ :: Unexpected Expression Compiler Error

Jul 30, 2013

My compiler (GCC) keeps expecting an expression where it shouldn't in 1 specific piece of my code:

int zxcNewWindow( HWND parent, TCHAR *text, zxWINDOW *kid,
UINT style, int x, int y, int w, int h, int type )
// right here
{
*kid = zxDefWINDOW;

The project contains only 2 files right now and the settings are just the default for an empty Code::Blocks 12.11 project. Both files are in UTF-8 format (tried in ASCII too), I just cannot see why this is not compiling correctly. I'll post the files in the next two posts.

Edit: For those of you who didn't get what the error was from the above here's the full log:

mingw32-gcc.exe -Wall -g -DzxDEBUG -c C:MePrjscppzxGUImain.c -o objmain.o
C:MePrjscppzxGUImain.c: In function 'zxcNewWindow':
C:MePrjscppzxGUImain.c:39:10: error: expected expression before '{' token
Process terminated with status 1 (0 minutes, 0 seconds)
1 errors, 0 warnings (0 minutes, 0 seconds)

View 8 Replies View Related

C++ :: Reverse Array - Unexpected Result

Jun 20, 2013

i use dev c++...i write this code to reverse an array and save the result in the same one

if n=3 i expect
a[0]=0 a[1]=1 a[2]=2 (before rev is OK but after calling rev)
a[0]=2 a[1]=1 a[2]=0 (expected result )
but i get
n=3

[code].....

View 4 Replies View Related

C++ :: Preventing The User From Feeding (Cin) Unexpected Values

May 2, 2013

I'm just starting c++, doing tutorials. So many tutorials. I've noticed that the tutorials all assume the same thing: That the user will always do exactly as he/she is told when asked for input.

Example: "Please enter your age:"

Now, the example code might be expecting the user to type some numbers, but what if the user feels like typing out the letters of their age?

"I am ninety five thank you very much, sonny"

I could specify to the user that I only want the information in number form, but what if the user is just being a dick?

What if the user types, "none o' yer business."?

So... how to approach "fool-proofing" player input?

View 2 Replies View Related

C++ :: Regex Unexpected Matching (Floating Point)

Oct 28, 2014

I have written this regex to match a floating point literal:

(^[[:space:]]*)(([0-9]+.?[0-9]*([eE][+-]?[0-9]+)?)|"
"(.[0-9]+([eE][+-]?[0-9]+)?))([fFdD]?[[:space:]]*)$

and when I match it with string like "123e" or "e2" it works while it shouldn't and I can't find the reason why.

View 2 Replies View Related

C/C++ :: Unexpected Behavior For Classes That Contain Member Object

Apr 1, 2014

I have a WordRecord that contains a LinkedList (both my doing). I have rigorously tested my LinkedList class, and know that it works (heck, I used it in the last project I had!). The problem is that undefined behaviour seems to happen when using the WordRecord, which has a std::string and a LinkedList<unsigned>. (The problem happens with the LinkedList.)

Here is the code:

main.cpp
#include <iostream>
#include "BinaryTreeNode.h" // here for test purposes
#include "LexicographicTree.h"
#include "LinkedList.h"//also for test purposes
#include "OutputStream.h"
#include "WordRecord.h"
using namespace std;
int main()
{
// setup the OutputStream to print to "test.txt"
OutputStream stream("test.txt");
// create a sampleWordRecord (make it have the word "I" on line 1)

[code]....

One of the requirements for the project is that it must compile on Unix server (I am using Windows, and have tested it in both environments.) I get a core-dump in the Unix environment. On the other hand, the output on-screen in the Windows environment looks right. However, when I open up the text file, I get the following

Sample word record:

WordLines
I{14}
/* I have no what is happening to sampleWordRecord's LinkedList; I am not trying to modify it, except for where I created the sampleWordRecord! */

View 14 Replies View Related

C++ :: Upon Checking Emptiness Of Array - Getting Unexpected Result

May 9, 2015

node = pool.at(0) is executed even though pool is empty,

I even tried pool.size() == 0, that line is still executed. What is the reason, I wonder?

Code:
NodePoolNode* NodePool::acquireNode(int x, int y, long t) {
std::stringstream key;
key << x << ":" << y << ":" << t;
NodePoolNode* node = usedNodes[key.str().c_str()];
if (node == NULL) {

[Code] ....

View 3 Replies View Related

C++ :: Function To Build Multiplication Table Of Arbitrary Dimensions - Unexpected Output

Nov 8, 2013

So the latest challenge from jumping into c++ is as following.

Code:
Write a function that builds the multiplication table of arbitrary dimensions This chapter also talks a ton about 2d arrays.

So I've built my program thus far as this.

Code:
#include <iostream>
using namespace std;
int drawTable(int,int);
int main()

[Code] .....

So basically, the idea is that I can use the arrays dimensions as a placeholder, and just multiple them to get that specific spot, so table[0][0] = 0, [0][1] = 0 and so on. However the output actually seems to be randomly generated numbers so I'd like to ask, what am I doing wrong? Am I on the right track? Or have I missed the bus stop completely.

View 12 Replies View Related

C++ :: Unexpected Result Decrementing End Of Range Iterator In Call To Transform Algorithm

Aug 23, 2013

I found this piece code on the following site:

[URL]

I predicted the outcome as being 01230 as I thought the prefix decrement operator on iterator ce would prevent the final element of the list from being transformed.

I was wrong, the correct output is 01234.

So, I removed the decrement prefix and ran the test again, expecting a different result. It wasn't! The result was still 01234.

Only when I decremented ce twice did I get the result I initially expected, 01230.

why the first decrement of ce appears to have no effect?

#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <cstdio>
int main() {
typedef std::list<int> L;

[code]....

View 2 Replies View Related

C/C++ :: Sequential Matrix Multiplication Program - Syntax Error Near Unexpected Token

Apr 30, 2014

I wrote a sequential matrix multiplication program in c.

After execution i get error like
./mul.c: line 11: syntax error near unexpected token `('
./mul.c: line 11: `int main(){'

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define wA (30 * 16)
#define wB (50 * 16)
#define hA (80 * 16)
#define hB wA
#define wC wB

[Code] ....

View 4 Replies View Related

Visual C++ :: Throttle Binary File Download Via WinHTTP Gives Unexpected Results

Nov 11, 2014

My Windows service may download a binary update in the background using WinHTTP APIs. I'm using code similar to the one from the bottom of this MSDN page. For ease of access I'll copy if below:

Code:
DWORD dwSize = 0;
DWORD dwDownloaded = 0;
LPSTR pszOutBuffer;
BOOL bResults = FALSE;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;

[Code] ....

The issue I'm experiencing is that while downloading, the code above "hogs" most of the bandwidth available on the client computer. My goal is to make it "throttle" itself on a configurable "bps" level, say at 50 kbps and not use any more bandwidth.

Someone suggested measuring my current download speed (in bps) and depending on the value make the thread "sleep" before calling WinHttpReadData again.

So the formula I'd use to calculate BPS is this:

BPS = (BytesDownloaded * 1000 * 8) / TimePassedInMilliseconds;

Then to convert to Mbps:

Mbps = BPS / (1024 * 1024);

And the waiting/sleep would be done as such:

Code:
DWORD DesiredBitsPerSec = 50 * 1000;//for 50 kbps
if(fBps > DesiredBitsPerSec) {
DWORD dwW = (DWORD)((fBps - DesiredBitsPerSec) * 1000) / DesiredBitsPerSec;
if(dwW > 0)
::Sleep(dwW);//Sleep to throttle the download speed to the one we need
}

So I implemented it into the code above but it didn't work -- the Sleep API would produce very strange delays. So I decided to add a little bit more tracking code to actually be able to see what BPS values I'm getting.

I created a small test project that is attached to this post to illustrate the issue. (It's hard to explain without running an example.)

For a test I began downloading a large binary file (Windows 10 MSI, off Microsoft's server) and at the same time had the Task Manager open on a Performance tab that was showing me the current network throughput. I made a screenshot:

But as you can see, the reading on the test program and the actual network BPS values differ greatly.

View 1 Replies View Related

C++ :: Using Vector Push Back Function To Output Contents Of Vector (similar To Array)

Feb 9, 2015

How to output vector contents using the push_back function. My program reads in values just fine, but it does not output anything and I've been stuck on why.

here is my code:

#include <iostream>
#include <array>
#include <vector>
using namespace std;
int duplicate( vector < int > &vector1, const int value, const int counter)

[Code].....

View 3 Replies View Related

C++ :: Open File And Read In Rows To String Vector And Return Vector

Jun 7, 2012

I have a cpp app that reads in a number of files and writes revised output. The app doesn't seem to be able to open a file with a ' in the file name, such as,

N,N'-dimethylethylenediamine.mol

This is the function that opens the file :

Code:
// opens mol file, reads in rows to string vector and returns vector
vector<string> get_mol_file(string& filePath) {
vector<string> mol_file;
string new_mol_line;
// create an input stream and open the mol file
ifstream read_mol_input;
read_mol_input.open( filePath.c_str() );

[Code] ....

The path to the file is passed as a cpp string and the c version is used to open the file. Do I need to handle this as a special case? It is possible that there could be " as well, parenthesis, etc.

View 9 Replies View Related

C++ :: Create Class Vector As Template And Define Operations On Vector?

May 13, 2013

I need to create a class vector as a template and define operations on vectors.

And this is what I made.

#include<iostream>
using namespace std;
template<class T>

[Code].....

View 2 Replies View Related

C++ :: Recursive Function 2 - Create Vector Of Vector Of Integers

Mar 26, 2013

Lets say that I have a vector of vector of integers. <1,2,3,4> , <5,6,7,8>, <10,11,12,13>

How do I make a function that creates vector of vector of every different integers?

<1,5,10> , <1,5,11>, <1,5,12>, <1,5,13>
<1,6,10> , <1,6,11>, <1,6,12>, <1,6,13>
<1,7,10> , <1,7,11>, <1,7,12>, <1,7,13>
<1,8,10>, <1,8,11>, <1,8,12>, <1,8, 13>
<2,5,10>, <2,5,11>, <2,5,12>, <2,5,13>

and so on...

View 2 Replies View Related

C++ :: Find Function To A Vector Of Structures Vector

Jul 5, 2013

I have asked a related question before, and it was resolved successfully. In the past, when I wanted to use std::max_element in order to find the maximum element (or even sort by using std::sort) of a vector of structures according to one of the members of the structure, all I had to do was to insert a specially designed comparison function as the third argument of the function std::max::element. But the latter comparison function naturally accepts two arguments internally.

For instance, here is a test program that successfully finds the maximum according to just one member of the structure:

Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

[Code] ....

And the output was this, as expected:
Maximum element S.a of vector<S> vec is at: 9
[I]max element of vec.a between slot 3 and slot 6 is: 6, and its index is: 6 vec[6].a = 6
[I]max element of vec.a between slot 4 and slot 7 is: 7, and its index is: 7 vec[7].a = 7
[I]max element of vec.a between slot 5 and slot 8 is: 8, and its index is: 8 vec[8].a = 8
[I]max element of vec.a between slot 6 and slot 9 is: 9, and its index is: 9 vec[9].a = 9

However, I now need to search and find an element of vector<myStruct> according to just one member of myStruct, instead of finding the maximum or sorting as before. This presents a problem because the function std::find does not accept such a comparison function as its third argument.

This was the description of the std::find function that I found: find - C++ Reference

Code:
template <class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val);

I could also find another function called std::find_if, but this only accepts a unary predicate like this: find_if - C++ Reference

Code:
template <class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);

And once again this is either inadequate of I don't see how to use it directly, because for the third argument I would like to insert a function that takes two arguments with a syntax like this:

Code:
int x=7;
std::vector<S>::iterator result;
result = std::find(vec.begin(), vec.end(), []( const (int x, const S & S_1) { return ( x == S_1.a ) ; } ) ;

Is there another std function that I can use to make search and find an element according to just one member of myStruct?

Or perhaps there is a clever way to pass two arguments to the unary predicate.

View 4 Replies View Related

C/C++ :: Cannot Print Vector Of Vector Of Doubles

Mar 31, 2014

I am trying to print a matrix solution that I've stored in a vector of doubles. The original matrix was stored in a vector of vector of doubles. I want to print to the screen and an output file. Right now it just prints to screen column-style and the output file is horizontal-style. I tried to change the solution from vector of doubles to a vector of vector of doubles like the original matrix but when I run my code it crashes. Here is what I am referring to:

void readMatrix(vector<vector<double>> &matrix, vector<double> &b, int &N, string input) {
ifstream in(input);
in >> N;
matrix.resize(N);
b.resize(N);

[Code] ....

However when I change my printResult function to this (I removed the string argument because I just want to get it working to the screen first):

void printResult(vector<vector<double>> &sol, const int N) {
//ofstream out(output);
//int j;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++){

[Code] ....

The program crashes. Am I even printing the matrix correctly?

View 4 Replies View Related

C/C++ :: Swapping Vector Int And Vector Strings

Mar 30, 2013

I want to have it so that when i ask for the person witch item they want to drop on the ground it goes into another vector that i can pick back up the item if they want it back and erase when they walk away.

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cmath>  
using namespace std;  
    struct InventoryItem

[Code] ....

View 4 Replies View Related

C++ :: Can't Read In Matrix Into Vector Of Vector Of Int

Mar 24, 2012

Code:
#include <iostream>
#include <istream>
#include <fstream>
#include <vector>

[Code]....

Why is it reading in nothing for the arrays, and also making the size of the total thing the total number of numbers? It should have a size of 2, not 5.

View 14 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved