LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 08-26-2008, 01:50 AM   #1
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
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.
 
Old 08-26-2008, 03:40 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
You can use "sizeof(void*)" to get the size of a generic pointer or in your case, "sizeof(char *)" is appropriate.
 
Old 08-26-2008, 05:00 AM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by jiml8 View Post
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
 
Old 08-26-2008, 06:47 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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.
 
Old 08-26-2008, 07:21 AM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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.
 
Old 08-26-2008, 10:47 AM   #6
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Original Poster
Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by dmail View Post
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.
 
Old 08-26-2008, 11:35 AM   #7
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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;
 
Old 08-26-2008, 01:31 PM   #8
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Original Poster
Rep: Reputation: 116Reputation: 116
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.
 
Old 08-26-2008, 07:33 PM   #9
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 08-27-2008, 01:03 AM   #10
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Original Poster
Rep: Reputation: 116Reputation: 116
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.
 
Old 08-27-2008, 06:52 AM   #11
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
It's often better to manually allocate and initialize global data in C++, especially when multiple threads are involved.
ta0kira
 
Old 08-27-2008, 03:02 PM   #12
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by jiml8 View Post

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.
 
Old 08-27-2008, 05:53 PM   #13
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Original Poster
Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by jlliagre View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using pointers in Eclipse 3.3.2 on SuSE 10.3 platform macdan74 Linux - Newbie 3 05-31-2008 05:04 PM
array of pointers to struct in c : how? moob8 Programming 6 11-21-2006 11:13 AM
c array of pointers greg108 Programming 10 08-17-2005 10:25 AM
array of char pointers djgerbavore Programming 2 01-08-2005 01:59 PM
a question about array pointers in C veritas Programming 12 07-18-2004 06:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration