Jump to content

  • Log In with Google      Sign In   
  • Create Account


Need funcion to return char[]?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
21 replies to this topic

#21 SiCrane   Moderators   -  Reputation: 9392

Like
0Likes
Like

Posted 05 February 2013 - 03:25 PM

You always have an extra operation when using a pointer to an object rather than the object itself, whether that be an int or a contiguous row of ints. It's pretty much built in to the idea of pointer versus object. Same deal when you have an array member as a part of a class. The compiler doesn't stick a pointer in the class layout and then the members of the array somewhere else, the array is embedded in the class. So if you use a pointer in place of an array, instead of needing an additional dereference on the stack pointer, you need an additional dereference based on the this pointer. Since you don't need an additional dereference in the code using a pointer like an array in C, it's very easy to forget the fact that the dereference is there.

Sponsor:

#22 King Mir   Members   -  Reputation: 1910

Like
0Likes
Like

Posted 05 February 2013 - 10:00 PM

 


 


I mentioned this in another thread, and this is a common tripping point, but an array isn't really a variable, its just a trick to look like one.  At the end of the day, an array is just syntactic sugar over a continuous block of memory.  

That's incorrect. An array is a type that's distinct from the concept of a continuous block of memory. In particular, dynamic arrays returned by new or malloc (and other functions) represent continuous blocks of memory, but are not of array type. Arrays are just a somewhat inconsistant feature of C/C++. It's not sugar.

 

But it's true that arrays behave differently from other variable types.

 

Exactly what distinction exists about arrays that does not amount to syntactic sugar?  I can use array subscript notation to navigate through newed/malloced memory, and I can use pointer arith to navigate static/stack based arrays.  As far as I'm aware, all of the inconsistencies of array notation in C\C++ are a result of the fact that, under the covers, arrays are just pointers to a block of data, however that data is allocated, and array subscript notation is just another form of dereferencing that pointer.

As SiCrane illistrated, arrays are not just poiners to dynamic memory. They are instead a block of memory inline with the other variables in the scope they are declared, be that the stack, inside a class, or as global. This is most apparent in the disharmony between 2D arrays, and what would be the dynamic equivilent, an array of pointers to dynamic arrays.

 

This:

int array1[10][10] = {{}};
 
array1[4][6]=1;

Creates a continious block of memmory typically of 400 bytes. The indexing is equivelent to somthing like (int*)(int(array1) + 10 * 4 + 6). Also, as pointed out, the base pointer itself does not live in memory.

 

This:

int **array2 = new int *[10];
for(int i=0;i<10;++i)
  array2 = new int[10];
array1[4][6]=1;

Creates an array of pointers to array. It uses a total of 440 bytes, on 32 bit processors, typically. The index calculation is trivial: *(array2+4)+6, but it does two extra memory loads.

 

(Note: I have not tested the code, and it may contain typos or errors)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS