C++ Pointer array declaration - c++

Currently I have a several classes with an array defined as 'float myIDs'. I want to move the variable into my parent class and change it to a pointer ('float * myIDs').
Currently I'm declaring its values like this:
float myIDs[] = {
//Variables
};
As its now a pointer, I thought that it would be roughly the same:
myIDs = new float[] = {
};
but that doesnt seem to be working. I'm not sure how to solve this as I've never had to declare a pointer array like this before.
Can anyone help me please?
Thanks

Note that you're not allocating an array of pointer but just an array of float, so basically you two array would have the same type, they just won't be stored in the same memory space.
Only a statically allocated array can be initialized this way, a dynamically allocated one cannot be initialized to anything other than zero.
myIDs = new float[]();
But if you know the elements to put in the array, you don't need to allocate it dynamically.
If you want to allocate an array of pointer, you have to do this :
float* myIDs[size]; // statically
float** myIDs = new float*[size]; // dynamically
But only the statically allocated one (the first one) can be initialized the way you describe and of course, it must be initialized with pointers.

If you want to declare array in a dynamic way, you can do it like this:
float *array = new float[size];
array[0] = first_value;
array[1] = second_value;
etc;
Just remember to free memory when you no longer need it (e.g. in a class destructor)
delete [] array;

If you want a dynamically allocated array you should use the following format (what you did seems more like C# not C++)
//The declaration of the object in the class
float *myIDs;
//The allocation it self (you must know which size you want to allocate at this point)
myIDs = new float[size];//bring change "size" to whatever you need is.

Consider the following snippet,
#include<iostream>
#include<stdlib.h>
int main(void)
{
int a[] = {1,2};
a =new int[2];
delete(a);
return 0;
}
This gives an error error: incompatible types in assignment of ‘int*’ to ‘int [2]’.
I am statically creating an array of int . a is a pointer(it is of type int[2]) but it can't be used to point to other dynamically allocated arrays because they return pointer of type int*.
If you want to create an array dynamically you have to assign its address to a float*
float * a = new float[10] ;
Refer this too.

The easiest way is:
float *myArray[size];
Example
#include <iostream>
using namespace std;
float* findMax (float* myArray[], int size) {
float max = 0;
int index = 0;
for (int i = 0; i < size; i++) {
if ( *myArray[i] > max) {
max = *myArray[i];
index = i;
}
}
return myArray[index];
}
int main()
{
float a = 1.25;
float b = 2.47;
float c = 3.92;
float d = 4.67;
float e = 5.89;
float f = 6.01;
float *myArray[6];
int len = *(&myArray + 1) - myArray;
myArray[0] = &a;
myArray[1] = &b;
myArray[2] = &c;
myArray[3] = &d;
myArray[4] = &e;
myArray[5] = &f;
cout << "Number of even values are : " << findMax(myArray, len) << endl;
return 0;
}

If you want an array of pointers to float, you must declare it as such. You declared just an array of floats. The name of the array is a pointer of course, but in the C sytnax it is treated the same and just a convenience.
float *myIDs[] = {
//Variables
};
myIDs = new *float[n] = {
};
Alternatively you can use
float **myIDs;
myIDs = new **float;
And access it the same way like an array:
float *x = myIDs[i];

Related

How do access and assign value for an int pointer datatype on struct?

If i want to give a value to int *num2, how would i do this ?
#include <stdio.h>
int main(int argc, const char * argv[]) {
// insert code here...
struct az {
int num1;
int *num2;
};
struct az test;
test.num1 =20;
printf("\n %d",test.num1);
return 0;
}
int *num2; is a pointer to some place in the memory that contains an int value. Since you just declare a pointer without any memory allocating, it is simply points to nothing (or random place).
You have to allocate memory then assign a value to this memory:
test.num2 =new int(20);
However, do not forget to delete this allocated memory when you finish:
delete test.num2;
The real question here, do you really want that pointer here? probably not.
You can assign num2 in the same way as num1. Of course, since it's not an int but an int*, you can't assign 20 to it - you can assign the address of some int variable instead:
struct az {
int num1;
int *num2;
};
struct az test;
test.num1 = 20;
int i;
test.num2 = &i;
To assign a value to the integer that test.num2 points to, you must de-reference the pointer with an asterisk, like so:
test.num2 = new int(20); // Allocates memory and sets the pointer to this memory
*(test.num2) = 10; // Assign a value of 10 to the int that test.num2 points to
To assign an address to the pointer, you must either assign another pointer to it, or assign it's value by referencing an int with an ampersand:
test.num2 = new int(20); // Allocates memory and sets the pointer to this location
int *ptr = new int(10);
delete test.num2; // Free memory that test.num2 pointed to
test.num2 = ptr // Makes test.num2 point to the same int as ptr
int x = 10;
test.num2 = &x; // Makes test.num2 point to x
In your example num2 is typically used as a pointer to a dynamically allocated array. However, neither num2 nor num1 is a good name in this case. Let's instead declare our dynamic array type as a record with the fields length and items:
struct Array {
int length;
int *items;
};
typedef struct Array Array;
If we have a variable a of type Array, before we can assign values to its elements we must first allocate memory for the array. In C this is typically done with the function malloc. This function, however, is rather error prone to use so it's a good idea to define a macro function which simplifies memory allocation:
#define NEW_ARRAY(ptr, n) (ptr) = malloc((n) * sizeof (ptr)[0])
After having called NEW_ARRAY we can assign values to the array:
int length;
Array a;
length = 20;
a.length = length;
NEW_ARRAY(a.items, length);
a.items[0] = 37;
a.items[1] = 19;
...
When we are done with the array we can release the memory used by the elements:
free(a.items);

How to replace one dynamic array with another without causing memory leak?

This is a simple question and I'm kind of a beginner, but I would just like to confirm this.
Say I have a class object int * data, which is pointing to an array of 5 ints. Say I have a function that would replace data with 5 arguments, as illustrated below.
void replaceData(int a, int b, int c, int d, int e){
int * temp = new int[5];
temp[0] = a;
temp[1] = b;
temp[2] = c;
temp[3] = d;
temp[4] = e;
data = temp;
}
I am concerned that this would cause a memory leak, because if data already had 5 ints in there, they would still be floating around. I am tempted to change it in the following way to avoid this:
void replaceData(int a, int b, int c, int d, int e){
int * temp = new int[5];
temp[0] = a;
temp[1] = b;
temp[2] = c;
temp[3] = d;
temp[4] = e;
delete data; //would this prevent a memory leak?
data = temp;
temp = 0;
}
Thank you very much for your input!
If data was allocated in the same way as you are allocating temp then yes, you would need to free the old array first before modifying the pointer - otherwise you will have a memory leak.
Note that if you allocate an array (new int[5]) then you must free the array like this:
delete[] data;
Also, it's much better to use an STL container like std::vector rather than allocating arrays manually, as all the memory management is handled for you.
Almost.
The delete operator is for pointers to single objects. You need to do delete[] data for this to work. Zeroing the temp pointer at the end doesn't make a difference.
Your code would be correct if it were delete[] instead of delete. If the size is hardcoded though, there's no point in using a dynamically allocated array (at least not in this context). Just use an int temp[5];.
Better yet, just use vectors. Then it's a simple call to std::swap.

initializing string arrays within structs

#include<iostream>
#include<string>
using namespace std;
int main(void) {
struct STRCT {
int num;
string str1,
arrStr1[],
str2,
arrStr2[];
};
int a;
string b[2],
c[3],
d,
e;
a = 10;
b[0] = "hello";
b[1] = "world";
c[0] = "stack";
c[1] = "over";
c[2] = "flow";
d = "random";
e = "text";
//how do i intialize the arrays (arrStr1[] and arrStr2[]) in aStruct along with the rest of items?
//is it like this?
//i want aStruct[] to be an array and i want its size to be declared from the start to later be filled with vals
STRCT aStruct[2];
//then later in the program i want to assign aStruct[] vals
aStruct[0] = {a, //int
d, //string
{b}, //string[]
e, //string
{c}}; //string[]
}
so basically i want to make a struct array with arrays inside then get the proper vals and then assign the proper vals to the arrays inside the struct array. thank you very much in advance for the help
Array declarations in your struct are simply illegal. C++ does not support size-less array declarations as class members. And even if some C++ compiler supports a C99-style "struct hack" declaration, only one size-less array is allowed and the array must be the last member of the struct.
You want to have arrays inside your struct - you have to give them specific compile-time sizes. Without specific compile time size you'll have to use pointers or std::vector.
In your example b has size 2 and c has size 3. You can declare your struct with the same sizes
struct STRCT {
int num;
string str1, arrStr1[2], str2, arrStr2[3];
};
and then initialize it as follows
STRCT aStruct[2] =
{
{
a,
d,
{ b[0], b[1] },
e,
{ c[0], c[1], c[2] }
}
// The rest of the array is value-initialized
};
That's just as far as you can get with ordinary arrays. It you want something more flexible, embedding arrays straight into the struct won't help you here. Either construct the necessary memory structures manually or use std::vector.
In C++ this is illegal
string arr[2] = {"This","is"};
string arr1[2];
arr1 = arr;
There is nothing like "copy an entire array into another array". The array elements must be copied individually.
Second you cant declare arrays of unknown size
You can modify your struct declaration by declaring string array of fixed size and do this
for(int i =0; i< 2; i++)
{
aStruct[i].num = a;
aStruct[i].str1= d;
for(int j=0;j<2;j++)
{
arrStr1[i] = b[i];
}
aStruct[i].str2= e;
for(int k=0;k<3;k++)
{
arrStr2[i] = c[i];
}
}
I suggest instead of string arrStr1[] ,string arrStr2[] , b[2] and c[2] make use of std::vector. That will help you in avoid hard coding the conditions in for loop.

Array as array[n] and as pointer array*

What is the difference when array is declared as array[n] or as pointer array* according to example below? I guess that for example both 'a' and 'c' point at the first element of array, but they behave different.
#include <iostream>
int main() {
int a[3] = {1};
int b[5];
std::cout << *a << std::endl; //prints 1 - ok
//a = b; //error during compilation
int* c = new int[3];
c[0] = 2;
int* d = new int[5];
std::cout << *c << std::endl; //prints 2 - ok
c = d; //works ok!
return 0;
}
Long story short - they are essentially the same, but ever so slightly different.
From what I've gathered from http://c-faq.com/aryptr/aryptr2.html , whilst they can both act as a pointer to the front of an array, when you declare an array as
int a[3];
you are essentially binding the size of '3' to your variable a, along with the fact it's an array. Hence, when you try to assign b, of size 5 to a, you get a compilation error.
In contrast, when you write
int * a;
You are merely saying 'this is a pointer that may point to an array', with no promise on the size.
Subtle, isn't it?
The difference between the following two lines:
int g[10];
and
int* h = new int[10];
is that the second is dynamically-allocated, whereas the first is statically-allocated.
For most purposes, they are identical, but where in memory they end up living is different.

Dynamically allocate C struct?

I want to dynamically allocate a C struct:
typedef struct {
short *offset;
char *values;
} swc;
Both 'offset' and 'values' are supposed to be arrays, but their size is unknown until runtime.
How can I dynamically allocate memory for my struct and the struct's arrays?
swc *a = (swc*)malloc(sizeof(swc));
a->offset = (short*)malloc(sizeof(short)*n);
a->values = (char*)malloc(sizeof(char)*n);
Where n = the number of items in each array and a is the address of the newly allocated data structure. Don't forget to free() offsets and values before free()'ing a.
In C:
swc *s = malloc(sizeof *s); // assuming you're creating a single instance of swc
if (s)
{
s->offset = malloc(sizeof *(s->offset) * number_of_offset_elements);
s->values = malloc(sizeof *(s->values) * number_of_value_elements);
}
In C++:
try
{
swc *s = new swc;
s->offset = new short[number_of_offset_elements];
s->values = new char[number_of_value_elements];
}
catch(...)
{
...
}
Note that in C++, you might be better off using vectors as opposed to dynamically allocated buffers:
struct swc
{
std::vector<short> offset;
std::vector<char> values;
};
swc *a = new swc;
Question: is values supposed to be an array of individual characters or an array of strings? That would change things a bit.
EDIT
The more I think about it, the less satisfied I am with the C++ answer; the right way to do this sort of thing in C++ (assuming you need dynamically allocated buffers as opposed to vectors, which you probably don't) is to perform the memory allocation for offset and values as part of a constructor within the struct type, and have a destructor deallocate those elements when the struct instance is destroyed (either by a delete or by going out of scope).
struct swc
{
swc(size_t numOffset = SOME_DEFAULT_VALUE,
size_t numValues = SOME_OTHER_DEFAULT_VALUE)
{
m_offset = new short[numOffset];
m_values = new char[numValues];
}
~swc()
{
delete[] m_offset;
delete[] m_values;
}
short *m_offset;
char *m_values;
};
void foo(void)
{
swc *a = new swc(10,20); // m_offset and m_values allocated as
// part of the constructor
swc b; // uses default sizes for m_offset and m_values
...
a->m_offset[0] = 1;
a->m_values[0] = 'a';
b.m_offset[0] = 2;
b.m_values[0] = 'b';
...
delete a; // handles freeing m_offset and m_values
// b's members are deallocated when it goes out of scope
}
You have to do it seperately. First allocate the struct, then the memory for the arrays.
In C:
swc *pSwc = malloc(sizeof(swc));
pSwc->offset = malloc(sizeof(short)*offsetArrayLength);
pSwc->values = malloc(valuesArrayLength);
In C++, you shouldn't be doing anything like that.
In C:
typedef struct
{
short *offset;
char *values;
} swc;
/// Pre-Condition: None
/// Post-Condition: On failure will return NULL.
/// On Success a valid pointer is returned where
/// offset[0-n) and values[0-n) are legally de-refrancable.
/// Ownership of this memory is returned to the caller who
/// is responsible for destroying it via destroy_swc()
swc *create_swc(unsigned int size)
{
swc *data = (swc*) malloc(sizeof(swc));
if (data)
{
data->offset = (short*)malloc(sizeof(short)*n);
data->values = (char*) malloc(sizeof(char) *n);
}
if ((data != NULL) && (size != 0) && ((data->offset == NULL) || (data->values == NULL)))
{
// Partially created object is dangerous and of no use.
destroy_swc(data);
data = NULL;
}
return data;
}
void destroy_swc(swc* data)
{
free(data->offset);
free(data->values);
free(data);
}
In C++
struct swc
{
std::vector<short> offset;
std::vector<char> values;
swc(unsigned int size)
:offset(size)
,values(size)
{}
};
You will need a function to do this.
Something like (my C/C++ is rusty)
swc* makeStruct(int offsetCount, int valuesCount) {
swc *ans = new swc();
ans->offset = new short[offsetCount];
ans->values = new char[valuesCount];
return ans;
}
myNewStruct = makeStruct(4, 20);
Syntax may be a bit off but that is generally what you are going to need. If you're using C++ then you probably want a class with a constructor taking the 2 args instead of the makeStruct but doing something very similar.
One thing to add to the many correct answers here: you can malloc an over-sized structure to accommodate a variable sized array in the last member.
struct foo {
short* offset;
char values[0]
};
and later
struct *foo foo1 = malloc(sizeof(struct foo)+30); // takes advantage of sizeof(char)==1
to get room for 30 objects in the values array. You would still need to do
foo1->offsets = malloc(30*sizeof(short));
if you want them to use the same size arrays.
I generally wouldn't actually do this (maintenance nightmare if the structure ever needs to expand), but it is a tool in the kit.
[code here in c. You'll need to cast the malloc's (or better use new and RAII idioms) in c++]
swc* a = malloc(sizeof(*a));
a->offset = calloc(n, sizeof(*(a->offset)));
a->values = calloc(n, sizeof(*(a->values)));
You should not cast void* in c... in c++ you must!
Use malloc function or calloc to allocate memory dynamically .
and search it on google to get examples.
The calloc function initializes allocated memory to zero.
Since nobody has mentioned it yet, sometimes it is nice to grab this chunk of memory in one allocation so you only have to call free() on one thing:
swc* AllocSWC(int items)
{
int size = sizeof(swc); // for the struct itself
size += (items * sizeof(short)); // for the array of shorts
size += (items * sizeof(char)); // for the array of chars
swc* p = (swc*)malloc(size);
memset(p, 0, size);
p->offset = (short*)((char*)swc + sizeof(swc)); // array of shorts begins immediately after the struct
p->values = (char*)((char*)swc + sizeof(swc) + items * sizeof(short)); // array of chars begins immediately after the array of shorts
return p;
}
Of course this is a bit more difficult to read and maintain (especially if you dynamically resize the arrays after it is first allocated). Just an alternative method I've seen used in a number of places.
Most of the answers are correct. I would like to add something that you haven't explicitly asked but might also be important.
C / C++ arrays don't store their own size in memory. Thus, unless you want offset and values to have compile-time defined values (and, in that case, it's better to use fixed-size arrays), you might want to store the sizes of both arrays in the struct.
typedef struct tagswc {
short *offset;
char *values;
// EDIT: Changed int to size_t, thanks Chris Lutz!
size_t offset_count;
size_t values_count; // You don't need this one if values is a C string.
} swc;
DISCLAIMER: I might be wrong. For example, if all offsets of all swc instances have the same size, it would be better to store offset_count as a global member, not as a member of the struct. The same can be said about values and values_count. Also, if values is a C string, you don't need to store its size, but beware of Schlemiel the painter-like problems.
You want to use malloc to allocate the memory, and probably also sizeof() to allocate the correct amount of space.
Something like:
structVariable = (*swc) malloc(sizeof(swc));
Should do the trick.
In addition to the above, I would like to add freeing up the allocated memory as below.,
typedef struct {
short *offset;
char *values;
} swc;
swc* createStructure(int Count1, int Count2) {
swc *s1 = new swc();
s1->offset = new short[Count1];
s1->values = new char[Count2];
return s1;
}
int _tmain(int argc, _TCHAR* argv[])
{
swc *mystruct;
mystruct = createStructure(11, 11);
delete[] mystruct->offset;
delete[] mystruct->values;
delete mystruct;
return 0;
}
**If** you will not be resizing the arrays, then you can get away with a single call to malloc().
swc *new_swc (int m, int n) {
swc *p;
p = malloc (sizeof (*p) + m * sizeof (p->offset[0]) + n * sizeof (p->values[0]);
p->offset = (short *) &p[1];
p->values = (char *) &p->offset[m];
return p;
}
You can then free it with a single call to free().
(In general, there are alignment considerations to take into account, but for an array of shorts followed by an array of chars, you will be fine.)