Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
08-26-2008, 01:50 AM
|
#1
|
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep: 
|
malloc array of pointers for platform independence
I have a circumstance where I need to malloc an array of pointers in C++. I wish this code to be platform independent, so I need to specify the size of the array in a platform independent fashion.
On a 32 bit x86 system, the pointer size is the word size (32 bit) but I can't guarantee this is always the case.
Can anyone tell me what type is guaranteed to match the size of a pointer on any platform?
Presently I am doing it this way:
char **osltxt;
osltxt = (char **)malloc(sizeof(int)*nentries);
but I am pretty sure that I can't use sizeof(int) as my assurance of the pointer size going forward. I thought of using sizeof(char *) but I don't know if that is valid or will work across all platforms.
|
|
|
|
08-26-2008, 03:40 AM
|
#2
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,325
|
You can use "sizeof(void*)" to get the size of a generic pointer or in your case, "sizeof(char *)" is appropriate.
|
|
|
|
08-26-2008, 05:00 AM
|
#3
|
|
Member
Registered: Oct 2005
Posts: 970
Rep: 
|
Quote:
Originally Posted by jiml8
I have a circumstance where I need to malloc an array of pointers in C++. I wish this code to be platform independent, so I need to specify the size of the array in a platform independent fashion.
|
Well you don't really, you could use either of the following.
Code:
char ** osltxt = new char*[nentries];
std::vector<char*> osltxt(nentries,0);
If for some reason you need to use malloc then you can do as jlliagre suggested
|
|
|
|
08-26-2008, 06:47 AM
|
#4
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,012
Rep: 
|
I don't really understand the problem. You need the array to unconditionally align at the binary level on all machines, and that array needs to hold pointers to something else, such as C-strings? I'm pretty sure that's not possible unless you do something like this:
Code:
#include <stdint.h>
typedef union
{
char *data;
uint64_t alignment;
} uniform_element;
uniform_element same_everywhere[32];
Sure, that might allow alignment of the actual elements, but that seems pretty futile since the pointers might be different sizes.
ta0kira
edit:
Another option:
Code:
typedef struct { char *data; } __attribute__ ((aligned (8))) uniform_element;
Last edited by ta0kira; 08-26-2008 at 06:55 AM.
|
|
|
|
08-26-2008, 07:21 AM
|
#5
|
|
Member
Registered: Oct 2005
Posts: 970
Rep: 
|
Maybe I have misunderstood your requirement and it is as taOkira writes, yet I took the following
Quote:
|
Can anyone tell me what type is guaranteed to match the size of a pointer on any platform?
|
To mean what is the size of a pointer on any one machine it is running on, not a universal type across different architectures. As pointers on machine have no correlation to a pointer on a different machine as it has a different address space.
Last edited by dmail; 08-26-2008 at 07:23 AM.
|
|
|
|
08-26-2008, 10:47 AM
|
#6
|
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Original Poster
Rep: 
|
Quote:
Originally Posted by dmail
Well you don't really, you could use either of the following.
Code:
char ** osltxt = new char*[nentries];
std::vector<char*> osltxt(nentries,0);
If for some reason you need to use malloc then you can do as jlliagre suggested
|
Hmmm...
My thought processes are very C-like. Though I have done work on and supported C++ programs in the past, this is the first time I have undertaken a major project in C++. I am doing it because, though it is costing me extra effort for the initial organization (due to my lack of familiarity) I do expect it to make things go a lot faster later on.
I take it that setting my array up this way automatically provides the required platform independence on pointer size? Looks like it would because there is no explicit size specified, unlike in malloc...
OK. Thanks.
From that perspective, is there a more C++ way to handle these two subsequent lines?
osltxt[n] = (char *)malloc(instring.length());
instring.copy(osltxt[n],instring.length());
It works perfectly well, but once again malloc is in play. (I like malloc and calloc  )
Last edited by jiml8; 08-26-2008 at 10:57 AM.
|
|
|
|
08-26-2008, 11:35 AM
|
#7
|
|
Member
Registered: Oct 2005
Posts: 970
Rep: 
|
Well for a start I would do away with the array of char pointers if the reason for the char pointers was for string and instead use an std::string and a std::vector.
Code:
std::vector<std::string> osltxt(nentries);
osltxt[n] = instring;
|
|
|
|
08-26-2008, 01:31 PM
|
#8
|
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Original Poster
Rep: 
|
Actually, my plan is to make osltxt static. It is buried in a class and I don't know how big it will be until the first time the class is invoked in the program.
This is a program initialization thing; this array is populated from a text file and will persist for the life of the program. The array contains the text of all messages that are posted in requesters throughout the program.
I am actually getting into a chicken and egg problem defining this as static. I want it to be a private static array but I cannot seem to get that to work. If it is static, I want to load it once then never again even if a new instance of the class is defined (which certainly will happen). Problem is that I can only define a prototype in the class if I make it static, and I have not figured out how to test for the existence of the array before I define storage for it - by which time it is too late.
I would prefer to completely avoid global variables in this program - in fact, that is a design goal - and would prefer to have "pseudo-globals" carried around in relevant classes, but it looks like I might not be able to do that given that this thing has to be defined dynamically.
If I turn this into a global array, then the mechanism you show will work just fine. Doing it in a class where it is private and static requires me to know nentries before I do know nentries, since I obtain that from the file I read in.
*sigh*
edit: Also, for this array I am better off keeping the char * organization because it is all headed for gtk, and this organization is more compatible with gtk. Saves me having to do a conversion as I send it.
Last edited by jiml8; 08-26-2008 at 01:41 PM.
|
|
|
|
08-26-2008, 07:33 PM
|
#9
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,012
Rep: 
|
I think you misunderstand how to implement a static member. You declare it as static in the class body in the header, then in the source you define it. That allows it to be private and also have a single definition. The definition will have to be in the header instead of the source if you use a template class, obviously.
Code:
//yourclass.hpp
class yourclass
{
static int values[64];
};
Code:
//yourclass.cpp
#include "yourclass.hpp"
int yourclass::values[64];
ta0kira
|
|
|
|
08-27-2008, 01:03 AM
|
#10
|
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Original Poster
Rep: 
|
Ultimately, that is what I figured out. I had tried to declare it static by merely declaring the pointer to it as static, then allocating the actual array dynamically. This doesn't work.
Unfortunately, I do not know the size of the array at compile time so I either have to allocate too much space in order to be static while still retaining some upgrade capability, or I have to find another way.
So now the pointer is defined as a global pointer, and I allocate it and populate it at run time and just treat it like any other C pointer.
It'll do. Since all the data goes to gtk, keeping it as a C pointer and a C array actually makes a lot of sense.
|
|
|
|
08-27-2008, 06:52 AM
|
#11
|
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,012
Rep: 
|
It's often better to manually allocate and initialize global data in C++, especially when multiple threads are involved.
ta0kira
|
|
|
|
08-27-2008, 03:02 PM
|
#12
|
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,325
|
Quote:
Originally Posted by jiml8
osltxt[n] = (char *)malloc(instring.length());
instring.copy(osltxt[n],instring.length());
It works perfectly well, but once again malloc is in play. (I like malloc and calloc  )
|
Are you sure these lines work that well ?
I may be wrong as I avoid C++ as much as I can but it looks incorrect to me not to allocate one extra character for a null string terminator and not to set that character.
|
|
|
|
08-27-2008, 05:53 PM
|
#13
|
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Original Poster
Rep: 
|
Quote:
Originally Posted by jlliagre
Are you sure these lines work that well ?
I may be wrong as I avoid C++ as much as I can but it looks incorrect to me not to allocate one extra character for a null string terminator and not to set that character.
|
Good catch. I did in fact have to do that. Then I changed to code to this:
osltxt[n] = new char[instring.length()+1];
which is the c++ way.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:31 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|