473,624 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*siz eof(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 26128

"Jamil Anwar Zaman" <sh****@spin.ne t.au> wrote in message
news:op******** ******@news.spi n.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*siz eof(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.ne t.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*siz eof(int));
printf("Size : %d", sizeof(a));

this actually gives me 4 bytes, which essentially is the sizeof the
pointer.
Not "essentiall y." 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.ne t.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*siz eof(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)ch ar*k,*s;{if(!k) return*s-36?x(0,0,s+1):s ;if(s)if(*s)c=1 0+(c?(x(
c,k,0),x(c,k+=* s-c,s+1),*k):(x(* s,k,s+1),0));el se 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.ne t.au> wrote in message
news:op******** ******@news.spi n.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*siz eof(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******@mkwah ler.net>
wrote:

Jamil Anwar Zaman <sh****@spin.ne t.au> wrote in message
news:op******** ******@news.spi n.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*siz eof(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******@mkwah ler.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.ne t.au> wrote in message
news:op******** ******@news.spi n.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
3255
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)) CType(local4, Short) = CType(src, Short)
5
6515
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 the object references. However, I just realized that indices may only be referenced by strings.
5
6371
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 array.split(";") command and proceeded to update the elemment by assigning the null value to the arrayindex array=""
35
6618
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
39838
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
5183
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 let's us do not go into an "each dot over i" clarification discussion now - however you want to call - you call it ;-) array contains records of all files in the current dir. array contains records of all subs in the current dir
3
9656
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... field1...field2...field3...
1
57850
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 GetUpperBound(0). I'd like the code to all be consistent but would like to know if there is any other reason I can provide to justify using only Array.Length instead of GetUpperBound(0). I read that Array.Length has special meaning to the...
9
5608
by: rkk | last post by:
Hi, I have written a generic mergesort program which is as below: --------------------------------------------------------- mergesort.h ----------------------- void MergeSort(void *array,int p,int r,int elemSize,int(*Compare)(const void *keyA,const void *keyB));
5
2496
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 another way? /* * Main.java * * Created on April 29, 2007, 6:57 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */
0
8240
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8625
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8336
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8482
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7168
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2610
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.