C/C++ :: Allocating Memory For 2D Array In Other Class?

Jun 10, 2014

I'm making some multi-threaded program, but thats not my problem as i've done that already. I have a class with user-functions containing a structure which then contains a two dimensional array for each user with 25 elements. So I dont want to limit the user and make the array for example with just 10 rows, but allocate the needed memory to match the amount of 'users' a potential user of my program would want. The problem is, that i know how i should allocate it using 'new int' but it just doesnt work ! It gives an error:

Error: no operator "=" matches these operands

UserStuff.h:
struct userDataStruct {
bool* isAdmin;

[Code]...

Then, in some completely other class function inside the file mentioned above: (I know i could do a function in CUsers class which could allocate the memory, but I have this function which is used for some other things and it already has the amount of max users

void OtherClass::somefunction(maxusers)
{
// This gives an error: Error: no operator "=" matches these operands
curUsers->uData.userNumbers = new int*[maxusers]; //maxusers is the int variable of max users specified by the client
// However this doesn't
for( int i = 0 ; i < maxusers ; i++ )
curUsers->uData.userNumbers[i] = new int[25]; // 25 columns, this doesnt give any error
}

I'm not really sure what I'm doing wrong. Doing this in some function from CUsers class works (without curUsers-> or with, doesn't give any error) but doing it from some other class's function doesnt.

View 11 Replies


ADVERTISEMENT

C :: Allocating Memory To Array Of Struct?

Sep 9, 2013

Do you have to allocate memory(malloc) for an array of structs? Example:

Code:
typedef struct{
char * name;
}First;
struct name{
First fname;
};
struct name Names[10];

View 7 Replies View Related

C++ :: Allocating Memory For Vectorized 2D Array?

Mar 2, 2015

I want to be able to dynamically allocate and index an array like the following: vv2d[1][2].x and vv2d[1][2].y. In order to accomplish that I have chosen to use a std::vector of a std::vector of a 2D point class.

Code:
/// Here is my templated version of a 2d point class which I have adopted from
/// one by Alexander Chernosvitov, Function Graphics, 2001 (see ogview.h)
/// http://www.codeguru.com/cpp/g-m/opengl/article.php/c5581/Function-graphics-in-3D.htm
template <typename T>

[Code]....

Boundary violation occurs as soon as vv2d[1][0].x is encountered. I believe the problem is my inability to dynamically allocate the size of the (primary) typedef vector. However, eliminating the typedef for the following does not change the result. Further examination shows the vv2d[1][0] size and capacity to be 0.

Code:
vector<vector<CPoint2D<double>>> vv2d;
vv2d.resize(3);
vv2d[0].resize(3);

View 14 Replies View Related

C :: Allocating And Freeing Memory For Array Of Structures

Jul 27, 2014

I have to allocate memory for an array of structures, and my structure looks as following:

Code:
typedef struct {
char *name;
Uint *start_pos;
Uint len;
}
example_struct;

And now I want to allocate memory, for a variable number (so an array) of example_struct, so I first do:
Code:

example struct *all_struct;
int total_num = 3;
//will be set somehow, but for the example I set it on 3 all_struct = malloc (sizeof(example_struct) * total_num);

And now, as far as I now, I will have to allocate for each field of the structure memory, in order to be able to use it later. But I have problem at this point, a problem of understanding:

- I just allocated memory for 3 structures, but don't I have to allocate then memory for each structure separately, or can I just now allocate the fields like this:

Code: all_struct[0].name = malloc.....

But if yes, why the hell this works...

View 10 Replies View Related

C :: Dynamically Allocating Memory?

Sep 18, 2013

I have created a database for music cds:

Code:

#include<stdio.h>
#include<stdlib.h>
#define array
typedef struct cd_database

[Code]....

When I am using malloc instead of arrays the code is not working properly after exit. I have tried alot but can't came up with a way

View 5 Replies View Related

C :: Error Allocating Memory

Jun 1, 2014

Code:

# include <stdio.h>
# include <math.h>
# include <stdlib.h>
# include <malloc.h>
}

[code]...

I am compiling it on a 64 BIT ubuntu machine having 64GB ram using gcc 4.6 compiler. I am getting the following output Error allocating memory. But (914*866*2724) is approximately 8 GB, Whats wrong with the code?

View 7 Replies View Related

C++ :: Not Allocating Enough Memory To Pointer?

Jan 24, 2014

I wrote the following C++ constructor, and I get an error - BUFFER too small on strcpy_s

Trace::Trace(const char *str) {
if (str) {
int len = strlen(str);
this->m_name = new char[len+1]; // asking for 'len+1' memory elements of char
strcpy_s(m_name, len, str); // **** I get here an error "BUFFER TOO SMALL" ****

[Code] .....

m_name is a private data member of type char* .

View 3 Replies View Related

C :: Under-allocating Memory For A Tagged Union

Mar 6, 2015

What i'm interested in is the behavour of a struct/union constructed like this:

Code:
typedef struct {
uint64_t num1;
uint64_t num2;
} st_a;

typedef struct {
uint64_t num1;
uint32_t num2;

[Code] .....

What kind of behavour could I expect from object, in the following cases:

1. newsomestruct(0)->u.a.num1 = 2;
2. newsomestruct(1)->u.b.num1 = 2;
3. newsomestruct(0)->u.a.num2 = 2;
4. newsomestruct(1)->u.b.num2 = 2;
5. newsomestruct(0)->u.b.num1 = 2;
6. newsomestruct(1)->u.a.num1 = 2;
7. newsomestruct(0)->u.b.num2 = 2;
8. newsomestruct(1)->u.a.num2 = 2;
9. Code:

somestruct* ss1 = newsomestruct(0);
somestruct* ss2 = newsomestruct(1);
* ss1 = * ss2; 10. Code: somestruct* ss1 = newsomestruct(0);
somestruct* ss2 = newsomestruct(1);
* ss2 = * ss1;

This is what I'd expect, but I can't find any evidence online in C standards or elsewhere:

1. Works as expected, sets the value of a.num1 to 2.
2. Works as expected, sets the value of b.num1 to 2.
3. Works as expected, sets the value of a.num2 to 2.
4. Works as expected, sets the value of b.num2 to 2.
5. Works as expected, sets the value of b.num1 to 2.
6. Works as expected, sets the value of a.num1 to 2.
7. Works as expected, sets the value of b.num1 to 2.
8. Crashes/Memory Corruption, attempted to alter memory outside struct.
9. Works as expected, * ss1 == * ss2
10. Crashes/Memory Corruption, attempted to alter memory outside struct.

I've tested simular code on my machine (Xubuntu 14.04LTS compiled with gcc on -O3) and it appears to be reliable, given that you stick with acessing the type tagged in the struct or the common initial union struct members (in this case num1).

View 6 Replies View Related

C++ :: Integer Pointer - Get Address Without Allocating Memory

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

C++ :: Allocating Array Of Pointers To Dynamically Allocated Array?

Jan 18, 2014

I'm trying extremely hard to understand pointers and I have the basic concept down.. I feel as though my knowledge of dynamically allocated pointers and pointers in general is not enough to understand the logic behind what I'm trying to do. The problem is that the donations array must be able to accept any number of donations. I've made it do just that, but there is also an array of pointers which must each point to the same element in the donations array. The program works if I assign int *arrPtr[100] for example, but it does not work if I try to dynamically allocate it to accept the same number of elements for donations entered by the user. Here it's the snippet

#include <iostream>
using namespace std;
//Function Prototypes

[Code]....

View 2 Replies View Related

C++ :: Dynamically Allocating Multidimensional Array?

Nov 24, 2013

Working on this one from the Jumping into c++ book. The book asks that I create a multidimensional array at run time based on user input and fill it with a multiplication table

My code compiles fine but throws an uninitiated error for p when I try and run it.

Code:
void multiDimentionalMultiplication(int x, int y, int z){
int ***p;
**p = new int[x];
std::cout << "Allocating array.

[code]....

View 8 Replies View Related

C++ :: Dynamically Allocating One Dimension Of 3D Array?

Jun 14, 2013

I am trying to figure out the syntax to dynamically allocate a single dimension of a triple dimensional array. Basically i have a 2D array of structs. but each struct is an array (basically rows of the information). The rows of this structure need to be allocated dynamically, but the height and width of the overarching structure are static.

Basically: Struct * ts_FieldInfo[100][100] = new Struct[Class.returndataitems()];

View 2 Replies View Related

C++ :: Allocating Number Of Element On Array

Mar 7, 2013

#include <iostream>
using namespace std;
int main() {
int elm = 0;
int size = 0;
int *array;

[Code] ....

View 6 Replies View Related

C/C++ :: Dynamically Allocating Pointer Array

Apr 15, 2014

so I have this code that dynamically allocates a pointer array increasing and removing elements of the array as its operated on.then it sorts and prints out the array when the user is finished operation on the array. i get this error message when running the program though.

"Unhandled exception at 0x0F474F98 (msvcr110d.dll) in Lab10_VarArray.exe: 0xC0000005: Access violation reading location 0xCCCCCCC0."

this is my code

#include <iostream>
#include <cstdlib>
#include "Header.h"
using std::cout; using std::endl; using std::cin;
int main(void) {
char op='x';

[Code]...

View 3 Replies View Related

C :: Segmentation Fault When Dynamically Allocating 2D Array

Apr 20, 2013

This is a homework assignment where I have to read a file into a dynamically allocated 2d array. The file format is
10
Jim 3.6
Jake 4.0
Will 3.0
Sara 3.4
Mike 2.5
Ellen 2.9
Melissa 3.9
Eric 3.8
John 3.5
Beth 3.9

where 10 is the number of students followed by the students and the gpa's. There is more to the program but I have not implemented it yet because I am getting a segmentation fault. The output I am getting when I print the array is
Jim 3.6
Jake 4.0
Will 3.0
Sara 3.4
Segmentation fault

I can see where the problem lies. If I raise value for row when I am allocating the rows of the array, all of the names print. I just do not see why I need to. From my understanding the row * sizeof(char*) should give me enough room for 10 entrie.

Code:
#include <stdio.h>
#include <stdlib.h>
void sort();
int main()

[Code] .....

View 6 Replies View Related

C/C++ :: Dynamically Allocating Array Passed As Parameter

Apr 18, 2015

I'm trying to dynamically allocate a standard array at runtime in the function of a class where the array is "owned" by the calling class. The calling class knows nothing about the array before it makes the call to create the array other than the datatype of the array. But the full array of data needs to be returned.

It appears that the pointer being passed makes a copy of the pointer on the stack and then when the function returns it pops it off the stack and the array is a memory leak because the pointer is once again a nullptr as it was before being passed and the array has not been deallocated with delete yet (as it should not have been).

(Edit:Unexpected value of MyArray being a nullptr instead of pointing to an array after returning from line 09.)

class Class1 {
void FunctionA() {
Class2 OwnedClass;
int* MyArray = nullptr;
int SizeOfMyArray = 0;

[Code] ....

View 14 Replies View Related

C++ :: Memory Usage Of A Program With CLASS And Without CLASS?

Nov 6, 2014

I developing a C++ Program to deal with huge data computation. I am using Pointers to hold the data during computation. Current program i developed without using CLASS.

I would like to ask that will the memory usage will be reduced and computational time will be shortened if I modified my program implementing the CLASS into it ?

View 1 Replies View Related

C++ :: Dynamic Memory Allocation In A Class

Apr 17, 2014

I'm having problems with this code:

#include <iostream>
using namespace std;
class Foo {
public:
Foo( int n );// Constructor
~Foo();// Destructor
int *ptr;
int N;

[Code] ....

I'm using Visual C++ 2008 version. The problem arises at the end, after the sentence 'system("pause")' is reached, which makes me think that the problem happens when calling the destructor. The destructor is called twice, the first time it's called is in the function print. The problem seems to be that the destructor can only be called once.

I know I can avoid this situation by defining the function print like this:

void print ( const Foo &f )
...

but I would like to know if there is some way I can do this keeping the definition that I've provided.

View 2 Replies View Related

C++ :: How Class Objects Stored In Memory

Aug 23, 2014

For example when I have:

Class A{
B objectB;
};

Now when I instantiate the object of class A like:

main(){
A objectA;//option 1
A* pObjectA = new A();// option2
}

How is objectB stored in memory (stack heap etc.) for both options??

View 1 Replies View Related

C++ :: Memory Address Of Class Member Variables?

Jun 22, 2013

Suppose I have two classes, MyClassX and MyClassY, each with two member variables, as defined below. I create an object instance of each class, and then create a pointer to each member variable for each object:

Code:
class MyClassX
{
public:
int a;
double b;
MyClassX(int _a, double _b)

[code]....

After converting the hexadecimal to decimal, it appears that with MyClassX, pxb is 8 bytes from pxa, whereas for MyClassY, pya is only 4 bytes from pyb. This makes sense for MyClassY, because the first member variable to be stored is an int, and so will occupy 4 bytes. However, why should this be any different for MyClassX, which also has an int as the first member variable, so shouldn't this also occupy 4bytes?

The reason I have come across this problem is that I am looking into streaming objects to memory and then loading them again. (I know boost can do this, but I am trying it out myself from scratch.) Therefore, this is causing an issue, because I cannot just assume that the size of memory occupied by an object is just the sum of the sizes of its member variables. MyClassX is 8 bytes larger than MyClassY, even though the intuition is that it should only occupy 4 bytes more due to a double being replaced by an int.

View 4 Replies View Related

C++ :: Variable Inside A Class - How To Manage Memory Allocation

Jul 30, 2014

recently I developed a class header only in C++ to deal with byte arrays. Since I developed programs in other languages that had some libraries to dial with byte arrays, I developed one that the syntax is very similar to other languages.

Since I'm not very familiar with memory management, I was concerned about somethings I've read about (ex: memory fragmentation).

Here is a very simple example of my practice:

class ByteArray {
public:
ByteArray(size_t size) {
buffer = (int8_t*)malloc(size);
}

[Code].....

The class is intended to be used as part of comunication protocol in a webserver, byte arrays are created and destroyed a lot. Should I use pools? Is there a better practice? Am I doing everything wrong (laugh)?

For those who wants to see the entire class: [URL]

View 9 Replies View Related

C++ :: Class Memory Allocation - Store Number Of Elements In Table

Mar 31, 2013

Say I have a class with a few member functions, and only two data members: an int* Table; and an int Size;, to store the number of elements in Table.

I'm using a copy constructor that takes in two parameters: int* table, int size. In this case, is the address that table points to the same address as the object that table is part of? And furthermore, is it possible to say table.Size? I want to compare the passed array's size to the passed size.

View 2 Replies View Related

C++ :: Vector Of Int Class - How To Delete Pointer X In Destructor To Free Memory

Feb 25, 2015

An attempt to create a class which is basically a mimic of vector<int> i don't seem to know how to delete pointer x in a destructor to free memory, also on pushback and pushfront methods, i can't free y when i implement delete[] y; y=NULL; i get some NULL out put when cout 'ing the object in main, why is that happening and how do i free memory y.

#include<iostream>
using namespace std;
class vectorOfint{
int* x;
int size;
public:
vectorOfint();

[Code] .....

View 6 Replies View Related

C++ :: Allocating Space Only For Two Characters

Jan 15, 2013

I am allocating space only for two characters but it fits all of them, if you run this it will print the whole string literal "hello my friend". How is that possible?

I am using gcc 4.6.3., I know about strncpy().

#include<iostream>
#include<cstring>
using namespace std;
int main(){
char* str = new char[2];
strcpy(str, "hello my friend");
cout << str << endl;
return 0;
}

View 4 Replies View Related

C++ :: Declare Not Allocating Storage?

Jan 22, 2014

decalration won't allocate storage, while definition will. This is a test program:

#include <iostream>
using namespace std;
extern int ei;
int i;

[Code].....

Others are all fine in this program except ei.
compiler error: undefined reference to ei.

I understand ei is only declared so there is no memory address, but when I do ei=1, then ei completed it's definition, why still cannot use pei to get it's address?

View 9 Replies View Related

C++ :: Dynamically Allocating Hash Table Using Data From File

Nov 5, 2013

I have an abstract based class and three derived classes. I also have a templated hash table class(using chaining as my collision resolution method, an array of stl lists), and a class to parse commands from a file, this also holds the instantiation of the hash table. My question is that since my command parsing class's constructor instantiates the hash table in the main driver(unable to modify) how can I make this dynamically allocated using data from the file?

template<class T>
class hashTable{
public:
hashTable(int size);
~hashTable();

[Code] .....

View 3 Replies View Related







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