Jump to content
Sign in to follow this  
DTPIC

Sizeof Operator

Recommended Posts

Hi,

I have a function into which Im passing a pointer to a char buffer; the sizeof() operator indicates different buffer lengths depending on whether it is used inside or outside the called function:

 

 

void rcv_RS232_msg(char *buffer); // function def for called function...

 

void myfunc(void) // calling function

{

char msgbuf[20]; // char message buffer

 

 

printf("size of buffer outside function is: %d",sizeof(msgbuf));

 

rcv_RS232_msg(msgbuf); // call the function, passing buffer in...

:

:

} // end of calling function

 

 

void rcv_RS232_msg(char *buffer) // the called function

{

 

printf("size of buffer inside function is: %d",sizeof(buffer));

 

}

 

 

The result of the first printf is 20 (correct)

The result of the second printf is 2 (?!?)

 

It appears that the length of the buffer is not reported correctly using sizeof() from inside the called function into which the buffer has been passed......

 

I dont think this behaviour is intentional - can you verify this behaviour? Is it on the fix list?

 

 

Thanks!

Share this post


Link to post
Share on other sites

Might be able to answer this one myself....(!)

 

the buffer is passed to the called function as a pointer - so Im probably being given the sizeof the <pointer>, not the buffer, inside the called function...

 

...so sizeof() is working ok after all....(!)

 

Hope this has been of some use to someone doing something similar...

 

I suppose I will need to pass the buffer length into the called function as another parameter if I really want to use it there... anybody know any other way?

Share this post


Link to post
Share on other sites
Might be able to answer this one myself....(!)

 

the buffer is passed to the called function as a pointer - so Im probably being given the sizeof the <pointer>, not the buffer, inside the called function...

 

...so sizeof() is working ok after all....(!)

 

Yes you are correct. Sizeof of a pointer will produce the value of 2 which is length of a pointer. If you want to use buffer length in the code you need either to pass length of the buffer as another function argument (as you already noted) or move buffer to global scope or use function template with buffer length as template argument like:

 

 

#include <system.h>

template <int N>
void rcv_RS232_msg(char *buffer)
{
    char size = sizeof(buffer); //result 2
    size = N;                    //result 20
}

void myfunc(void)
{
    char msgbuf[20];
    
    char size = sizeof(msgbuf); //result 20

    rcv_RS232_msg<sizeof(msgbuf)>(msgbuf);
}

void main()
{
    myfunc();
    while(1);
}

 

Regards,

Pavel

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×