473,320 Members | 1,990 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,320 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 26102

"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...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.