473,387 Members | 1,464 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,387 software developers and data experts.

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

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 */
}

Jan 2 '07 #1
12 2063
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.
Jan 2 '07 #2
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.

Jan 2 '07 #3
On Jan 2, 1:10 pm, dbtid <dbt...@nospam.gmail.comwrote:
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

Jan 2 '07 #4
matevzb said:
On Jan 2, 1:10 pm, dbtid <dbt...@nospam.gmail.comwrote:
>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.
Jan 2 '07 #5
On Jan 2, 3:18 pm, Richard Heathfield <r...@see.sig.invalidwrote:
matevzb said:
On Jan 2, 1:10 pm, dbtid <dbt...@nospam.gmail.comwrote:
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

Jan 2 '07 #6
matevzb said:
On Jan 2, 3:18 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>matevzb said:
On Jan 2, 1:10 pm, dbtid <dbt...@nospam.gmail.comwrote:
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.
Jan 2 '07 #7
On Jan 2, 3:39 pm, Richard Heathfield <r...@see.sig.invalidwrote:
<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

Jan 2 '07 #8
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>
Jan 2 '07 #9
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);
}
Jan 2 '07 #10
matevzb wrote:
On Jan 2, 1:10 pm, dbtid <dbt...@nospam.gmail.comwrote:
>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.
sizeof is an operator, not a function. Look up its definition (in
the standard). From N869:

6.3.2.1 Lvalues and function designators

.... snip ...

[#2] Except when it is the operand of the sizeof operator,
the unary & operator, the ++ operator, the -- operator, or
the left operand of the . operator or an assignment
operator, an lvalue that does not have array type is
converted to the value stored in the designated object (and
is no longer an lvalue). If the lvalue has qualified type,
the value has the unqualified version of the type of the
lvalue; otherwise, the value has the type of the lvalue. If
the lvalue has an incomplete type and does not have array
type, the behavior is undefined.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Jan 3 '07 #11
CBFalconer said:
matevzb wrote:
>On Jan 2, 1:10 pm, dbtid <dbt...@nospam.gmail.comwrote:
>>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.

sizeof is an operator, not a function.
matevzb didn't claim that sizeof is a function. His mention of "a function"
was related to the OP's question, not to dbtid's sizeof(a).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 3 '07 #12
Richard Heathfield wrote:
matevzb said:
>On Jan 2, 1:10 pm, dbtid <dbt...@nospam.gmail.comwrote:
>>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.
Yeah, that's the part I didn't mention: he should pass sizeof (a) (the
array) to the function as a parameter.

Jan 3 '07 #13

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

Similar topics

12
by: LongBow | last post by:
Hello all, From doing a google serach in the newsgroups I found out that a string can't be returned from a function, but using a char* I should be able to do it. I have spent most of the day...
2
by: aj902 | last post by:
Hello , I am trying to create a program where all detail, http://www.albany.edu/~csi333/projects.htm
9
by: Jim H | last post by:
In one of my functions I create a char string s, of dynamic allocated length. I want to free the memory before my function returns. Everywhere I find says free(s) is the way to do this, but I'm...
72
by: ravi | last post by:
I have a situation where i want to free the memory pointed by a pointer, only if it is not freed already. Is there a way to know whether the memory is freed or not?
23
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
43
by: Frodo Baggins | last post by:
Hi all, We are using strcpy to copy strings in our app. This gave us problems when the destination buffer is not large enough. As a workaround, we wanted to replace calls to strcpy with strncpy....
8
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2, C# for Windows application. I use DllImport so I can call up a function written in C++ as unmanaged code and compiled as a dll us vs2005. My application is able to...
36
by: gert | last post by:
Any comments why char **page doesn't reallocate #include <stdlib.h> void add(char **page,char *line,int n) { char **temp; if(temp=realloc(page,sizeof(char *)*(n+1))) {
5
by: Paul | last post by:
hi, there, I was asked such a question: how to determine the size of memory of the int pointer pointed? for example int GetTheSizeofMemory(int *buffer) { int size; //enter your code here,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.