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


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++ :: 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 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++ :: Dynamic Creation Of Arrays Of Pointers To Arrays Of Pointers

Apr 15, 2013

I'm trying to write a function that takes a 32bit address and a data to store at this address.

I'm wanting to take the 32 bit memory address eg 0x12345678 and split it
into 4 x 2 bytes
12, 34, 56, 78

then each of the 4 entries is at most a 256 entry array.eg
FF, FF, FF, FF

So in this example, 0x12 points to 0x34 in the second array, which points to 0x56 in the third array, which finally points to 0x78 in the last array. This last array holds the actual data.

After successfully doing 0x12345678, say I might get a read for 0x1234AABB. So, the first and second pointers already exist, but I then have to create and write to dynamically created arrays.

The arrays need to have all entries set to NULL so that i know whether to follow the pointers to overwrite a previously entered value or create new arrays and pointers.

It all looks good and simple in the pseudo code I've written up but I'm having trouble coding it. I'm currently trying to deal with the first entry case, ie all array elements are NULL, but I'm getting confused with the pointers and creation of new arrays.

void cpu::store(unsigned int mem_add,unsigned int mem_val) {
int first = (mem_address&4278190080)>>24;
int second = (mem_address&16711680)>>16;
int third = (mem_address&65280)>>8;
int fourth= (mem_address&255);

[Code] .....

A1 has been declared as
int* A1[256] ;

View 3 Replies View Related

C++ :: Use Different Pointers For Arrays?

Dec 24, 2013

So when if you want to change the values themselves for example in an array (just for example) this is valid;

void test(int* test)
{
test[0] += 10;
}
int bob[] {1, 3, 5};

If you did that bob[0] would not equal 11. All well and good right?

Now if you do this?

int sally = 33;
test(sallay);

This wouldn't work at all you actually have to use

void test(int& test)
{
test += 10;
}

how the memory addresses etc. are working here? I don't understand why you need to use & the reference operator if it's not an array? Why wouldn't that still work?

View 3 Replies View Related

C :: How To Use Character Pointers Instead Of Arrays

Apr 14, 2013

Here is my program to look up words. How do I use character pointers instead of using arrays?

Code:
#include <stdio.h>
struct entry
{

[Code].....

View 2 Replies View Related

C :: Dynamic Arrays And Pointers

Feb 11, 2013

I am having troubles with dynamic arrays and pointers. All the errors are of that kind. I think when I am assigning malloc to an array we assign to its address.

Code:
/*Create an array of genes of the large matrix*/
gene_t gene,gene1,gene2;
gene=malloc(INITIAL*sizeof(gene2)); Code:
while(...) {
if (gene_num==current_size){

[Code] .....

View 9 Replies View Related

C :: Arrays And Pointers In A Loop

Jun 27, 2013

Set an Array (a[10]) and a Pointer to that array (*pa) and code a loop (for( ; ; loop) that will advance that pointer (*pa) and will set a new content into it with each loop. that means that in the end of the day, my program will automatically set content to each cell of the array by promoting the pointer by 1 and add the sum to that pointer.

When I run the program it prints the address of the cells instead the value of it.

Code:
#include <stdio.h>
void main() {
float a[11], *pa; // Array and ptr set.
int i; // counter for the loop.
pa = a;
for (i=0 ; i<=10 ; i++) // the loop itself.

[Code] .....

View 3 Replies View Related

C :: Merging Arrays Using Pointers

Jul 17, 2013

Merging two arrays together using pointers!

View 4 Replies View Related

C :: Accessing Arrays Using Pointers

Aug 30, 2013

I came across the below code snippet in a project and was not sure how value of variable "response" is computed. Here as we can see, pic_data holds two one dimensional arrays but "response" access both the single dimensional array as two dimensional array.

Code:

#define MAX 100
#define MAXBUF 100
u32 response;
u32 index;
}

[code]....

View 3 Replies View Related

C :: Pointers And Bi-dimensional Arrays

Feb 6, 2013

I have a problem with a pointer and a bidimensional array. Here's the code:

typedef char t_board[5][4];
int i,j;
t_board *copy (t_board b) {
t_board *ptr;

ptr = (t_board *) malloc (sizeof(t_board));

[Code] ...

After this, I print new_board, and its ok; but the following code after the call to copy, crashes. Don't know why. if I remove the statement new_board = copy(some_board); , the code that follows runs ok.

View 4 Replies View Related

C/C++ :: How To Use Pointers To Arrays In Function

Feb 25, 2015

I am trying to use pointers to arrays in my function.

I can get the pointers to work outside of a function but I just can't figure out how to make them work in my function.jwhittle58, on 25 February 2015 - 06:06 PM, said:

I am trying to use pointers to arrays in my function. I can get the pointers to work outside of a function but I just can't figure out how to make them work in my function.

View 8 Replies View Related

C :: Passing Arrays To Other Data Has Pointers?

May 7, 2013

I am looking for an example of when passing arrays to other data that has pointers,

I understand the terms I would just like to see a small example of code that actually demonstrates this process.

View 4 Replies View Related

C :: Functions Which Change Arrays Without Pointers?

Jan 17, 2014

the book I learn from gave a task to write a program which gets a matrix , and we have to write a function that switches 2 columns or rows the user inputs .as far as I know a function can not change variables in the main function without using pointers .so , theoretically, can a function described here can be written without using pointers ? as far as I tried - it can not.

View 9 Replies View Related

C++ :: Class With Pointers And Dynamic Arrays

Apr 25, 2014

Class with Pointers and Dynamic Arrays

View 2 Replies View Related

C++ :: Project Combining Arrays / Pointers And Structures

May 6, 2014

Write a program that uses a record structure to store a Student Name, Student ID, Test Scores, Average Test Score, and Grade. The program should keep a list of test scores for a group of 6 students. The program should ask for the name, ID, and four test scores for each student. Then the average test score should be calculated and stored. The course grade should be based on the following scale:

Average Test Score Course Grade
------------------ ------------
90 - 100 A
80 - 89 B
70 - 79 C
60 - 69 D
59 or below F

A table should be displayed on the screen listing each student's name, ID, average test score, and course grade. Implement with functions.

My code runs but it isnt returning anything (readable/correct) and i have no clue why. This is what i have so far:

#include <iostream>
using namespace std;
const int columns = 4;
struct StuRec //user defined datatype {
int id[6];
char names[6][20];

[Code] .....

it compiles completely but at the end instead of showing the student name ID average score and class grade it shows.... [URL] .....

View 2 Replies View Related

C/C++ :: Project Combining Arrays / Pointers And Structures

May 5, 2014

Write a program that uses a record structure to store a Student Name, Student ID, Test Scores, Average Test Score, and Grade. The program should keep a list of test scores for a group of 6 students. The program should ask for the name, ID, and four test scores for each student. Then the average test score should be calculated and stored. The course grade should be based on the following scale:

Average Test Score Course Grade
------------------ ------------
90 - 100 A
80 - 89 B
70 - 79 C
60 - 69 D
59 or below F

A table should be displayed on the screen listing each student's name, ID, average test score, and course grade. Implement with functions.

My code runs but it isnt returning anything(readable/correct) and i have no clue why. This is what i have so far:

#include <iostream>
using namespace std;
const int columns = 4;
struct StuRec //user defined datatype {
int id[6];
char names[6][20];
int scores[6][4];
double avg[6];
char grade[6];
}; //semi-colon required

[Code] ...

View 8 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 Use Dynamic Memory Allocation And Pointers To Iterate Through The Arrays

Dec 21, 2014

I need to use dynamic memory allocation and use pointers to iterate through the arrays that I have already in this program. I am lost, nothing I do works and where to use the pointers. I am just looking for a push in the right direction so I can finish this project and how I can implement pointers in my program.

#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <stdio.h>
using namespace std;

[Code]...

View 1 Replies View Related

C/C++ :: Why Do Void Function Pointers EXECUTE Arrays Of Hexcode

Dec 23, 2012

I know how to use functions pointers in C and C++ and I know if you have something like

char buf[] = {
0x48, 0xb8, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x48, 0xbf, 0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0f, 0x05
};
((void (*) (void))buf)();

That this will execute those binary instructions in hexadecimal notation BUT WHY? I don't get why that works since that's an array of data not a function?

View 7 Replies View Related

C++ :: Pointers And Arrays - Program To Display Sorted List And Average Of Scores With Appropriate Headings

Apr 22, 2014

I am writing a class that dynamically allocates an array that holds a user-defined number of test scores (test scores go from 0 to 10 both included). Once all the test scores are entered and validated (values only between 0 and 10, both included), the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates theaverage of all the scores.The main program should display the sorted list of scores and the average of the scores with appropriate headings.

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







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