0

I have a struc like this:

struct process {int PID;int myMemory[];};

however, when I try to use it

process p;
int memory[2];
p.myMemory = memory;

I get an criptic error from eclipse saying int[0] is not compatible with int[2];

what am i doing wrong?

Thanks!

7 Answers 7

4

Don't use static arrays, malloc, or even new if you're using C++. Use std::vector which will ensure correct memory management.

#include <vector>
struct Process {
    int pid;
    std::vector<int> myMemory;
};

Process p;
p.reserve(2); // allocates enough space on the heap to store 2 ints
p.myMemory.push_back( 4815 ); // add an index-zero element of 4815
p.myMemory.push_back( 162342 ); // add an index-one element of 162342

I might also suggest creating a constructor so that pid does not initially have an undefined value:

struct Process {
    Process() : pid(-1), myMemory() {
    }
    int pid;
    std::vector<int> myMemory;
};
Sign up to request clarification or add additional context in comments.

1 Comment

Who voted this down? Coming from Java, you might not know about the STL classes and how important they are. It's very important to advise those new to C++ that the standard template libraries are exceedingly useful. The only reason you should manually memory-manage a heap-based array is as an exercise for homework.
1

I think you should declare myMemory as an int* then malloc() when you know the size of it. After this it can be used like a normal array. Int[0] seems to mean "array with no dimension specified".


EXAMPLE:

int *a;      // suppose you'd like to have an array with user specified length

// get dimension (int d)

a = (int *) malloc(d * sizeof(int));

// now you can forget a is a pointer:
a[0] = 5;
a[2] = 1;

free((void *) a);     // don't forget this!

3 Comments

i get the int* but not the malloc(). can you post an example? I'm a java programmer so i'm not quite familiar....
@kralco626: look my edit. @Seth Johnson: in fact I'm a C programmer. ;)
that's good, but may I advise in the most delicate manner that people should only post answers to languages with which they're familiar? This would have been a great answer if the question were tagged as [c], but malloc/free is completely wrong for [c++], the language that is the topic of this question.
1

All these answers about vector or whatever are confused :) using a dynamically allocated pointer opens up a memory management problem, using vector opens up a performance problem as well as making the data type a non-POD and also preventing memcpy() working.

The right answer is to use

Array<int,2>

where Array is a template the C++ committee didn't bother to put in C++99 but which is in C++0x (although I'm not sure of the name). This is an inline (no memory management or performance issues) first class array which is a wrapper around a C array. I guess Boost has something already.

1 Comment

I don't think the OP knows the length of memory a priori. Otherwise he would have chosen int myMemory[2] in his struct declaration.
0

In C++, array definition is almost equal to pointer constants, meaning that their address cannot be changed, while the values which they point to can be changed. That said, you cannot copy elements of an array into another by the assignment operator. You have to go through the arrays and copy the elements one by one and check for the boundary conditions yourself.

Comments

0

The syntax ...

struct process {int PID;int myMemory[];};

... is not valid C++, but it may be accepted by some compilers as a language extension. In particular, as I recall g++ accepts it. It's in support for the C "struct hack", which is unnecessary in C++.

In C++, if you want a variable length array in a struct, use std::vector or some other array-like class, like

#include <vector>

struct Process
{
    int               pid;
    std::vector<int>  memory;
};

By the way, it's a good idea to reserve use of UPPERCASE IDENTIFIERS for macros, so as to reduce the probability of name collisions with macros, and not make people reading the code deaf (it's shouting).

Cheers & hth.,

1 Comment

I'm not the down voter, but you're confused what the struct hack is: the struct hack is a special rule that makes a C anon stuct typedef the tag name of a struct so it has external linkage.
0

You cannot make the array (defined using []) to point to another array. Because the array identifier is a const pointer. You can change the value pointed by the pointer but you cannot change the pointer itself. Think of "int array[]" as "int* const array".

The only time you can do that is during initialization.

// OK

int array[] = {1, 2, 3};

// NOT OK

int array[];

array = [1, 2, 3]; // this is no good.

Comments

0

int x[] is normally understood as int * x.

In this case, it is not, so if you want a vector of integers of an undetermined number of positions, change your declaration to:

struct process {int PID;int * myMemory;};

You should change your initialization to:

int memory[2];
p.myMemory = new int[ 10 ];

2 Comments

i get error on with line p.myMemory = new int[2] int[0] and int[2] are not compatible types.
Mmm... I see. Then change the declaration to int * myMemory instead. Obviously the compiler is assuming a vector of zero positions instead of a pointer. This is odd, but probably has to do with the dimension of the struct. I will edit my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.