11 Replies - 1416 Views - Last Post: 28 April 2010 - 03:24 AM Rate Topic: -----

#1 Guest_Arif*


Reputation:

c++ function prototype

Posted 26 April 2010 - 12:54 AM

what is difference? b\w
void fun(int * A);
and
void fun(int A[]);

both A are different
what is difference b\w their memory allocation
Is This A Good Question/Topic? 0

Replies To: c++ function prototype

#2 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: c++ function prototype

Posted 26 April 2010 - 01:20 AM

*A is pointing to the address of what is stored in A, whereas A[] is an array.

This post has been edited by IngeniousHax: 26 April 2010 - 01:21 AM

Was This Post Helpful? 0
  • +
  • -

#3 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: c++ function prototype

Posted 26 April 2010 - 04:39 AM

My bad, nevermind ...

This post has been edited by sarmanu: 26 April 2010 - 07:20 AM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_taylorc8*


Reputation:

Re: c++ function prototype

Posted 26 April 2010 - 07:14 AM

WHAT?

no..
arrays are all passed by reference.

sarmanu? what?
Was This Post Helpful? 0

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: c++ function prototype

Posted 26 April 2010 - 07:28 AM

My mistake on the above post. Indeed, arrays are automatically passed by their address. Consider the following example:
#include <iostream>

void foo(int a[])
{
    // Prints 4. That's exactly the size of a pointer on 32 bit machines
    std::cout << sizeof(a) << std::endl;
}

int main()
{
    int a[] = { 34, 12, 33, 41, 10 };
    // Prints 20
    std::cout << sizeof(a) << std::endl;
    foo(a);

    return 0;
}


This post has been edited by sarmanu: 26 April 2010 - 07:31 AM

Was This Post Helpful? 0
  • +
  • -

#6 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: c++ function prototype

Posted 26 April 2010 - 07:36 AM

Quote

what is difference b\w their memory allocation


Well, in C++, memory can be a fixed size or can be allocated dynamically. Maybe this will help a little?

	// Fixed size array
	int array[10];

	// Typical C Memory allocation and deallocation
	int *dynArray = (int*)malloc(size of memory);
	free(dynArray);

	// Typical C++ Memory allocation and deallocation
	int *dynArray = new int [size of memory];
	delete[]dynArray;

Was This Post Helpful? 0
  • +
  • -

#7 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: c++ function prototype

Posted 26 April 2010 - 07:40 AM

The difference is what you see and what to expect.


Since C and C++ treat array parameters as pointers, any structure data you had is gone (like sizeof(), it'll be 4 bytes, the size of a pointer).


The first says, "expect a pointer to a single integer", whereas the second says, "expect an array of integers of unknown size". Arrays have an implict pointer to their base (index 0), hence the many similarities.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_taylorc8*


Reputation:

Re: c++ function prototype

Posted 26 April 2010 - 08:27 AM

are there performance increase/decrease because of using

void *name

instead of void name[] ??

also, about passing multi-dimensional arrays to functions, why is it necessary to specify the size of the array dimensions if there are no bounds checking?
Was This Post Helpful? 0

#9 Guest_taylorc8*


Reputation:

Re: c++ function prototype

Posted 26 April 2010 - 08:29 AM

the reason I ask about the performance is that I'm sure humans would understand the difference between [] and * easily, but what does it mean to the machine?
Was This Post Helpful? 0

#10 Guest_taylorc8*


Reputation:

Re: c++ function prototype

Posted 26 April 2010 - 08:33 AM

Quote

Arrays have an implict pointer to their base (index 0), hence the many similarities.


what are the implications of using
void *name


instead of
void name[]
??
Was This Post Helpful? 0

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3106
  • View blog
  • Posts: 19,145
  • Joined: 14-September 07

Re: c++ function prototype

Posted 26 April 2010 - 08:33 AM

If all you do is pass and exit there's no difference. The difference would arise if you iterated through the elements passed. A single element from a pointer would be O(1) whereas iterating through the array would be O(n).
Was This Post Helpful? 0
  • +
  • -

#12 Guest_Arif*


Reputation:

Re: c++ function prototype

Posted 28 April 2010 - 03:24 AM

Thanx to all
Was This Post Helpful? 0

Page 1 of 1