473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Array length

If I dynamically allocate memory to a pointer, then is it possible to see
afterwards what is the memory size the pointer is looking at.
e.g

int SIZE = 10;
int *a = malloc(SIZE*sizeof(int));
printf("Size : %d", sizeof(a));

this actually gives me 4 bytes, which essentially is the sizeof the
pointer. But I want to know the actual memory size. Any idea how can I do
that.
--
Jamil Anwar Zaman
Nov 13 '05 #1
7 26107

"Jamil Anwar Zaman" <sh****@spin.net.au> wrote in message
news:op**************@news.spin.net.au...
If I dynamically allocate memory to a pointer, then is it possible to see
afterwards what is the memory size the pointer is looking at.
e.g

int SIZE = 10;
int *a = malloc(SIZE*sizeof(int));
printf("Size : %d", sizeof(a));

this actually gives me 4 bytes, which essentially is the sizeof the
pointer. But I want to know the actual memory size. Any idea how can I do
that.


This is not possible in standard C. If you want the size you should store it
yourself.

One implementation I've seen has a function _msize, but this is of course
not portable and it's slow for some reason.
Nov 13 '05 #2
Jamil Anwar Zaman <sh****@spin.net.au> writes:
If I dynamically allocate memory to a pointer, then is it possible to
see afterwards what is the memory size the pointer is looking at.
No; you need to track it yourself if you need it.
e.g

int SIZE = 10;
int *a = malloc(SIZE*sizeof(int));
printf("Size : %d", sizeof(a));

this actually gives me 4 bytes, which essentially is the sizeof the
pointer.
Not "essentially." Precisely.
But I want to know the actual memory size. Any idea how can I
do that.


The normal method is to carry the size along; I generally use a
structure that holds a size and either a pointer or an array.
Nov 13 '05 #3
On Sun, 10 Aug 2003 02:43:49 +1000
Jamil Anwar Zaman <sh****@spin.net.au> wrote:
If I dynamically allocate memory to a pointer, then is it possible to see
afterwards what is the memory size the pointer is looking at.
e.g

int SIZE = 10;
int *a = malloc(SIZE*sizeof(int));
printf("Size : %d", sizeof(a));

this actually gives me 4 bytes, which essentially is the sizeof the
pointer. But I want to know the actual memory size. Any idea how can I do
that.


Yes: remember the size of the memory you allocate.

--
char*x(c,k,s)char*k,*s;{if(!k)return*s-36?x(0,0,s+1):s;if(s)if(*s)c=10+(c?(x(
c,k,0),x(c,k+=*s-c,s+1),*k):(x(*s,k,s+1),0));else c=10;printf(&x(~0,0,k)[c-~-
c+"1"[~c<-c]],c);}main(){x(0,"^[kXc6]dn_eaoh$%c","-34*1'.+(,03#;+,)/'///*");}
Nov 13 '05 #4

Jamil Anwar Zaman <sh****@spin.net.au> wrote in message
news:op**************@news.spin.net.au...
If I dynamically allocate memory to a pointer, then is it possible to see
afterwards what is the memory size the pointer is looking at.
e.g

int SIZE = 10;
int *a = malloc(SIZE*sizeof(int));
printf("Size : %d", sizeof(a));

this actually gives me 4 bytes, which essentially is the sizeof the
pointer. But I want to know the actual memory size. Any idea how can I do
that.


Yes. The size is SIZE*sizeof(int). Why ask what you
already know?

-Mike

Nov 13 '05 #5
On Sat, 9 Aug 2003 23:30:25 -0700, Mike Wahler <mk******@mkwahler.net>
wrote:

Jamil Anwar Zaman <sh****@spin.net.au> wrote in message
news:op**************@news.spin.net.au...
If I dynamically allocate memory to a pointer, then is it possible to
see
afterwards what is the memory size the pointer is looking at.
e.g

int SIZE = 10;
int *a = malloc(SIZE*sizeof(int));
printf("Size : %d", sizeof(a));

this actually gives me 4 bytes, which essentially is the sizeof the
pointer. But I want to know the actual memory size. Any idea how can I
do
that.


Yes. The size is SIZE*sizeof(int). Why ask what you
already know?

-Mike


I was trying not to pass the array size when I pass array as a parameter to
soem other function. I'm wondering how java actually does that ? Direct
memory manipulation ?

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 13 '05 #6
in comp.lang.c i read:
On Sat, 9 Aug 2003 23:30:25 -0700, Mike Wahler <mk******@mkwahler.net>
wrote:
Yes. The size is SIZE*sizeof(int). Why ask what you
already know?

I was trying not to pass the array size when I pass array as a
parameter to soem other function. I'm wondering how java actually does
that ? Direct memory manipulation ?


it passes a reference, which contains (amongst other things) the size. c
is not java, don't try to apply any of the rules you learned about java to
c or you will just frustrate yourself.

--
a signature
Nov 13 '05 #7
Jamil Anwar Zaman wrote:
Jamil Anwar Zaman <sh****@spin.net.au> wrote in message
news:op**************@news.spin.net.au...
If I dynamically allocate memory to a pointer, then is it possible to
see
afterwards what is the memory size the pointer is looking at.

[...]

I was trying not to pass the array size when I pass array as a parameter to
soem other function. I'm wondering how java actually does that ? Direct
memory manipulation ?


<off-topic>

An array in Java is a full-fledged Object. In addition
to the "contents" portion, the Java array Object also has a
`length' member that gives the number of array elements.
In short, Java passes the array size along with the array;
they're just bundled together conveniently.

</off-topic>

A way to imitate Java's "packaging" of the array contents
with the array size is to use a struct, e.g.

struct array_of_int {
size_t element_count;
int *elements;
};

It's still not quite as convenient as Java's formulation
because you've got to maintain both the count and the pointer,
and because the struct's presence is so obtrusive. Still,
this sort of "descriptor" style can be useful.

--
Er*********@sun.com
Nov 13 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
5
by: Denis Perelyubskiy | last post by:
Hello, I need to make an array of elements accross forms. My javascript skills, as evident from this question, are rather rudimentary. I tried to make an associative array and index it with...
5
by: effendi | last post by:
I wrote a simple script to remove an element of an array but I don't think this is the best way to go about it. I have a list of about five elements seperated by ";" I split the array using...
35
by: VK | last post by:
Whatever you wanted to know about it but always were affraid to ask. <http://www.geocities.com/schools_ring/ArrayAndHash.html>
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
38
by: VK | last post by:
Hello, In my object I have getDirectory() method which returns 2-dimentional array (or an imitation of 2-dimentional array using two JavaScript objects with auto-handled length property - please...
3
by: Ben | last post by:
Hi I am creating a dynamic function to return a two dimensional array from a delimeted string. The delimited string is like: field1...field2...field3... field1...field2...field3......
1
by: Samuel R. Neff | last post by:
Are there any differences between using Array.Length and Array.GetUpperBound(0) on a one-dimensional array? We have a team of developers and most people use Array.Length but one developer uses...
9
by: rkk | last post by:
Hi, I have written a generic mergesort program which is as below: --------------------------------------------------------- mergesort.h ----------------------- void MergeSort(void...
5
by: Stephen3776 | last post by:
I am doing an inventory control progam and trying to output a multiple array, I am getting an illegal conversion error java.lang.double !d. Can somebody tell me what I am doing wrong or if there is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.