[Follow up question]The complicated code ever

_V3Signals = (_v3signal **) new char[sizeof (_pscsignal **) * (_max_v3_signals + PSC_NOMPINS)];



This code look so complicated.Could you explain to me what actually the code doing?or have a more simple example for me to understand?
Last edited on
This is basically dynamic memory allocation with typecasting.

Literal meaning: dynamically allocating memory to _V3Signals, a size of an array of char, being typecasted to a pointer to pointer to _v3signal.
This size is the sizeof a pointer to pointer to _pscsignal times that of (max_v3_signal+PSC_NOMPINS).

I hope this is clear to you.

Aceix.
Break it down into parts:
1) new char[] allocates an array of characters.
2) The number of characters allocated is determined by taking the size of a pointer to a pointer sizeof (_pscsignal **) and multiplying it by (_max_v3_signals + PSC_NOMPINS)
3) The resulting array is cast to a pointer to a pointer to a _v3signal.
4) Which is then stored in _V3Signals.




I think a malloc should be used instead of new? Here is what I think it should look like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <cstdlib>
#include <cstdio>
#define PSC_NOMPINS 1000

int main()
{	
   const unsigned _max_v3_signals(5);
   //C
   _V3Signals = (_v3signal **)(char*) malloc (sizeof (**_pscsignal) * (_max_v3_signals + PSC_NOMPINS));
   //C++
   _V3Signals = (_v3signal **) new char  [sizeof (**_pscsignal) * (_max_v3_signals + PSC_NOMPINS)];
   
   return 0;
}
Last edited on
@Smac89

Any advantage of malloc(); over new?

Aceix.
Nah, I was thinking something totally different because I didn't recognize the typecasting so I thought the person who wrote it was trying to use c-style memory allocation
FOLLOW UP QUESTION:

_V3Signals = (_v3signal **) new char[sizeof (_pscsignal **) * (_max_v3_signals + PSC_NOMPINS)];


For the above line.Why cant I just make it like
_V3Signals = new _v3signal[sizeof (_pscsignal **) * (_max_v3_signals + PSC_NOMPINS)];

Does it valid?

or

_V3Signals = new _v3signal**[sizeof (_pscsignal **) * (_max_v3_signals + PSC_NOMPINS)];

Which one is valid?and why i cant use so?
Last edited on
Neither is valid.

I would expect (in C++) something more along the lines of:

_v3Signals = new _v3signal*[_max_v3signals+PSC_NOMPINS] ;, although I can't be sure if that is entirely right. The _pcsignal** in the original code code concerns me.
Last edited on
cire wrote:
The _pcsignal** in the original code code concerns me.
It is the standard method to declare array of pointers to something in C using malloc.
Next lines would probably initialize it.
Looks like somebody couldn't get rid of C habits.

Actually I would expect to use sizeof(_pscsignal *), not sizeof(_pscsignal **)
Last edited on
Let me make it simple abit:

_v3signal **_V3Signal; //this is define inside another class

when _V3Signals = (_v3signal **) new char[10];


A simple example something like for example:
Point *p1 = new Point[2]


But why cant i do ?
_V3Signals = new _v3signal[10];
Last edited on
because new x will return *x, not x nor **x, and _V3Signals have type of _v3signal**. You should create array of pointers to _v3signal (new _v3signal*[10]), like cire shown you
Last edited on
Hi MiiNiPaa

Means that _V3Signals = new _v3signal*[10];


will become valid when there is douple pointer _v3signal **_V3Signal;?



Could you provide me link to more example like this.
Last edited on
Yes:
Like you posted before (compilation error):
http://ideone.com/5MJ5Vt

Like cire and me posted:
http://ideone.com/Y6x244
_V3Signals = new _v3signal*[10]


Sorry for disturbing.Just to verify one last thing.
Is the code above mean
I got a double pointer _V3Signals in type v3signal.
So now assign dynamic memory to the double pointer that will point to array of 10space which are in type v3signal and will store only pointer value?

Please correct me if i am wrong. thanks u very much.
Last edited on
Yes. It will create array of 10 uninitializated pointers to _v3signal
Following code will probably initialize it, like:
1
2
for(unsigned i = 0; i < 10; ++i)
    _V3Signals[i] = new v3signal;
Hi MiiNiPaa


I confuse again:


What is the diff between these two?

_V3Signals = new _v3signal*[10]

_V3Signals[i] = new v3signal



I know both also assign dynamic memory.But what is the diff ?
Last edited on
I know both also assign dynamic memory.But what is the diff ?
The same as with type** x = new type*[n]; and x[i] = new type.
First line creates an array of uninitializated (invalid) pointers.
Second line takes one such pinter from array and makes it point to newly created object.
Topic archived. No new replies allowed.