473,322 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

array[]=malloc() ... some considerations and a question

1
I have a little problem... many solutions but maybe someone will give me a better hint.
It's about the possibility of assigning the return value of a malloc assignment to a variable of array type.
Namely I have a working function and a generic helper, where function has a very big array on it and callls helper(&bigarray).
Now, the point is that helper relies on the operator sizeof, so the verybigarray cannot be of pointer type (this way the 'size' that the compiler will tell is wrong), on the other size function cannot afford to have the whole array on the stack...
In code is something like this:

#define BIGSIZE 0xFFFFFFFFFFFFFFFFFFFF/*cking long*/

void function()
{
...
long anArray[BIGSIZE];
helper(anArray);
// This compiles, but the stack is far too big...

long aPointer* = (int*) malloc(BIGSIZE * sizeof int);
helper(*aPointer);
// This too compiles, but the sizeof in helper will give the wrong result.
// trashing all down :(
...
}

template(typename T)
void helper(T& a)
{
...
someFunction(sizeof a);
...
}

Possible solutions I thought of are:
*) Passing - from outside - a reference to the array as an argument to function... so having to change all the calls to it.

*) Switching to 'new[]'... having then to worry about the problems of mixing it with malloc.

*) Declaring helper as

template<typename T>
void helper(T& t, size_t s = sizeof t);

... but in this case what if somebody calls helper with an array passed as a pointer and forgets to add it's size?
Moreover I suspect that if I have something like n arrays of the same type but of differrent size the compiler will generate n functions (my application will run on an embedded system, so code size is really an issue).

... In less words I'd like to find a way to have an array declared inside a function but allocated on the heap, or anyway to convert the result of malloc in an array before calling 'helper', so that the latter can rely on the sizeof operator.

Any suggestion welcome.

Sergio
Apr 30 '07 #1
1 1292
JosAH
11,448 Expert 8TB
What's wrong with a vector<long>? Vectors don't grow on the stack and they're
so much easier to handle than those arrays.

kind regards,

Jos
Apr 30 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: jonnytansey2 | last post by:
Can anyone out there give me a pointer regarding creating a dynamically-generated drop-down list connected to an array? And is that question as clear as chocolate spread? Here's what I've got....
5
by: Andrew Dixon - Depictions.net | last post by:
Hi Everyone. Bit new to Java and I have question about arrays. I have wrote some code to look for certain substrings within a larger string which work fine. I would like to store each substring...
14
by: dam_fool_2003 | last post by:
Friends, cannot we malloc a array? So, I tried the following code: int main(void) { unsigned int y={1,3,6},i,j; for(i=0;i<3;i++) printf("before =%d\n",y); *y = 7; /* 1*/
2
by: Nikhil Patel | last post by:
I have a Jagged array that stores RoutingIDs and theirs corresponding Bank Names. I can access it as myArray. The column index indicates whether the value is RoutingID or a BankName. myArray -...
14
by: WStoreyII | last post by:
quick question i know that a two dimension array is like a chart basically (x columns, y rows) my question is though what would an array with more than two dimensions look like? would it...
2
by: Len via DotNetMonster.com | last post by:
I have to set up a 2d array(13, 4) containing a deck of playing cards. What I am wondering is how to correctly set up the array in the first place, and then how do you access the information once...
2
by: purna chandra | last post by:
Hello, I have a simple question.Hoping not to take much of your valuable time...:-). I am trying to get the data from a string, and am wondering if I get...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
14
by: =?Utf-8?B?RWR3YXJk?= | last post by:
Hi everybody, To me the following code shouldn't work but it does ! Imports system.String Dim x As String="This,is,a,test" Dim y(1) As String y=x.Split(",") TextBox1.text=y(3) why "Y" which is...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.