PhoenixPerson
PhoenixPerson

Reputation: 312

How to dynamically allocate memory in Visual studio 2012 Visual c++

I want to allocate memory for an int array of size "n". I have used

int *Money = new int[n];

and also

int *Money = (int*) malloc(n*sizeof(int))

But neither worked. During the program, I read from a file "Test.txt" and fill the array. But while debugging, I always see only one entry (the first) in the Money array, and its size is 1. Same is the case with other dynamically allocated arrays here.

How can I get it to work?

#include<cstdio>
#include<cstdlib>
#include<assert.h>

int *Money;
int *Earn;
int *Sorted;
int *R1;
int *R2;
bool *Visited;
int visited;

void TSort(int room, int n);

int main(){
    FILE *in=fopen("Test.txt","r");
    int n,ind=0,money,r1=0,r2=0;
    char room1[7], room2[7];
    fscanf(in,"%d",&n);
    printf("%d\n",n);
    Money = (int*) malloc((n+1)*sizeof(int));
    Sorted = (int*) malloc((n+1)*sizeof(int));
    Sorted[n] = n;
    R1 = (int*) malloc(n*sizeof(int));
    R2 = (int*) malloc(n*sizeof(int));
    Visited = (bool*) malloc((n+1)*sizeof(bool));
    Earn = (int*) malloc((n+1)*sizeof(int));
    visited=0;

/* In this for loop I read in the input from the said file */

    for(int i=0;i<n;i++){
        fscanf(in,"%d %s %s",&money, room1, room2);
        Money[i]=money;
        Earn[i] = -1;
        if(room1[0] == 'E')
            R1[i] = n;
        else{
            int j=0;
            r1=0;
            while (room1[j]){
                r1 = 10*r1 + (room1[j]-48);
                j++;
        }
            R1[i] = r1;
        }
        if(room2[0] == 'E')
            R2[i] = n;
        else{
            int j=0;
            r2=0;
            while (room2[j]){
                r2 = 10*r2 + (room2[j]-48);
                j++;
            }
            R2[i] = r2;
        }
        Visited[i]=false;
    }

/* Reading and initializations end */

    Earn[0] = Money[0];
    TSort(0,n);
    for(int i=0; i<n; i++){
        assert(Earn[Sorted[i]] != -1);
        int curr=Sorted[i];
        int r1 = R1[curr];
        int r2 = R2[curr];
        int possible = Earn[curr] + Money[r1];
        if(possible > Earn[r1]) Earn[r1] = possible;
        possible = Earn[curr] + Money[r2];
        if(possible > Earn[r2]) Earn[r2] = possible;
    }
    printf("%d\n",Earn[n]);
    getchar();
    return 0;
}

void TSort(int room, int n){
while(visited<n){
        Visited[room] = true;
        Sorted[visited] = room;
        visited++;
        if((R1[room] != n)&&(!Visited[R1[room]])) TSort(R1[room],n);
    if(((R2[room] != n))&&(!Visited[R2[room]])) TSort(R2[room],n);
    }
return;
}

Upvotes: 1

Views: 1566

Answers (1)

EvilTeach
EvilTeach

Reputation: 28837

This is normal. The variable is a pointer to the first element. The IDE doesn't really know that it is an array.

Put a watch point on the variable, and tell the IDE you want to see more by appending a number to it like this.

Money, 5

That ought to show you the first 5 elements of the allocation.

how? I have to do this from memory.

under the debug menu there are various windows you can open. there ought to be one or more watch windows. open one of those.

go to the variable in your source code and select the variable name by double clicking it. drag it into the watch window and let it go.

then click on the variable in the watch window. edit the value, to put a comma 5 after it. press enter.

that ought to do the trick.

Upvotes: 7

Related Questions