Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

sizeof operator not working in CCS4

Status
Not open for further replies.

scorrpeio

Full Member level 5
Full Member level 5
Joined
Dec 26, 2006
Messages
286
Helped
10
Reputation
20
Reaction score
9
Trophy points
1,298
Activity points
3,496
Hello,

I am programming MSP430 mcu in CodeComposerStudio (CCS) 4.

The ultimate aim is that
-> I am sending the pointer to array as an argument to the function.
-> I want to calculate the no of elements in that array for which I used sizeof operator

However, the instruction of sizeof is omitted by compiler.

When I try to set breakpoint at this instruction

NoOfBytes = sizeof CharData;

the IDE doesnt even allow me to set the breakpoint.

I have set the compiler optmisation at '0' i.e. lowest level, still the problem persists.

What can be the solution over this?
 

No compiler can do that, as its wrong...sizeof operator returns the size of the data type in bytes for example
Code:
char c = 'A';
sizeof(c);
the above code will return 1 byte as char data type in most system is of 1 byte or 8bits.
Code:
int i = 100;
sizeof(i);
the above code will return 4 bytes as int data type in most system is of 4 byte or 32bits.
The size may differ as its for embedded system,and different system use different memory size for their basic data types.

The point is that sizeof operator will return memory size of the data type.For a char pointer in your case it will return the memory size of the char pointer not the size of the array which its pointing to.If you want to send the size of your array to a function you have to pass it as an argument to the function.But first know this if you use sizeof operator with array it will return you the full memory size not how many element is in their for example if you have a array of int of size 10 like this
Code:
int i[10] = {0,1,2,3,4,5,6,7,8};
sizeof(i);
the sizeof operator will return 10 * 4 = 40 as each int data type is of 4 bytes,so to find the number of elements of the array you can do this
Code:
int i[10] = {0,1,2,3,4,5,6,7,8};
int num =  sizeof(i)/sizeof(int); 
OR
int num =  sizeof(i)/sizeof(i[0]);
In case of char array you dont have to do this as its 1 byte of size but if the array is for some string then remember it will have a null terminator "\n" at last so it will be also counted for example
Code:
int string[10] = "edaboard";
sizeof(string);
It will return 9 , the last char is the null terminator.

Their are also other C standard library function like strlen() to find out the length of a string i.e.. char array ,but it depends on your compiler check out its help files what C standard library function it have.

Good Luck
 

thank you for the reply.

I have tried this as well...
Code:
const char Data[] = {0x09, 0x34, 0x34};
NoOfBytes = sizeof(Data) ;

But even it didnt work.

The issue is not what sizeof returns, rather sizeof is not returning anything in my code. That instruction is skipped by compiler. It doesnt allow me set breakpoint at that instruction. :( :( :(
 

You must have done something wrong in your code or project.Make a new project and a simple program to check if sizeof operator is working or not.Like this
Code:
int main(void)
{
int NoOfBytes = 0;
const char Data[] = {0x09, 0x34, 0x34};
NoOfBytes = sizeof(Data) ;
return 0;
}

Good Luck
 

I tried it.

If the variable is in the same file, then sizeof works fine.
But, if the variable is extern, it shows syntax error...

The original variable is in X file
UINT8_T G_u8YArrow[] = {0x04,0x02,0xFF,0x02,0x04};

And it is declared and used in Y file as
extern UINT8_T G_u8YArrow[];
u8NoOfBytes = sizeof G_u8YArrow;

The error I am getting is...

incomplete type is not allowed

I already tried putting bracket as well, but the error persists
 

I think I resolved the problem.
sizeof doesnt work for extern variables. ...was the reason
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top