473,802 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using sizeof() with a pointer : Not the normal debate....

My Google Groups search on this one showed the endless debates and I
did my best to look through them to see if my question was answered
and didn't find it. So, here it goes....

From the GG search, I have pretty much gathered and understand why
using sizeof(pointer to char[x]) returns 4 in my program. Is there a
way to get it to return the actual amount of characters?

the basic idea is that I am initializing a char [50] and passing it
into a function that accepts a char*. That function then passes it
into an API as a char* and loads it up with characters. In this case
it is 11 characters long.

As we know.... doing a sizeof() inside of the function that has the
pointer to the original char[50] returns 4. Is there a way to get it
to return the amount of characters (in this case 11)?

Code <clipped>:

//***********

memcpy(inBuffer , _dev.pDeviceTyp e, sizeof(_dev.pDe viceType));

// This returns the first 4 characters out of the 11 total.
// My work around for now....

sprintf(inBuffe r, "%s", _dev.pDeviceTyp e);

//***********
Jul 23 '05 #1
3 3672
What is _dev.pDeviceTyp e? Is it null terminated string? If yes, use
strlen() instead.
Size of pointer is 4 byte on 32 bit system, so following will copy only
4 byte.
memcpy(inBuff er, _dev.pDeviceTyp e, sizeof(_dev.pDe viceType));
That function then passes it
into an API as a char* and loads it up with characters. In this case
it is 11 characters long.

How come to know it? Does it mean API returns the No of characters? If
yes, then You may use that in memcpy() .

Jul 23 '05 #2
Charlie wrote:
My Google Groups search on this one showed the endless debates and I
did my best to look through them to see if my question was answered
and didn't find it. So, here it goes....

From the GG search, I have pretty much gathered and understand why
using sizeof(pointer to char[x]) returns 4 in my program. Is there a
way to get it to return the actual amount of characters?

the basic idea is that I am initializing a char [50] and passing it
into a function that accepts a char*. That function then passes it
into an API as a char* and loads it up with characters. In this case
it is 11 characters long.

As we know.... doing a sizeof() inside of the function that has the
pointer to the original char[50] returns 4. Is there a way to get it
to return the amount of characters (in this case 11)?
strlen() + 1 // if you initialized everything correctly

Code <clipped>:

//***********

memcpy(inBuffer , _dev.pDeviceTyp e, sizeof(_dev.pDe viceType));

// This returns the first 4 characters out of the 11 total.
// My work around for now....

sprintf(inBuffe r, "%s", _dev.pDeviceTyp e);

//***********


Jul 23 '05 #3
Charlie schreef:
My Google Groups search on this one showed the endless debates and I
did my best to look through them to see if my question was answered
and didn't find it. So, here it goes....

From the GG search, I have pretty much gathered and understand why
using sizeof(pointer to char[x]) returns 4 in my program. Is there a
way to get it to return the actual amount of characters?
No, because there is no meaning to the word "actual"
the basic idea is that I am initializing a char [50] and passing it
into a function that accepts a char*. That function then passes it
into an API as a char* and loads it up with characters. In this case
it is 11 characters long.

As we know.... doing a sizeof() inside of the function that has the
pointer to the original char[50] returns 4. Is there a way to get it
to return the amount of characters (in this case 11)?


No. C++ has no way of knowing - unless there is something special
about these bytes. e.g. the first 10 are not 0 and the 11th is 0.
In that case, we call the bytes a string (aka "C string"). You can
then use strlen.

Other byte sequences have a convention in which the first few bytes
indicate the length that follows. E.g. some Windows APIs use this
convention.

Now, since there are at least two ways to encode the sequence length
in a sequence, what should C++ do? It doesn't know which way was
used. You do, so you should write the code.

HTH,
Michiel Salters

Jul 23 '05 #4

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

Similar topics

3
3557
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't know it returns two bytes in UniCode. Please help... For ANSI: In ISO/ANSI C++ Standard, 5.3.3 § 1, it stays: "The sizeof operator yields the number of bytes in the object representation of its
12
2748
by: Casper | last post by:
How would one go about summing up the memmory custom tree structures occupies in memmory? Example: struct node { struct node *parent; unsigned int nChildCount; string folder; //string class };
138
5297
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that array(i.e)it will have the address of the first element.In the the program below am not able to increment the value stored in x,which is the address of the first element.Why am I not able to do that?Afterall 1 is also a hexadecimal number then...
9
10692
by: dati_remo | last post by:
Hi, is it possible to find the dimension of an array using a pointer? main() { int a; f(a); return; }
29
3658
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a string to a integer number. bool Convert(const string& str, int* pData); bool Convert(const string& str, int& data);
9
2923
by: Money | last post by:
If I allocate memory like this int *ptr = new int; Can I apply delete ptr; instead of delete ptr; since I am only allocating memory for 1 integer.
20
2723
by: junky_fellow | last post by:
Hi, In my previous post I asked if sizeof may be implemented as a function. Somebody pointed a link that says that sizeof may calculated as follows with a warning that it is not guaranteed to work on all implementation. size_t size_obj = (char*)(&obj + 1) - (char*)(&obj); I wanted to find out on which implementation this would fail. One of
5
2135
by: Angus | last post by:
Hello I am doing this in a worker thread: char* szPartial = new char; if (szPartial) { lstrcpy(szPartial, szBuffer); PostMessage(m_thishWnd, WM_USER+1, 0, (LPARAM)szPartial); }
14
2800
by: ManicQin | last post by:
Hi all. I'm trying to get the size of a variable in a struct by his relative postion i.e. /// #define offsetof(s,m) (size_t)&(((s *)0)->m) struct ThePimp{ char rings; char blings;
0
9699
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
10529
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10301
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
10280
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,...
1
7596
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6835
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
5492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4268
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
2
3788
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.