-2

How can I create new char[] in a function or constructor? When I need the array size will be as I got by parameter?

The code:

MyString::MyString (int length=10)
{
    char myCharArray[length];
}

The errors I get:

  • expected constant expression

  • cannot allocate an array of constant size 0

  • 'myCharArray' : unknown size

I already tried the following:

  1. Give an initial value in the function.
  2. Adding const to the int value.

Note: in this task I can't use std::string, although I wish so.

YanivGK
  • 893
  • 12
  • 18

1 Answers1

1

If you do use myCharArray out of the constructor, define it as a class field. This code does what you need. Unlike Java, there is no garbage collector in C++. You need to release the memory after usage.

MyString::MyString (int length=10)
{
   char *myCharArray;
   myCharArray=new char[length];
   .....
   delete[] myCharArray;
}

Update:

As suggested by Ulrich Eckhardt:

MyString::MyString (int length=10)
{
   char *myCharArray=new char[length];
   .....
   delete[] myCharArray;
}
Arashium
  • 325
  • 2
  • 9
  • Why did you make `myCharArray` global? Why did you not explain any of this code to the OP? Why did you not talk about later _freeing_ this resource? – Lightness Races in Orbit Jan 17 '15 at 16:44
  • 1
    I would also add "const" to input parameter. – Pavel Oganesyan Jan 17 '15 at 16:44
  • I meant define it inside the class if you want to use it later. If you do not use it out of the constructor, you can have it inside the constructor. – Arashium Jan 17 '15 at 16:45
  • You did not define it "inside the class". In the code in your answer, the way it's written, it can only be a global variable. – Lightness Races in Orbit Jan 17 '15 at 16:45
  • @PavelOganesyan I wouldn't, since it would have no meaningful effect. For any caller, constructors `MyString(int)` and `MyString(const int)` are indistinguishable. –  Jan 17 '15 at 16:57
  • Althought some people don't see my question as useful, It really helped me, so thanks for that. – YanivGK Jan 17 '15 at 16:58
  • The code as it stands is a bad example, because it declares a variable (the pointer `myCharArray`) without initializing it. – Ulrich Eckhardt Jan 17 '15 at 17:00
  • @Arashium I am not asking for a holy war here, but my point is just the same as in this thread: http://stackoverflow.com/questions/117293/use-of-const-for-function-parameters (see the most upvoted answer) I am totally agree that it is not the main point for this question. – Pavel Oganesyan Jan 17 '15 at 17:02
  • @hvd: Nonsense! When applied to the parameter-list on the function definition, it means the `int` may not be modified within that function, which is certainly a "meaningful effect". Arashium you add `const` for the same reason you ever add `const`. In general you should take the approach of "write `const` unless I _know_ I want to change the variable". – Lightness Races in Orbit Jan 17 '15 at 17:02
  • @PavelOganesyan, I did not oppose any opinion. I just asked it simply to know your opinion. I think the user meant that he tried to define the length constant as an unsuccessful attempt to escape any compiler error. – Arashium Jan 17 '15 at 17:06
  • The OP's problem is that the compiler error is misleading, because there are constants and constants in C++, but some are more constant than others. The point is that some are known at compile-time while others are not. Only compile-time constants can be used as size for an array, all runtime values must use `new[]` (didn't that change with C99 and/or C++11, btw?). Then, there is of course the distinction between an object declared constant and one to which you only have a `const` reference, but that is really going way too far for this question. – Ulrich Eckhardt Jan 17 '15 at 17:11
  • @LightnessRacesinOrbit If you want to argue that any function taking `int` that happens not to change the parameter should be defined as taking `const int`, then go ahead, but I strongly suspect you'll be in the minority. –  Jan 17 '15 at 17:11
  • @hvd: Yes I think I would be and I find that incredibly sad. `const`-as-default is self-evidently superior. – Lightness Races in Orbit Jan 17 '15 at 17:11
  • @LightnessRacesinOrbit FWIW, for local variables, I can see a clear benefit if they're potentially initialised to a constant value, but function parameters don't have the same advantage, because their value is never known at compile time (except by optimising compilers who don't need `const` for that anyway). –  Jan 17 '15 at 17:13
  • @hvd: I don't see what runtime vs compile-time has to do with it. Adding `const` is about catching (nay, _preventing_) accidental mutations to your data which may otherwise result in quiet and hard-to-find bugs. If you don't intend to modify your data, get the compiler to ensure that you don't! – Lightness Races in Orbit Jan 17 '15 at 17:14
  • @LightnessRacesinOrbit It's what Ulrich Eckhardt pointed out too. `const int i = expr;` makes `i` a compile-time constant if `expr` is a constant expression, and even if you don't end up using it as a compile-time constant, it easily enables further optimisations even by simple compilers. It's a different benefit than what you're describing. –  Jan 17 '15 at 17:17
  • @hvd: Yes, that is also a good reason to use it. I didn't feel the need to enumerate all the benefits of `const` for you. Please don't take that to mean that I am ignorant of them. – Lightness Races in Orbit Jan 17 '15 at 17:18
  • @LightnessRacesinOrbit You wrote that you didn't see what runtime vs compile time had to do with it. I responded to that. I don't think continuing this is going to be productive, so let's drop it. There's no logical inconsistencies in your point of view, it's perfectly valid, it just happens to be an uncommon point of view, and one I don't share. –  Jan 17 '15 at 17:20
  • @hvd: I don't see what runtime vs compile-time has to do with enforcing non-mutability, is what I meant. You'd started talking about it and I couldn't see how it was a response to my points! We began talking at cross-purposes at that time I think. No matter. – Lightness Races in Orbit Jan 17 '15 at 17:24