ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
My app needs to call a function that will read some number of values from a text file and put them in the array. The count is not known until the function opens the file. The array must be new'ed within the function. What is the syntax that allows the new'ing of the array within the function. Below are some code fragments showing the concept. I know the syntax is wrong but cannot create a search function that covers new'ing the array in the function.
Code:
int main( ... )
{
float test_data[ ];
int test_count;
status = get_test_data( &test_data, &test_count );
for( int i = 0; i < test_count; i ++ ) { do something with the data };
... }
bool get_test_data( &test_data, &test_count )
{...
test_count = 128; // gets set from an fscanf( ... )
test_data = new float[ test_count ];
for( j = 0; j < test_count; j ++ )
fscanf( test_data_file, "%f", test_data[ j ] );
... }
Resolved: I used the format from post #9
Last edited by bkelly; 07-07-2020 at 06:44 PM.
Reason: final details
Yes, but this part is not C++ anything, just standard C code, just procedural and not Object Oriented anything.
And no, I meant new. As I recall, possibly a faulty recall, we should prefer new over malloc. Should that change? And does it make a significant difference?
Yes, but this part is not C++ anything, just standard C code, just procedural and not Object Oriented anything.
OK, so C code only.
Quote:
Originally Posted by bkelly
And no, I meant new. As I recall, possibly a faulty recall, we should prefer new over malloc. Should that change? And does it make a significant difference?
Any reference to the "new" capability, or some advisory where programmers are supposed to use new over malloc?
Just my $0.02, malloc() works. And has done so for ... can't guess how many years, I've been using it for about 30 years. Searching for references using new in C programs only found descriptions of how to use new in C++.
Sorry, but perhaps I'm still reading you in reverse or something.
OK, so C code only.Any reference to the "new" capability, or some advisory where programmers are supposed to use new over malloc?
Just my $0.02, malloc() works. And has done so for ... can't guess how many years, I've been using it for about 30 years. Searching for references using new in C programs only found descriptions of how to use new in C++.
Sorry, but perhaps I'm still reading you in reverse or something.
I did state it might be a faulty memory. Lets presume it is and go with malloc. So,..., what is the syntax to pass a pointer to a function such that the function can malloc the memory, put data in that memory, and the caller can use said data?
Here's a simple example of creating an array with new.
Code:
#include <iostream>
#include <string>
int main () {
std::string *strs = new std::string [3];
...
That does not answer the question. What is the syntax to new or malloc an array when the pointer is passed to a function and the malloc/new is performed within the function:
Code:
int main(){...
float *the_data;
int data_size;
status = my_function( &the_data, &data_size ... }
status = my_function( float &the_data, & data_size ){...
data_size = 128;
the_data = new float[ data_size ];
// or if preferred
the_data = malloc( data_size );
... }
I am working on post 9 from SoftSprocket. But, I am using a computer from work at home in an odd setup and just lost two days work due to a reboot. Starting over. But I do note that the answer looks much simpler that I was expecting. I often make things too complicated and cannot see the easy solution.
The pointer (*) is necessary for the allocated array. The reference (&) can be thought of as a constant pointer that automatically dereferences. In the example "float* &farr" is a reference to a pointer. It can't be reassigned like a pointer and in that way is different from a pointer to a pointer, "float** far", like in my earlier example.
An address doesn't exist for the float* that is passed in until it is allocated so in order for the address from the call to new to be accessible outside the function the address of the float* needs to be created - either through the extra pointer or a reference.
I hope that makes sense. There's probably a cleared way to say it but my thinking cap isn't working at the moment.
Yes, it mostly makes sense. I think you explained it well. I was thinking along the lines of **. Combining the * & gives the compiler more information, I presume. And yes, the code does work for me.
Thank you for your time and patience.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.