0

I am writing a program to simulate a cache in c++ and am trying to copy addresses that are given in a file into an array. I am struggling to figure out how to copy an array into another array so that I can have an array of memory address arrays. I have read in the addresses into an array called "address" and I want my simulated cache to be an array called "L1_Cache". h is a counter that I am incrementing after I put an address into the L1_Cache. Also, cache size is going to be how many lines of addresses are available in my L1_Cache array, which will be decided by the user of the program. Below is the snippet where I am trying to put the array into the other array.

if(sizeof(L1_Cache) < cachesize)
strcpy(L1_Cache[][h], address);

they are defined as:

const char* address[10];
char* L1_Cache;

If anyone has any suggestions on how to copy one array into another array to make an array of arrays, let me know. I am not sure if anything I am doing is correct, but I am struggling to figure this out.

I want to compare new addresses that I am given to old addresses that are already in the L1_Cache array.

Dude
  • 261
  • 2
  • 5
  • 14
  • 1
    C++? Array of Arrays is no more the thing to go for. Use std::vector instead. – icbytes Mar 07 '15 at 21:49
  • 1
    Don't bother with C-style arrays. Use `std::array`. Same performance with value semantics. – Pradhan Mar 07 '15 at 21:51
  • Never use `strcpy`, but `strncpy`. Are you sure that you are using C++ and not C? – Leonard Michlmayr Mar 07 '15 at 21:52
  • Why is your L1_Cache a char*? Shouldn't it be a two dimensional array? http://stackoverflow.com/a/7949598/546375 – Alex M Mar 07 '15 at 21:52
  • 2
    "trying to copy addresses that are given in a file" - I sincerely hope you mean postal addresses. Your usage of the `sizeof` operator is *highly* questionable, as I sincerely doubt you intend that expression to compare against the size *of a pointer*. And the question is rather difficult to follow. It seems the largest issues are not knowing how to implement your solution, where in-fact the solution itself is potentially suspect as wrong to begin with. Include in your question the actual *problem* your solution is attempting to solve; not just the problem(s) with your solution. – WhozCraig Mar 07 '15 at 21:52

2 Answers2

2

Yes, it is possible to make an array of arrays.

int a[3][3]; // a is an array of integer arrays

You have

a[0]; // this refers to the first integer array
a[1]; // this refers to the second array

Is the following what you are looking for?

#include <iostream>
#include <cstring>

int main()
{
    char p[2][256];
    strncpy(p[0], "This is my first address", 256);
    strncpy(p[1], "This is my second address", 256);

    std::cout << p[0] << std::endl << p[1];

    return 0;
}
jensa
  • 2,792
  • 2
  • 21
  • 36
1

Yes. They are called multidimensional arrays.
They can have any number of dimensions.
For example:

int foo[3][3]; // initialize the 2 dimensional array of integers
foo[0][0] = 1; // change a value
foo[0][1] = 2; // change a value
foo[0][2] = 3; // change a value
foo[1][0] = 4; // change a value
foo[1][1] = 5; // change a value
foo[1][2] = 6; // change a value
foo[2][0] = 7; // change a value
foo[2][1] = 8; // change a value
foo[2][2] = 9; // change a value
for(int i=0;i<3;++i){ // display the 2d array
    for(int j=0;j<3;++j){
        cout<<foo[i][j];
    }
    cout<<endl;
}

What's happening:
pic
Values are being assigned in a chart.
Think of it like writing a value on each point of a piece of paper.

ʇolɐǝz ǝɥʇ qoq
  • 717
  • 1
  • 15
  • 30