0
// I need to download data from the (json-format) file net_f:
std::ifstream net_f("filename", std::ios::in | std::ios::binary);
// to a square int array *net of size n:
int n;
int * net;
load_net(net_f, &n, net);

// The size is initially unknown, so I want to do it in the procedure:
void load_net(std::ifstream& f, int *n, int *net)
{
    int size; // # of rows (or columns, it's square) in the array
    int net_size; // the array size in bytes
    /*
        some code here to process data from file
    */
    // Returning values:
    *n = size;
    // Only now I am able to allocate memory:
    *net = (int *)malloc(net_size);
    /*
        and do more code to set values
    */
}

Now: the compiler warns me that 'variable "net" is used before its value is set'. Indeed, it is, since I don't have enough information. It also pops-up during the runtime, and I just ignore it. How should I rework my code to make it more elegant? (BTW it has to be an array, not a vector; I'm copying it then to a CUDA device).

2
  • What is this supposed to mean: *net = (int *)malloc(net_size); given that the RHS expression is cast to int* and the LHS expression is int? Commented Feb 24, 2015 at 7:56
  • stackoverflow.com/questions/2838038/… Commented Feb 24, 2015 at 8:41

2 Answers 2

4

Since you're trying to modify net in the called function, you need to pass net by reference (since you're using C++). Also, this would be preferred for n as well:

void load_net(std::ifstream& f, int &n, int *&net)
{
    // ...

    /* Set output args */
    n = size;
    net = (int*)malloc(net_size);
}

The C way would be to pass a double pointer (and not cast the result of malloc!):

void load_net(FILE* f, int *n, int **net)
{
    // ...

    /* Set output args */
    *n = size;
    *net = malloc(net_size);
}

You seem to be writing a mix of C and C++ code. Don't do this. Pick one, and use its features as they're intended.

Sign up to request clarification or add additional context in comments.

5 Comments

OP is actually trying to modify the thing pointed at by net (and n too). And since it hasn't been initialized...
@juanchopanza That's what his code is doing, but I'm fairly confident that's not what he intends. It doesn't make sense any other way.
@juanchopanza Look at his code. He's mallocing a buffer, and trying to assign it to net, which is an int* in the calling function. Clearly he wants to assign that buffer to that pointer, he just isn't using enough indirection.
@JonathonReinhart, you get it right. I'm coding by use of ready examples, and clearly I need to think it over yet.
@JonathonReinhart, thank you for help. This was exactly what I needed. Now the code is running perfectly well. :) Credits go to you!
0

you can use double pointer in function argument and pass pointer address in function

// I need to download data from the (json-format) file net_f:
std::ifstream net_f("filename", std::ios::in | std::ios::binary);
// to a square int array *net of size n:
int n;
int *net;
load_net(net_f, &n, &net);

// The size is initially unknown, so I want to do it in the procedure:
void load_net(std::ifstream& f, int *n, int **net)
{
    int size; // # of rows (or columns, it's square) in the array
    int net_size; // the array size in bytes
    /*
        some code here to process data from file
    */
    // Returning values:
    *n = size;
    // Only now I am able to allocate memory:
    **net = (int *)malloc(net_size);
    /*
        and do more code to set values
    */
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.