Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Need to know the size of the memory block pointed to by a char*

Reply
Thread Tools

Need to know the size of the memory block pointed to by a char*

 
 
Frodo Baggins
Guest
Posts: n/a
 
      01-02-2007
Hi
I need to know the size of the memory block pointed to by a char* in a
function receiving this pointer. Typically, this pointer points to a
string. strlen() will not do the job since sometimes the string is not
null terminated.
That is:


int foo(void)
{
/* ...... */
char buf[20];
a(buf);
/* ....... */
return 0;
}
void a(char* ptr)
{
/* Here, I need the length of the mem block pointed at by ptr */
}

 
Reply With Quote
 
 
 
 
dbtid
Guest
Posts: n/a
 
      01-02-2007
Frodo Baggins wrote:
> Hi
> I need to know the size of the memory block pointed to by a char* in a
> function receiving this pointer. Typically, this pointer points to a
> string. strlen() will not do the job since sometimes the string is not
> null terminated.
> That is:
>
>
> int foo(void)
> {
> /* ...... */
> char buf[20];
> a(buf);
> /* ....... */
> return 0;
> }
> void a(char* ptr)
> {
> /* Here, I need the length of the mem block pointed at by ptr */
> }
>


For objects declared as arrays, sizeof(a) will work just fine.

For blocks of memory obtained by using malloc(), you must keep track of
the length yourself. Using sizeof on a pointer only gives you the
number of bytes for the pointer type.
 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      01-02-2007
Frodo Baggins wrote:

(Watch out! Records suggest that this year you will have to leave
home, carry a heavy burden, suffer betrayal and loss, eat poorly,
and return to find that life is no longer satisfying. You can
avoid all this, however, if you'll just lend me your ring a moment ...)

> I need to know the size of the memory block pointed to by a char* in a
> function receiving this pointer.


Pass in the size as a parameter.

Or, use structs with both the size and the pointer, rather than
just char* values.

You can't find out just from the pointer how many items there are.

> Typically, this pointer points to a
> string. strlen() will not do the job since sometimes the string is not
> null terminated.


If it's not nul-terminated, it isn't a C string.

> That is:
>
> int foo(void)
> {
> /* ...... */
> char buf[20];
> a(buf);
> /* ....... */
> return 0;
> }
> void a(char* ptr)
> {
> /* Here, I need the length of the mem block pointed at by ptr */
> }


void a( int length, char *ptr ) ...

.... a( sizeof (buf), buf ) ...

--
Chris "hopefully not Pyecroft" Dollin
Nit-picking is best done among friends.

 
Reply With Quote
 
matevzb
Guest
Posts: n/a
 
      01-02-2007
On Jan 2, 1:10 pm, dbtid <(E-Mail Removed)> wrote:
> For objects declared as arrays, sizeof(a) will work just fine.

No, it will not. Arrays decay into pointers when passed to a function,
so once inside a function, it's too late to get the size information
(see http://c-faq.com/aryptr/aryptrparam.html for more detail). As
Chris suggested, pass the size as a parameter to the called function.
--
WYCIWYG - what you C is what you get

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-02-2007
matevzb said:

> On Jan 2, 1:10 pm, dbtid <(E-Mail Removed)> wrote:
>> For objects declared as arrays, sizeof(a) will work just fine.

>
> No, it will not.


Yes, it will. But the OP can't use sizeof because he's not actually getting
an array, merely a pointer.

> Arrays decay into pointers when passed to a function,


And the function thus does not receive an array parameter (not least because
there's no such thing), but a pointer parameter, and so sizeof will yield
not the size of the array in bytes but the size of the pointer in bytes.

> so once inside a function, it's too late to get the size information


Right.

> (see http://c-faq.com/aryptr/aryptrparam.html for more detail). As
> Chris suggested, pass the size as a parameter to the called function.


Right again.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
matevzb
Guest
Posts: n/a
 
      01-02-2007
On Jan 2, 3:18 pm, Richard Heathfield <(E-Mail Removed)> wrote:
> matevzb said:
>
> > On Jan 2, 1:10 pm, dbtid <(E-Mail Removed)> wrote:
> >> For objects declared as arrays, sizeof(a) will work just fine.

>
> > No, it will not.

> Yes, it will. But the OP can't use sizeof because he's not actually getting
> an array, merely a pointer.

Indeed, that's another way of interpreting "sizeof(a) will work just
fine". What I meant was "no, it will not work as a means of getting
information about array size" (which I presumed dbtid suggested) and
not "sizeof(a) will not work fine", which of course it always should.
--
WYCIWYG - what you C is what you get

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-02-2007
matevzb said:

> On Jan 2, 3:18 pm, Richard Heathfield <(E-Mail Removed)> wrote:
>> matevzb said:
>>
>> > On Jan 2, 1:10 pm, dbtid <(E-Mail Removed)> wrote:
>> >> For objects declared as arrays, sizeof(a) will work just fine.

>>
>> > No, it will not.

>> Yes, it will. But the OP can't use sizeof because he's not actually
>> getting an array, merely a pointer.

> Indeed, that's another way of interpreting "sizeof(a) will work just
> fine". What I meant was "no, it will not work as a means of getting
> information about array size" (which I presumed dbtid suggested) and
> not "sizeof(a) will not work fine", which of course it always should.


Well, actually you have misinterpreted me, albeit in a way that does not
give rise to any C-ontradictions so it probably doesn't matter. What does
matter (and I think we are in full agreement here) is this:

#include <stddef.h>

void foo(int *p)
{
size_t x = sizeof p; /* gives size of pointer, in bytes, probably 4 */
}

void bar(int *q, size_t nobj)
{
while(nobj--)
{
*q++ = 42;
}
}

int main(void)
{
int arr[20] = {0};
size_t y = sizeof arr; /* gives size of array, in bytes, probably 80 */
foo(arr); /* not good enough */
bar(arr, sizeof arr / sizeof arr[0]); /* good enough */
return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-02-2007
Frodo Baggins wrote:
>
> I need to know the size of the memory block pointed to by a char*
> in a function receiving this pointer. Typically, this pointer
> points to a string. strlen() will not do the job since sometimes
> the string is not null terminated.


Then it isn't a string. Besides which strlen doesn't give the size
of the storage. Just add a parameter to the function holding the
maxsize available. Don't forget to allow for the '\0' termination
char.

ex: #define MAXSZ 123
...
myfunct(char *s, size_t maxsz) {
/* myfunc code */
}
...
callingfunction(whatever) {
char mystring[2 * MAXSZ];
...
myfunction(&mystring, sizeof(mystring)-1);
...
}
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


 
Reply With Quote
 
matevzb
Guest
Posts: n/a
 
      01-02-2007
On Jan 2, 3:39 pm, Richard Heathfield <(E-Mail Removed)> wrote:
<snip>
> Well, actually you have misinterpreted me, albeit in a way that does not
> give rise to any C-ontradictions so it probably doesn't matter.

Most probably, either due to my (non-intentional) ignorance, or the
fact that I've yet to learn all the subtleties of English language. I
possibly misinterpreted dbtid's post too. And all of these are poor
excuses of course =/
> What does matter (and I think we are in full agreement here) is this:
>
> #include <stddef.h>
>
> void foo(int *p)
> {
> size_t x = sizeof p; /* gives size of pointer, in bytes, probably 4 */
> }
>
> void bar(int *q, size_t nobj)
> {
> while(nobj--)
> {
> *q++ = 42;
> }
> }
>
> int main(void)
> {
> int arr[20] = {0};
> size_t y = sizeof arr; /* gives size of array, in bytes, probably 80 */
> foo(arr); /* not good enough */
> bar(arr, sizeof arr / sizeof arr[0]); /* good enough */
> return 0;
> }

Agreed, fully.
--
WYCIWYG - what you C is what you get

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      01-02-2007
Frodo Baggins wrote:
> Hi
> I need to know the size of the memory block pointed to by a char* in a
> function receiving this pointer. Typically, this pointer points to a
> string. strlen() will not do the job since sometimes the string is not
> null terminated.


A string is alway '\0' terminated by definition. You mean 'char array'.

> That is:
>
>
> int foo(void)
> {
> /* ...... */
> char buf[20];
> a(buf);
> /* ....... */
> return 0;
> }
> void a(char* ptr)
> {
> /* Here, I need the length of the mem block pointed at by ptr */

pass the value to a() [which needs a prior declaration anyway,
the implicit declaration will cause a redefinition error in C89
and is not sufficient in C99 even for functions returning int].
A suitable signature for a() might be
void a(size_t n, char *p);

> }


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to determine the size of memory pointed by a int pointer? Paul C Programming 6 11-29-2007 09:55 PM
Size of memory Pointed-to Winsk C Programming 4 10-08-2007 09:18 AM
Size of list pointed by key, in a hash bravetanveer Perl Misc 5 03-03-2006 03:20 AM
What Happens When Native Memory Space Pointed By java.nio.ByteBuffer Is Deleted res7cxbi@verizon.net Java 5 01-02-2006 09:32 PM
How to know the memory pointed by a ptr is freed? ravi C Programming 72 09-14-2004 01:13 PM



Advertisments