C++ :: Creating A Pointer To Dynamic Integer Array?
Sep 15, 2013
Create in the private section of the ServerGroup class a pointer to a dynamic integer array called servers.
#ifndef ServerGroup_h
#define ServerGroup_h
class ServerGroup {
public:
private:
int *ptr = new int; // needs pointer to a dynamic integer array called servers
};
#endif
View 2 Replies
ADVERTISEMENT
Aug 29, 2014
I'm a little lost with this program. The idea is to dynamically allocate an array and increase its size every time a new integer is inputted by the user. I believe it is a memory leak but as we have just started learning this I'm not sure how to recognise it. Sometimes I can input as many integers as I want other times 2 or 3 before it crashes. When I can input enough values i exit the loop and send it to the sort function and mean calculator function, all works fine there except the last number inputted becomes this huge value not hexadecimal though... As such I'm at a loss as what to look at next, so here you go:
#include <iostream>
using namespace std;
void SelectSort(int [], int);
float MeanCalc(int [], int);
float MedianCalc(int* [], int);
[Code] .....
View 14 Replies
View Related
Nov 29, 2014
I have a class called Book and I am trying to create a dynamic pointer based array of the class. When I try to run the program I keep getting the error: pointer being freed was not allocated at the line of code that says "delete [] A;". I am using Xcode to run the program.
Book *changeArraySize(Book *A, int &size, double factor) {
int i;
int newsize = size*factor;
Book *A2 = new Book[newsize];
[Code] ....
View 7 Replies
View Related
Aug 21, 2014
In case the question was not very clear. Say I have the following code:
int sum(int * x, size_t N){
int sx;
/* returns sum of elements in x */
return sx;
[Code] ....
I'm curious to know whether there is a way that ensures a function does not modify the memory pointed to by a pointer.
View 9 Replies
View Related
Jan 18, 2015
I'm completely new to pointers and have a homework assignment due where I'm supposed to create a user defined dynamic array for player scores. The only errors I'm experiencing is a C2109: subscript requires pointer type and only on the lines that use the int *score; variable (57, 62, 64, 69, 71, and 82). I've already tried adding = nullptr to the variable and that didn't work.
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
void players(string[], int[], int);
void average(int[], int);
[Code] ....
View 3 Replies
View Related
Mar 6, 2015
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
char * form(char *format, ...);
}
[code]...
So, this is the code i have problem with, as far as i can see, function form actually does the same thing that printf does.It probably puts all of the arguments sent to the function together into one string.
View 4 Replies
View Related
Feb 7, 2012
We are supposed to create a menu with an option to add a car (make, model etc) to inventory. I have the structure format set up as far as adding the car, but I am stuck on how to make it a dynamic array of structures or whatnot (I'm not really sure on what I'm trying to do in the first place!) Basically, the user can choose to add a car and its info in. How do you set up the array to add whatever number of cars is needed? Will it be able to add more cars should the user come back to the program later?
This is the instruction of this portion of the project :
Menu Application
Add a car to inventory
Prompted to add
Make, Model and Year, Color, Miles, Price
Finish (adds the car to the file)
Cancel takes you to the main menu
View 1 Replies
View Related
Mar 27, 2014
I have become overwhelmed an frustrated at these current operator overloads!
Currently I still can not get my +,* or istream operators working at all!
#include <iostream>
#include <cstring>
#include "myint.h"
[Code]....
View 1 Replies
View Related
Jan 4, 2013
I'm writing a program in which I have to use a matrix to represent a file in code. because it's a file, the size of the matrix is undefined, and therefore the matrix has to be dynamic. I found out that my compiler doesn't like dynamic multidimensional arrays, so I was thinking of this matrix as a dynamic (monodimensional) array of other dynamic (monodimensional) arrays. My program (and thus this example) uses unsigned chars.
unsigned char variable=something;
unsigned char**matrix=new unsigned char*[lenghtOfMainArray];
for(int rowNumber=0;rowNumber<lenghtOfArray;rowNumber++)
{
[Code].....
View 2 Replies
View Related
Oct 24, 2013
I'm writing a program involving a theoretical hotel. Most of the code is already written, but the part I'm having trouble with involves the very beginning of the code.
The program is designed to read in from an input file the hotel's ID number, the types of rooms it has, the Room Numbers of each room, the base rate for each room, and the rate of additional charge per person.
The code demonstrates inheritance for my Object-Oriented Programming class, as each type of room will inherit from generic class Room, but I can't seem to figure out how to make the double-pointer for dynamic allocation work.
The code is below.
Hotel.h
Code:
class Hotel{
int hotelID;
static const int MAX_SIZE = 101;
Room **rPoint;
[Code] ....
View 11 Replies
View Related
Jan 31, 2013
Do I really need to create a separate pointer to point to dynamic arrays?
T* temp = new T[capacity_ * 2];
T* tIter = &temp; //Do these need to be here?
T* dIter = &data; //Or can I use *(temp + i) and *(data + i)?
(for unsigned int i = 0; i < size_; i++) {
*(tIter + i) = *(dIter + i);
}
View 7 Replies
View Related
Feb 13, 2013
I am using a pair of pthreads that call a pair of functions for ping-pong dma data transfer that are used in a loop for data transfer from an acquisition board. For a large # of waveforms, I ultimately run out of PC memory and the program stops. At the end of each function I use the delete[] command to clear memory for reuse, but the pointer appears to advance by the array size used for the transfer until the location exceeds the 2 GB I have for memory. I can see this happening using the Task Manager performance button time plot and window of total memory used continuing to increase to the limit. The culprit for one of the functions (2nd) is:
unsigned char* dataBuffer2 = (unsigned char *) (pci_buffer2.UserAddr);
where pci_buffer1 and 2 have been set up and allocated in main. I also had the following line in each function process:
double* Rin = new double[length];
and it used up memory twice as fast. When I transferred the last line to an area just prior to main and used a constant 1024 for length, the program ran twice as far before exceeding system memory, so it appears that both lines were forcing new memory assignments and moving the pointers accordingly. In addition to using the delete[] command to free memory unsucessfuly at the end of each function procedure, I ended up closing the memory at the end of each procedure, then reallocating it again with the idea that the pointer would be set back to the original value, but it still seems to icrement along. So, neither approach appears to allow reuse of the memory because the pointer continues to march along. Using Visual C++ 6.0 to compile.
View 1 Replies
View Related
Aug 2, 2013
I'm currently reading the C++ Guide for Dummies
Anyway right now I'm working with pointers and classes, and when I create a new pointer for my class it looks like this...
Pen *Pointerpen = new Pen;
But in the book they threw in this...
Pen *Pointerpen = new Pen();
Can you actually designate memory space for a function? Or was this a typo on their part? It's never come up before and they didn't explain it.
View 5 Replies
View Related
Jun 26, 2014
I have a class in my application that only needs to be created once, but the object needs to be available to all other classes in my application if necessary. Since declaring everything static can be restrictive (as I understand), I created a class like this:
class Foo {
// Data members
// Constructor/Destructor
// Functions
};
extern Foo* myFoo = new Foo();
And then the global variable gets deleted at the very end of the main method when everything is done:
#include "Foo.cpp" // (yes, I know this is normally bad, this is how I'm required to code)
int main() {
// do stuff
delete myFoo;
return 0;
}
These won't link, though, because I get undefined reference linking errors to myFoo wherever I use it. I'm pretty sure this means I'm creating a singleton wrong, but I'm not sure what I'm doing wrong -- there's no const conflicts and the pointer is properly initialized (to my understanding). If there's a better way to do this than extern, I'm completely open to it, as long as it's understandable and works.
View 9 Replies
View Related
Oct 25, 2013
I am trying to assign the integer value to unsigned char array. But it is not storing the integer values. It prints the ascii values. Here the code snippet
Code: uchar uc[100];
for(i=0;i<100;i++)
{
uc[i] = i;
}
The values which are stored in uc[] is ascii values.I need the integer values to be stored in uc[]. I tried to do it with sprintf. but the output is not as expected. if I print the uc[i] it should diplay the value as 0,1,2....99.
View 9 Replies
View Related
Mar 22, 2014
I have this code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
[code]....
What I want is basically to assign to the *p the pointer of the string so that i could do the following printf(" print string %s",*p); so i dont know how to do that.
View 6 Replies
View Related
Mar 18, 2013
The warning: :63:7: warning: passing argument 1 of fputc makes integer from pointer without a cast [enabled by default]
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
[Code]....
View 2 Replies
View Related
Nov 28, 2013
I am getting error"incompatible integer to pointer conversation..." and don't know how to fix this. In my code, user inters line like this (3+3*(55-52)) I need to separate number from the line so I can do other operation.here is my code
Code:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main(){
[Code]....
View 1 Replies
View Related
Dec 15, 2013
Code:
#include <stdio.h>
#include <stdlib.h>
#define CLASS 4
#define STUDENT 11
#define GRADE 5
[Code] ....
Giving me that error on 75:10
avesub+=grade[k];
and 90:17
donkeypunch+=grade[j][k];
not sure exactly why
View 8 Replies
View Related
Aug 5, 2014
I have written this program
Code:
#include <stdio.h>
#include <string.h>
int main(void) {
struct point {
[Code] ....
error: request for member is not a structure or union
printf("%d
,%d
",p1->i,p1->j);
I was just trying to typecast a integer pointer to structure pointer, but throws error as shown.
View 5 Replies
View Related
Apr 21, 2013
I am trying to make a program that prints out an 8 by 8 board with 1 queen on each row.
For example, if the user enters: 0,3,4,0,0,7,6,7
the program should create following board and print:
Q.......
...Q....
....Q...
Q.......
Q.......
.......Q
......Q.
.......Q
I think I am doing it right but im getting a couple of errors that i cant fix ...
#include <iostream>
using namespace std;
int main(){
int x [8] [8], r, c;
for(c = 0; c <= 8; c++){
cout << " Enter a number from 1 to 7 " << endl;
[Code] .....
these are the errors :
assignment15_meem.cpp: In function âint main()â:
assignment15_meem.cpp:7: error: no match for âoperator>>â in âstd::cin >> x[c]â
assignment15_meem.cpp:10: error: ISO C++ forbids comparison between pointer and integer
View 7 Replies
View Related
May 4, 2014
template <class ST>
bool OrderedSet<ST>::IsIn (const ST & value) const {
for (LNode * np = first; np != NULL; np = np -> next)
if (np -> next == value)
return true;
return false;
}
View 1 Replies
View Related
Jul 27, 2013
when i compile the following program i get a compiler warning, but i don't understand why. for me the code seems to be all right and does legitimate this warning. so here is the code
// multiplication.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "crypto.h"
#define PROGNAME "multiplication"
void usage();
int isnumaber(char* str);
[code]....
View 2 Replies
View Related
Mar 29, 2012
In the code below:
void test(int * i) {
i = new int;
*i = 10;
cout << "i: " << *i << endl;
[Code] ....
The outuput is:
i: 10
t: (some memory position).
If I really want to allocate an integer variable to t, i need to modify the void test(int * i) procedure to void test(int *& i).
View 2 Replies
View Related
Jun 3, 2013
I have an integer pointer and i want its address without allocating memory,
main() {
int *a;
cout<<a;
}
This is giving 00000000 and its but obvious. Now if i use address of a (&a) along with *a,
main() {
int *a;
cout<<a;
cout<<&a;
}
'cout<<a' gives me a constant address but 'cout<<&a' gives me different address.
what is the reason behind & and why behaviour of 'cout<<a' changes when using with &.
View 8 Replies
View Related
Jan 23, 2014
int hash = 0;
char *strings[100];
if((int)strings[i] != 0)
if((int) strings[hash] != 0)
while((int) strings[hash] != 0)
if((int)strings[hash] != 0)
if((int)strings[hash] != 0)
View 12 Replies
View Related