Click here to Skip to main content
15,895,462 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have some problems,please help me ,thank you.....
I want to know the size of some structs by their name. But ,
I read the name from a txt and the type of the name is string.When I use the function "sizeof()" ,I have problems.If the string is "int",function sizeof gives the size of the string, not the size of int.
How can I use sizeof to know the size of a struct by their name?
Please help,thank you ....
Posted

Without additional work, you cannot do that.
I.e. you may write
C++
struct Point
{
  int x, y;
};

// ...
size_t s = sizeof(Point);


but

C++
size_t s = SizeOf("Point");

requires reflection capabilities missing in C++.
 
Share this answer
 
Comments
Guyverthree 9-Aug-11 5:22am    
this is correct my 5.
hunshimowang 9-Aug-11 5:26am    
can you give me some advise?...thank you
CPallini 9-Aug-11 5:29am    
What are you doing? Maybe there are better alternative to SizeOf("...").
hunshimowang 9-Aug-11 5:31am    
i have hundreds of struct names in a txt, i need to read them into string and then get their size....
CPallini 9-Aug-11 5:39am    
Cannot you parse the text file to generate 'compiler acceptable' operations (e.g. sizof(Point)?
Use this little example:
C++
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>

#pragma comment(lib,"user32.lib")

static int scase(const TCHAR* find,const TCHAR* list,TCHAR sep)
{
  unsigned int is,il,done;
  for(done=il=0;list[il];done++)
  {
    for(is=0;(find[is]==list[il])&&(0!=find[is]);is++,il++);
    if((0==list[il]) || (sep==list[il]))
    {
      return 0==find[is]?done:-1;
      if(list[il]) ++il;
    }
    else
    {
      for(++il;list[il]&&(sep!=list[il-1]);il++);
    }
  }
  return -1;
}

size_t sizeof_typename(const TCHAR* typ)
{
  switch(scase(typ,__TEXT("int|short|char|long|__int64|POINT|DWORD|RECT"),'|'))
  {
    case 0: return sizeof(int);
    case 1: return sizeof(short);
    case 2: return sizeof(char);
    case 3: return sizeof(long);
    case 4: return sizeof(__int64);
    case 5: return sizeof(POINT);
    case 6: return sizeof(DWORD);
    case 7: return sizeof(RECT);
  }
  return 0;
}

int FAR PASCAL _tWinMain(HINSTANCE h,HINSTANCE p,LPTSTR c,int sw)
{
  const TCHAR*  type = __TEXT("__int64");
  size_t        size = sizeof_typename(type);
  TCHAR          msg[256];

  _stprintf_s(msg,sizeof(msg)/sizeof(msg[0]),__TEXT("sizeof(%s) = %i"),type,size);
  MessageBox(0,msg,__TEXT("sizeof_typename"),32);

  return 0;
}
 
Share this answer
 
Comments
Sergey Chepurin 9-Aug-11 8:48am    
I gave 4 simply because it is not a universal solution. But i like it. And OP could use it.
mbue 9-Aug-11 9:03am    
there is no universal solution in c.
Sergey Chepurin 9-Aug-11 9:49am    
I know. And i'm really glad you provide one for the purpose.
Sergey Alexandrovich Kryukov 10-Aug-11 0:58am    
Agree, I voted, 4, too.
--SA
The only way to do this would be to read your text file, parse the structures into C/C++ source code and then compile and execute the result. You may be able to do this quickly with a text editor.
 
Share this answer
 
Comments
hunshimowang 9-Aug-11 6:45am    
Thank you very much! I got it!
Richard MacCutchan 9-Aug-11 8:56am    
You're welcome; please mark as accepted solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900