0

I'm really not sure what it is (if it's an array of pointers, or an array of pointer arrays???), but when I step on it via the Debugger it gives me 0xCDCDCDCD, meaning the memory is allocated, but uninitialized. Can anyone show me how to initialize it?

Thanks.

char* (*vars)[4];

I've tried stuff like this, but it gives compile errors:

for (int i = 0; i < 4; i++)
 vars[i] = new char*[new char*][4]; // error C2440: 'initializing' : cannot convert from 'char **' to 'unsigned int'
Till
27.6k13 gold badges92 silver badges124 bronze badges
asked Mar 15, 2012 at 21:15
6
  • 6
    If you're not sure what it is, why are you trying to use it? Commented Mar 15, 2012 at 21:17
  • 3
    I think it would do more good to tell us what you're trying to accomplish, and let us help you do that. I'm hard put to figure out what you'd want to do, that this would be the right way to do it. Commented Mar 15, 2012 at 21:18
  • 2
    That looks extremely overcomplicated. Commented Mar 15, 2012 at 21:19
  • To be honest, I have no idea what it is. A former co-worker had it as double, and I am asked to change it to a string (or char*). So I changed it from "double (vars)[4]" to "char (*vars)[4]." Commented Mar 15, 2012 at 21:27
  • I ended up using a 2-Dimentional CString array: [CString (*vars)[4];] [vars = new (CString[100][4]);]. This was much easier to instantiate and free. Thank you all for your suggestions. Commented Mar 16, 2012 at 17:04

3 Answers 3

7

Firstly, you need to use cdecl:

declare vars as pointer to array 4 of pointer to char

How you initialise it depends on what your aim is. But, for instance:

// Create a new array
vars = new (char *[1][4]);
// Each element of the array in turn points to an array
for (int i = 0; i < 4; i++) {
 (*vars)[i] = new char[27];
}
...
// Cleanup
for (int i = 0; i < 4; i++) {
 delete [] (*vars)[i];
}
delete [] vars;

Lastly, you should never write code like this in C++. There's always a better way to do whatever it is you're trying to do (usually involving containers and smart pointers).

answered Mar 15, 2012 at 21:19
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, it all works except the first line. It says, error C2440: '=' : cannot convert from 'char *' to 'char *()[4]'
@SoftwareGuy: Ok, I've fixed it. My example was wrong, because I never use constructs like this, so I'm out of practice!
Now the constructor works, but the destructor throws a Debug Assertion Failed message when the first delete is hit: "Expression: _CrtIsValidHeapPointer(pUserData);" "Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse);" "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Due to the fact that I was not able to free the memory, I had to follow the general advice given here, which is to look for a different structure. Everything else in your code works, though, and it gave me some valuable insight on this obscure structure I had gone for. Thanks again!
2

vars is a pointer to an array of 4 pointers to char.

You can not nest index new in C++, so you have do so something like:

vars = new (char*)[4];
for(int i=0; i<4; i++) {
 (*vars)[i] = ...;
}

The inner part of the loop, the initialization of the pointers within that array, it strongly depends on the rest of your program.

But seriously, if you start writing something like this, whatever medication you're on, take either more, or less.

answered Mar 15, 2012 at 21:23

Comments

0

double (vars)[4]; just declares an array of 4 doubles. (The optional brackets are just confusing). If you want an array of 4 strings, use char *((vars)[4]); (OK, I went overboard with the brackets.) You can then initialise them to null, or allocate new strings, or point them at preallocated memory.

char *vars[4];
for (int i = 0; i < 4; i++)
 vars[i] = new char[I_HOPE_THIS_IS_BIG_ENOUGH];
answered Mar 15, 2012 at 21:58

2 Comments

Thanks for the alternative. I'm giving this a shot to see how easy it might be to adapt everything to the new structure.
This idea was really good. I ended up with a 2-D CString array, where the "I_HOPE_THIS_IS_BIG_ENOUGH" was set to 100 items. In practice we know we won't reach this... Thanks for your suggestion and help.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.