C++ :: Why Asterisk Required On Smart Pointers For Assigning Value

May 19, 2013

When i try to compile this code it gives me a error during run-time saying "*program name* has stopped working"

Code:
#include <iostream>
#include <memory>
using std::cout;
using std::endl;
using std::unique_ptr;

[Code] .....

Why is this happening? Also why do you need the asterisk on smart pointers to assign them a value? Is it just because, or is there a reason.

View 5 Replies


ADVERTISEMENT

C++ :: Smart Pointers In A Vector

Aug 21, 2014

I am new to smart pointers and have a question.If you push a smart pointer onto a vector and then some where later pop it back off it will delete the memory right?

View 1 Replies View Related

C++ :: Arrays And Smart Pointers

Aug 3, 2014

I'm writing quite a large program that needs to work with very large arrays (up to 100 million integers). It is necessary that i can access every point of the array directly (without running through the array) and that I use as little memory as possible. I first wrote the program with pointers that point to allocated heap memory. It works fine but now I wanted to use smart pointers instead (so I'm sure to have no memory leaks). Here's a simple visualization of my problem:

#include <iostream>
#include <memory>
using namespace std;
unique_ptr<int[]> upArray;
int main() {
int nArrayLength = 10;

[Code] ....

There are 2 things that do not work how I would like the to:

1. It wants me to assign the heap memory in one step: unique_ptr<int[]> upArray(new int[nArrayLength]); But I'd like to have the unique_ptr in my Class_Declaration before I know the array length and allocate the memory later.
2. *(upArray + i) = i;
cout << *(upArray + i);

Those lines don't work! How else can I do it.

View 6 Replies View Related

C++ :: Using Smart Pointers To Sort And Relink Potentially Large Data Elements

Feb 20, 2014

I am trying to use smart pointers to sort and re-link potentially large data elements. I have defined a class in my code for smart pointers, as listed below:

template <typename T>
class sptr {
public:
sptr(T *_ptr=NULL) { ptr = _ptr; }

[Code] ....

The object I'm trying to sort is a singly-linked list containing class objects with personal records (i.e., names, phone numbers, etc.). The singly-linked list class has an iterator class within it, as listed below.

template <class T>
class slist {
private:
struct node {
node() { data=T(); next=NULL; }

[Code] .....

The following is my function within my list class for "sorting" using the smart pointers.

template <typename T>
void slist<T>::sort(){
vector< sptr<node> > Ap(N); // set up smart point array for list
//slist<T>::iterator iter = begin();
node *ptrtemp = head->next;

[Code] .....

I must have a bad smart pointer assignment somewhere because this code compiles, but when I run it, std::bad_alloc happens along with a core dump. Where am I leaking memory?

View 6 Replies View Related

C++ :: Assigning Physical Values To Pointers In VC++?

Sep 9, 2013

The problem may only apply to VC++ compiler - ??.

Cannot do this:

const unsigned int Port = 0x900E0000;
unsigned int *pData = Port; //Error - can't convert from int to int*.

Solution:

const unsigned int PORT = 0x900E0000;
const unsigned int PORT_PTR = ((unsigned int *)0x900E0000);
unsigned int *pData = PORT_PTR; //No Error.

I found no way to do it but the above.

Ex., you cannot do:

pData = (unsigned int *)0x900E0000;

View 1 Replies View Related

C/C++ :: Assigning Functions To Array Of Function Pointers

May 13, 2013

I'm trying to create an array of function pointers and then assign compartilbe functions to them, so I can just call *pf[0](xxx);

The functions are all of the type

void func01(unsigned char*, int, int)

how would I create an array of function pointers and assign the address of the functions to them? So I could call them like

ptrToFunction[i](charBuffer, 10, 20);

I've read a bit on line and I thought I could do it but so far I've failed.

It seems trivial and I feel I'm close but close isn't good enough.

I'd like to assign the fuction addresses like this:
for (int i=0; i<10; i++)
if (i==1)
ptrToFunction[i]=func01;
if (i==2)
ptrToFunction[i]=func02;
etc.

The actual logic is somewhat different than this but this close.

View 4 Replies View Related

C :: Sorting Arrays Of Pointers - Assigning Data To Gene

Feb 11, 2013

Code:
typedef struct {
name_t name;
float beta[beta_num];
} gene_t;

[Code] ....

Is gene an array of address ? How come the compare function doesn't work ?

View 1 Replies View Related

C++ :: How To Design A Smart Pointer Class

Nov 11, 2014

smart pointer class is the one that take in charge of release allocated resource when itself destroyed.

recently,i want design a smart pointer class, it take in charge of release resource allocated by new or new[].

my problem how to design destructor, you should determine the pointer ptr is pointer an array or a single object.

the structure of this smart point class may be:

template<class T>;
class smart_pointer{
public:
smart_pointer(T* p):ptr(p){};
~smart_pointer(){

[Code] ......

View 6 Replies View Related

C++ :: Pass By Reference To Smart Pointer?

Jul 30, 2014

I just want to know if there is any real difference between the two below, if yes, when would i use one over the other? I would thought the "&" is pointless in below function, as far as the data is concerned.., the only things is with "&", if the pointer address value is changed in Test function, it will affect the caller's copy of data. Both function should behave the same if data is changed.

Code:

Between

void Test(QSharedPointer<Data> data)
{
}

and

void Test(QSharedPointer<Data> & data)
{
}

View 6 Replies View Related

C++ :: A Compiler Error With The Implementation Of Smart Pointer

Oct 8, 2014

Here is my code,

Code:
class A {
public:
void display() {
cout<<"A"<<endl;

[Code] .....

The compiler error is "error C2039: 'display' : is not a member of 'SP<T>'". What am I missing here?

View 14 Replies View Related

C++ :: Smart Accessor Function For Multiple Data Containers?

Mar 14, 2012

Suppose I'm writing a program designed to simulate a large company. I'm interested in tracking each company employee by the location where they work. This company has perhaps a thousand different locations:

class Employee {
public:
AccessorFunction1(); // does something
AccessorFunction2(); // does something different
AccessorFunction3(); // does something completely different
protected:
// Some data

[code]....

Once employees are created and pointers to them are saved in the proper Location vector, I write an accessor function, OrganizeLocation(), designed to do a number of operations on a given vector. The problem is, I have maybe a thousand vectors. How do I call this function and specify which vector I want?

Currently, I'm using this clunky solution:

void Company::OrganizeLocation(int a){
switch(a) {
case 1: {
for(unsigned int i=0; i<LocationA.size(); i++) {
LocationA[i]->AccessorFunction1();
LocationA[i]->AccessorFunction2();
LocationA[i]->AccessorFunction3();

[code]....

The key point here is that whichever vector I choose to operate upon, I'll do the exact same procedure every time. I hate this solution because it results in very long and repetitive code not to mention its very error-prone when you re-editing all those "LocationA 's into "LocationB/C/D/etc."

void Company::OrganizeLocation( string $WhichOne$ ){
for(unsigned int i=0; i<LocationA.size(); i++) {
Location$WhichOne$[i]->AccessorFunction1();
Location$WhichOne$[i]->AccessorFunction2();
Location$WhichOne$[i]->AccessorFunction3();
}

Or, if it can't be done in C++, is there a better design approach? Ultimately I need the Company object to hold multiple vectors but use one compact accessor function to perform operations on just one of them.

View 5 Replies View Related

C :: What Is Left Most Asterisk Doing

Apr 1, 2014

I encounter this from a library:

( *(reg8 *)(pinPC) |= CY_PINS_PC_DATAOUT)

From my understand the cast (reg8 *) applies to the result of the bitwise OR. But what is the left most asterisk doing?Is it just dereferencing the casted pointer?

View 1 Replies View Related

C++ :: Asterisk Bar Graph

Feb 12, 2014

i have to make a programs that prompts the user to enter quiz grades and add them up. For examples the user enters 6 test grades they are out of 5 so he enters 0-5 and i store them in the array. This part works great but now i have to print out a bar of vertical asterisks for every part too. So if at the end we have one test grades that are 2 grades of 1 points, 1 grade of two point, 2 grades of three point and 1 grade of 5 point it will have to display them as this

There are 2 grades of 1
There are 1 grades of 2
There are 2 grades of 3
There are 1 grades of 5

i need to do for loops but i am stuck on what to count too and what to print i know i will need cout << "*" and a couple of spaces.

#include <iostream>
using namespace std;
int main (){
int size;
int tests;
int a[6]={0};

cout << "How many quiz scores will you enter: ";
cin >> size;

[code]....

View 1 Replies View Related

C++ :: Smart Array Class - Constructor Throws Wrong Exception

Jan 24, 2014

Writing a smart array class for my C++ class. I'm running into a problem where the constructor apparently throws the wrong exception. Compiled on G++ with C++11 extensions enabled.

Code:
// headers
#include <iostream>
#include <utility>
#include <cctype>
// stuff we need from namespace std
using std::cout;
using std::cin;

[Code] .....

When I try to check the error-handling code, when I enter a size less then two, Array's ctor throws InvalidSize, and gets caught, all good. But when I enter a letter, the ctor also seems to throw InvalidSize!

View 14 Replies View Related

C :: How To Change The Numbers Into Asterisk

Oct 10, 2014

i have my basic C program here (i'm new to C language):

Code:

#include <stdio.h>
int main()
{
int grade;
int A=0;
int B=0;
int C=0;
int D=0;
int E=0;

[Code]....

As you can see, it's only for counting how many students got the grades from A to E, but the problem is that i need to change it from numbers into asterisk

for example: 4 students got the grade A, and 3 students got the grade B

instead of displaying

A=4
B=3

i want it to display

A=****
B=***

View 11 Replies View Related

C++ :: How To Change Password Input To Asterisk

Mar 6, 2015

Code:
#include <iostream>
#include <string>
using namespace std;

int main()

[Code] ....

View 6 Replies View Related

C :: Nested Loops - Asterisk Diamond

Jul 11, 2013

Getting close but I think I am stuck on the second loop. The input you put in will be doubled (and it's not supposed to).

Code:
int main() {
int n, i, j, k;
printf("What would you like the height to be? (Positive odd integer)
");
scanf("%d", &n);

[Code] .....

View 3 Replies View Related

C++ :: Place Two Asterisk Triangles On Top Of Each Other But Only Using 3 For Statements

Oct 19, 2014

I have to place two asterisk triangles on top of each other BUT only using 3 for statements. I have gotten the first one:

for(int a=1;a<=10;a++) {
for(int b=1;b<=a;b++)
cout << "*";
cout << endl;
}

I need the output to look like this:
*
**
***
****
*****
******
*******
********
*********
**********
*
**
***
****
*****
******
*******
********
*********
**********

The only kicker is I can have a total of 3 nested for loop statements.

View 7 Replies View Related

C++ :: Asterisk Diamond Is Printing Out Wrong

Sep 6, 2014

I feel as if I'm so close and I'm just messing up a value somewhere, but I am at a loss as to what I should change. Here is my code:

#include "stdafx.h"
#include <iomanip>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) {

[Code] ....

View 2 Replies View Related

C++ :: How To Produce Password Field With Asterisk

Sep 2, 2014

void Log_In() {
system("cls");
gotoxy(30, 30);
time_t now;
time(&now);

[Code] .....

How to produce a password field with asterisk ****** .....

View 1 Replies View Related

C/C++ :: Printing Asterisk Along A Function Of Y Columns

Jun 18, 2014

The assignment is to plot the functions, by implementing a function having the following prototype:

void plotPoint(double y);

This function should print a single "*" symbol, in a position determined by the value of y, and then a newline. The position of the * symbol can span over 80 columns: each column should represent a delta of length 0.1 in the value of y, and the zero should be placed on the 40-th column.

For example:
• placePoint(0) should print the * symbol on the 40th column
• placePoint(0.1) should print the * symbol on the 41st column
• placePoint(1) should print the * symbol on the 50th column
• placePoint(-1) should print the * symbol on the 30th column

Here is what I have so far:

#include <iostream>
#include <cmath>
int a;
int b;
int x;
int y;
int Func1(double a, double b )/>

[Code] ....

I'm lost now as to where to go to plot. I know that depending on the option chosen I call the corresponding function to return a value for y which is just then plugged into a function to plot it on columns of y.

View 6 Replies View Related

C/C++ :: Stopping Case For Asterisk Pattern?

Dec 1, 2014

So, I'm going to write a recursive function for a asterisk pattern but I'm stuck figuring out how to create a stopping case for it, better yet, I'm unable to describe the pattern completely.

*
**
-*
****
--*
--**
---*
********
-----*
-----**
------*
-----****
-------*
-------**
--------*

( - represent white spaces )

What I've been thinking:

* Every odd row has 1 * with 1 incremented white space

* Every "pair" of asterisks equals 8 total (EX. 8 one pair *'s, 4 two pair *'s, 2 four pair *'s)

Unfortunately, that's all I got. how I can represent this as I function. Once I figure out what my stopping case should be, I think I can do the coding on my own.

View 1 Replies View Related

C :: Not Getting Required Result

Apr 30, 2013

Code:
void search(){void output(void);
char title[20],;
char *p;
clrscr();

[Code] ......

Info:Program that stores information about reports .the above function searches the report according to its title. list is the name of the structure that stores the records.

Why i'm using strstr:

for eg. there is a report titled 'report on tigers'

I want the report information to be output if someone searches for 'tiger'

Output:displays all the entries i have made till now

file is attached.

View 4 Replies View Related

C++ :: Utilizing Arrays Create Vertical Asterisk Graph?

Jun 25, 2013

This program needs to display a vertical graph of asterisks showing production for each plant in the company ...

Here is what I have so far:

***********************************
This program will read data and
display a vertical graph showing
productivity for each plant.
Reference Display 7.8 pg. 401-3
***********************************
*/
#include <iostream>

[Code].....

View 1 Replies View Related

C/C++ :: Encrypting Password To Be Inputed And Printing It As Asterisk Format

Sep 14, 2014

#include<stdio.h>
#include<conio.h>
#include<string.h>
char str1[20], str2[20]="kent";
main() {
printf("Enter your Username: ");
scanf("%s",str1);

[Code] ...

View 1 Replies View Related

C++ :: Why Using Namespace Required In Linux But Not In Turbo

Jan 8, 2015

Why is using namespace needed in linux but not in turbo c++?

View 1 Replies View Related







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