3 Replies - 517 Views - Last Post: 13 June 2011 - 02:52 AM Rate Topic: -----

#1 FamiHug  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 34
  • Joined: 03-December 10

Return number of members of an "array"

Posted 12 June 2011 - 03:26 AM

#include <stdio.h>
#include <stdlib.h>

int main()
{
	int *a;
	a = (int *)malloc(sizeof(int)*3*3);
//	int a[10]; If i delete 2 lines above and  uncomment this line, it will return 10;
	int i;
	int j;
	for(j = 0; j < 3; j++)
	{
		for(i = 0; i < (sizeof(a)/sizeof(*a)); i++)
			printf("%d ",sizeof(a)/sizeof(*a));
		printf("\n");
			}
	return 0;
}



Hi all!, I'm writing a function that print out a matrix that I don't know size. But I use "dynamic array" by a = (int *)malloc....instead of the true array a[][]. When I try to get size of memory that a point to, it only return size of int. Please explain me and tell me how to do this. Thanks!

This post has been edited by FamiHug: 12 June 2011 - 03:28 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Return number of members of an "array"

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5833
  • View blog
  • Posts: 12,687
  • Joined: 16-October 07

Re: Return number of members of an "array"

Posted 12 June 2011 - 04:06 AM

The sizeof(a)==sizeof(int *), the sizeof(*a)==sizeof(int). You simply can't get the total memory size from a pointer. It's why you see size getting passed around to functions all the time.

I'd recommend using a struct to store your dimensions and go from there:
typedef struct {
   int *data;
   int rows, cols;
} Matrix;


Was This Post Helpful? 2
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Return number of members of an "array"

Posted 12 June 2011 - 03:58 PM

It is kind of unfortunate but C's memory management scheme does not offer anyway to tell the size of a block of dynamic memory. All you have is a pointer. I suppose the rational was: You had to allocate by passing in a size so you should know how big it is. And then for strings they come up with this neat little zero termination business...

Anyway, it would have been neat if you could somehow ask malloc what size was that block of memory but alas you can't (without hacking your compiler's C runtime). So... you have to do bookkeeping and keep track of the size and pass it around (see baavgai's suggestion above) OR you have to come up with some scheme for marking the end (like c-string's zero termination).

For your case the best bet is probably to work on bookkeeping. But sometimes the other way has its benefits!
Was This Post Helpful? 1
  • +
  • -

#4 FamiHug  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 34
  • Joined: 03-December 10

Re: Return number of members of an "array"

Posted 13 June 2011 - 02:52 AM

Thanks baavgal and NickDMax.
I do use struct as baavgal recommended and it works very well for my problems.

Once again, thanks you all very much for helping me many times :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1