473,320 Members | 2,083 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.

dynamic, static array -> sizeof()

Hi, I have just one question :
how to know the size of an array, i have un little program, i use
first static, ant i can use sizeof() to know the size,
but when i put it as paremeter in the function, size of return "1".

ex
int tab[10];
printf("%d",sizeof(tab)/sizeof(int)); // print 10

int length(int *tab)
{
return sizeof(tab)/sizeof(int); // return 1
}

dynamic case :
int *tab = malloc(sizeof(int)*10);
printf("%d",sizeof(tab)/sizeof(int)); // print 1

Why the result aren't the same ?
How can i know the size of the array, without save the size ?

Thanks a lot.
Jun 27 '08 #1
7 3093
In comp.lang.c, al************@gmail.com wrote:
Hi, I have just one question :
how to know the size of an array, i have un little program, i use
first static, ant i can use sizeof() to know the size,
but when i put it as paremeter in the function, size of return "1".

ex
int tab[10];
printf("%d",sizeof(tab)/sizeof(int)); // print 10

int length(int *tab)
{
return sizeof(tab)/sizeof(int); // return 1
tab is a pointer to an integer
So, you've determined that a pointer to an integer is at least one times the
size of an integer, and less than two times the size of an integer, on your
platform.
}

dynamic case :
int *tab = malloc(sizeof(int)*10);
printf("%d",sizeof(tab)/sizeof(int)); // print 1

Why the result aren't the same ?
Because, you cannot determine the total size of an array from a pointer to
an element in the array.

How can i know the size of the array, without save the size ?
You cannot "know the size of the array" allocated outside of the current
function "without saving the size"
>
Thanks a lot.
--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
Jun 27 '08 #2
On 18 May 2008 at 20:23, al************@gmail.com wrote:
int *tab = malloc(sizeof(int)*10);

How can i know the size of the array, without save the size ?
Your implementation probably provides a function for this. For example,
on the GNU platform you can
#include <malloc.h>
and then call malloc_usable_size() on your pointer.

I believe the MicroSoft equivalent is _msize - Jacob will be able to
confirm or correct this.

Jun 27 '08 #3
Antoninus Twink <no****@nospam.invalidwrote:
On 18 May 2008 at 20:23, al************@gmail.com wrote:
int *tab = malloc(sizeof(int)*10);

How can i know the size of the array, without save the size ?
Your implementation probably provides a function for this. For example,
on the GNU platform you can
#include <malloc.h>
and then call malloc_usable_size() on your pointer.
If the OP uses a GNU based platform and for some versions of the
GNU libc. But if it's available it doesn't tell him/her how many
elements have been allocated for the array(and it doesn't help
at all if it's not a dynamically allocated array) but only how
much memory was set aside for the array, which very well could
be more than was originallu asked for. So it's absolutely use-
less for what the OP is looking for.
I believe the MicroSoft equivalent is _msize - Jacob will be able to
confirm or correct this.
Maybe and if the OP uses a Microsoft system. Why can't you stop
posting stuff that is wrong or maybe wrong or, if you're lucky,
is correct instead of pointing the people asking not really C
related questions to the groups where the questions can be really
answered? You do not help them that way. In the right newsgroup
they will find enough people that know the correct answers and
enough people that can intervene if wrong answers are given.

Or don't you dare to answer questions in a newsgroup were you
can be sure that enough real experts are taking a look?

--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jun 27 '08 #4
al************@gmail.com wrote:
Hi, I have just one question :
how to know the size of an array, i have un little program, i use
first static, ant i can use sizeof() to know the size,
but when i put it as paremeter in the function, size of return "1".

ex
int tab[10];
printf("%d",sizeof(tab)/sizeof(int)); // print 10

int length(int *tab)
{
return sizeof(tab)/sizeof(int); // return 1
}

dynamic case :
int *tab = malloc(sizeof(int)*10);
printf("%d",sizeof(tab)/sizeof(int)); // print 1

Why the result aren't the same ?
How can i know the size of the array, without save the size ?
Save the size, even though you don't want to.

--
pete
Jun 27 '08 #5
al************@gmail.com writes:
how to know the size of an array, i have un little program, i use
first static, ant i can use sizeof() to know the size,
but when i put it as paremeter in the function, size of return "1".

ex
int tab[10];
printf("%d",sizeof(tab)/sizeof(int)); // print 10

int length(int *tab)
{
return sizeof(tab)/sizeof(int); // return 1
}

dynamic case :
int *tab = malloc(sizeof(int)*10);
printf("%d",sizeof(tab)/sizeof(int)); // print 1

Why the result aren't the same ?
How can i know the size of the array, without save the size ?
In general, you can't -- so you should save the size if you're going
to need it later.

Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 27 '08 #6
Jens Thoms Toerring said:
Antoninus Twink <no****@nospam.invalidwrote:
<snip>
>I believe the MicroSoft equivalent is _msize - Jacob will be able to
confirm or correct this.

Maybe and if the OP uses a Microsoft system. Why can't you stop
posting stuff that is wrong or maybe wrong or, if you're lucky,
is correct instead of pointing the people asking not really C
related questions to the groups where the questions can be really
answered?
Because he's a troll. Duh.
You do not help them that way.
If he were interested in helping people, he would help them - as you say -
by pointing them to where the experts hang out (which, for C, is right
here, but for other stuff it's obviously elsenet). Since he doesn't do
that, we must assume that he is not interested in helping people, but only
in attracting flame. Some people are like that.
Or don't you dare to answer questions in a newsgroup were you
can be sure that enough real experts are taking a look?
Presumably that's why he rarely if ever posts topical replies here.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #7
On 18 May 2008 at 22:52, Jens Thoms Toerring wrote:
Antoninus Twink <no****@nospam.invalidwrote:
>Your implementation probably provides a function for this. For example,
on the GNU platform you can
#include <malloc.h>
and then call malloc_usable_size() on your pointer.

If the OP uses a GNU based platform and for some versions of the
GNU libc. But if it's available it doesn't tell him/her how many
elements have been allocated for the array(and it doesn't help
at all if it's not a dynamically allocated array) but only how
much memory was set aside for the array, which very well could
be more than was originallu asked for. So it's absolutely use-
less for what the OP is looking for.
How do you know? The OP's specification was vague, and it might or might
not be what he was looking for. At least it's a starting point.

The OP said he had a pointer returned by malloc, but didn't have the
size he'd passed to malloc() to obtain it to hand. The only portable
advice is: don't forget the size you passed to malloc(). Of course
that's good advice, and it's what I usually do myself. But sometimes
there are good reasons for seeking another solution.

Here's an example. Suppose we have a function like this:
struct node *array_copy(struct node *dst, struct node *src);
where the arrays are terminated by a sentinel value (e.g.
last_node->data == NULL). We might want (in a debugging phase - allow
for the moment that a small number of programmers outside the clc Clique
actually do have to debug their code) to bounds-check writes to the dst
list. Or we might want to allow our function to grow the dst list
dynamically if necessary, and if so return the realloc()d pointer (or
NULL). In each of these cases, malloc_usable_size will be just perfect.

On the other hand, if the OP is hoping to take a function
double compute_mean(double *x, size_t nelts);
and replace it with one like
double compute_mean(double *x);
and calculate the size of the array at runtime, then yes, he'll be
disappointed (I imagine on every platform - it's not in malloc()'s
interest to record the memory asked for, only the memory actually
allocated). But he would surely realize this when he read the
documentation of the function I suggested might be useful.
>I believe the MicroSoft equivalent is _msize - Jacob will be able to
confirm or correct this.

Maybe and if the OP uses a Microsoft system. Why can't you stop
posting stuff that is wrong or maybe wrong or, if you're lucky,
is correct instead of pointing the people asking not really C
related questions to the groups where the questions can be really
answered? You do not help them that way. In the right newsgroup
they will find enough people that know the correct answers and
enough people that can intervene if wrong answers are given.
There are plenty of people here that can intervene if wrong answers are
given, but apart from Jacob every man Jack of them would rather play
their silly topicality oneupmanship games than help new posters. I made
it crystal clear that I'm not sure about the exact function of _msize,
but I'm 100% sure that using that as a search term will lead the OP to
useful information if he's using the Windows platform. Why is pointing
people to other newsgroups any better than pointing them to Google with
a useful search term?

Jun 27 '08 #8

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

Similar topics

14
by: Spare Change | last post by:
I am told that I can have a dynamic or static string array. So if I declare string dynamic; How do I add elements to dynamic and resize it ?
4
by: Scott Lyons | last post by:
Hey all, Can someone help me figure out how to pass a dynamic array into a function? Its been giving me some trouble, and my textbook of course doesnt cover the issue. Its probably something...
5
by: meyousikmann | last post by:
I am having a little trouble with dynamic memory allocation. I am trying to read a text file and put the contents into a dynamic array. I know I can use vectors to make this easier, but it has to...
2
by: raxitsheth | last post by:
I am using array in my progrm... is there any tool /tips so that i can convert my program to Dynamic Array...so that I can Add more Element to Array.... Code is around 4000 line 'C' (not C++)...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
2
by: Ghada Al-Mashaqbeh via DotNetMonster.com | last post by:
Hi all, I am facing a problem in dynamic code generation at run time, the problem occurs when the dynmaic code use global data exist within the original application. Lets say that my...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
11
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
1
by: lumumba401 | last post by:
Hello everybody, i am asking about how to define a bidimensional dynamic array as a global variable to use as incoming variable in a function Let us see , for example in a part of a programm...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
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...
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: 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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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: 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

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.