I'm trying to write a function that takes a 32bit address and a data to store at this address.
I'm wanting to take the 32 bit memory address eg 0x12345678 and split it into 4 x 2 bytes 12, 34, 56, 78
then each of the 4 entries is at most a 256 entry array.eg FF, FF, FF, FF
So in this example, 0x12 points to 0x34 in the second array, which points to 0x56 in the third array, which finally points to 0x78 in the last array. This last array holds the actual data.
After successfully doing 0x12345678, say I might get a read for 0x1234AABB. So, the first and second pointers already exist, but I then have to create and write to dynamically created arrays.
The arrays need to have all entries set to NULL so that i know whether to follow the pointers to overwrite a previously entered value or create new arrays and pointers.
It all looks good and simple in the pseudo code I've written up but I'm having trouble coding it. I'm currently trying to deal with the first entry case, ie all array elements are NULL, but I'm getting confused with the pointers and creation of new arrays.
void cpu::store(unsigned int mem_add,unsigned int mem_val) { int first = (mem_address&4278190080)>>24; int second = (mem_address&16711680)>>16; int third = (mem_address&65280)>>8; int fourth= (mem_address&255);
I'm writing a program in which I have to use a matrix to represent a file in code. because it's a file, the size of the matrix is undefined, and therefore the matrix has to be dynamic. I found out that my compiler doesn't like dynamic multidimensional arrays, so I was thinking of this matrix as a dynamic (monodimensional) array of other dynamic (monodimensional) arrays. My program (and thus this example) uses unsigned chars.
I have a quick question. I need a way to represent orientation in degrees. I made a class which automatically changes negative angles into positive ones (adds 360 until >0) and makes sure they are under 360 (subtracts 360 until <360).
Now I realized I also need a class to represent angular movement, which must be in the range ]-360;360[
I don't think custom types with the unsigned keyword is a thing, but I'm just looking for a better way to do this then making two classes. Or would you just create a class which inherits from angle and overloads a few methods?
Or I could just use floats to represent rotation, because testing if an object does more than 1 turn per second isn't really required.
{ int codice; char* nome; char* cognome; int esami; printf("Leggi uno studente da tastiera e memorizzalo in una struct "); lettura_studente(s1, codice, nome, cognome, esami); return 0; }
[code]....
In conclusion I'va two problems:
1) Program crash; 2) I can't read struct studente within parameter function. What are the problems?
I see many time where static data member is used to count creations of objects -
i.e.
1. the static data member is init to 0
2. the static data member is incremented by 1, in the Class' constructor, every time an object is created
However, if you define a global object of a class,
How can you tell that the static data member is initialized BEFORE the constructor of the global object is called? (i.e. before the global object is created).
Because to my understanding, you do not know in advance the order of global objects' creation -
so the Global Object could be created BEFORE the static data member was created and initialized.
Code: Int** d = malloc( ROWS * sizeof(int*)); for (i = 0; i < ROWS; i++) d[i] = malloc(COLS * sizeof(int)); fx(d);
My question is, in a function declaration, why do I not have to specify the number of columns. How is this different than when I pass a static 2D array to a function, in which I must declare the function parameter with the number of columns.
Code: void fx(int d[][COLS]); VS. Code: void fx(int **d);
I remember in C++, when a dynamic array is allocated, the size of this array is stored right before the array in memory. Therefore compiler knows exactly how long, when this array is deleted.
Do all compilers store the size this way? Is it a safe method to get the size of a dynamic array?
Here is a example code, it works fine on Visual Studio 2012.
#include <iostream> using namespace std; class dummy { public: dummy() { cout<<"dummy created"<<endl;
I need to confirm that this problem cannot be solved without a pointer. Namely I need to read the rows and columns number from the user cin >> m, n and then use to declare an array int A[m][n];
However as m and n are not constants I am not able to do that. Is there a workaround? The following is the solution I came with BUT using a pointers which should be not the case.
// solution with using pointers as "int A[m][n]" does not work for me!!! void TwoDimensionalArrayFunc(){ int m = 0; int n = 0;
// instruct the users to enter array dimensions cout << "Please insert value for m:"; cin >> m;
arrays with dynamic sizes. That being said, I'm working with a simple code which seems to work just fine, my only concern is that once I display the 'char array', not only displays the user's inputs but some extra data, symbols and what not.
why, if to my understanding the first user's input already sets the size of the array
#include <iostream> #include <iomanip> using namespace std;
I have a 1000 bytes global array (which did not dynamic allocated).
I need to make "dynamic allocation" from this array.
For example - MyMalloc(50) ---> The program will allocate 50 bytes OF THE ARRAY'S SIZE. ------ MyFree(pointer) ---> I need to check if the pointer is in the array and free the space.
It should be managed by blocks. The array should also contain the manage variables (for me).
I am trying to figure out copy constructors for a dynamic array and I am definitely missing something. If I go into the copy constructor routine during debug, the values appear to be correct but they don't percolate up to the newly created object. I'll post a portion of the code below:
Code:
// include header files for the classes that are being used #include "stdafx.h" //
NOTE: THis reference must be added to all cpp files in Visual Studio Express 2013
#include <iostream> #include <string> #include <cstdlib> #include <map> using namespace std; const int ARRAY_SIZE_DEFAULT = 32; class vectorOfInt { public:
[code]....
The size of c is 0. Values of a were not copied to c, although they appear to do so within the copy constructor routine.
Code: public void dam_data_setup() { // fill list damgtype.Add( den1); damgtype.Add( den2); damgtype.Add( da1); damgtype.Add( da2); damgtype.Add( db1); damgtype.Add( db2);
[Code] .....
This is a genetics program and is to parse the source array and write all possible combinations to a new array. All sections but dilute work correctly. For some reason the dilute's Boolean is not testing true when it should. This is causing data corruption.
I have a little problem with one of my functions. The function purpose is to get a number (n) and create an array (size n) with pointers to strings (each string length is 20 chars) and i don't know why but during the debugging i get a <bad ptr> message and this message :
CXX0030: Error: expression cannot be evaluated
This is my function:
Code: char** getlist(int n) { int i=0; char **arr; arr=(char**)malloc(sizeof(char)*n); if (arr==NULL)
I am trying to create an dynamic array (lno) This array will store addressess of different Linked list. What exactly I want is:- Take N Number of Linked List user want to create> eg. 2 now It will create 2 linked list for which I am trying to allocate memory.
Code:
struct node{ int data; struct node *next; }
first; lno[0] Node 0's first address stored in ln[0] lno[1] Node 1's first address stored in ln[1] Here is the code in which I am facing problem with error Illegal structure Operation
I coded a program that takes some strings and lexicographically orders the strings and its substrings. I have used dynamic memory allocation technique and its working fine for all strings without consecutive same alphabets.I use a list in which a string is placed in its exact position by moving the others right.
I'm having a bit of trouble trying to delete elements in a dynamic array of objects. I tried to delete elements by shifting that particular element and the ones that follow over one element and then assigning the last one to NULL.
cin >> input; while (input != -1 || k == 7) { for(int i = 0; k < numberOfRecords; i++) { if (input == records[k].id) {
I made a resize function for my dynamic array template class that goes as follows. Note that the private member variables are T* arr, unsigned used, and unsigned cap.
template <class T> void darray<T>::resize(unsigned size) { if (size > cap) { T* temp_arr = new T[size];
[Code] ....
Whenever I use this function to increase the size of the array, it will work the first time I need to use it, but after that, Visual Studios will trigger a breakpoint at line 14, and if I continue past the breaks, I eventually get _CrtIsValidHeapPointer(pUserData) assertion failure.
I am trying to pass a dynamic array to a function which will:
- Copy the contents of the array to a temporary dynamic array - Change the array passed in to one size bigger - Copy the elements from the temp array back into the newly changed array - Insert an item into the last spot of the array
Here is my code:
#include <iostream> using namespace std ;
void make_array ( int Old [] , int & old_size , int toInsert ) ; void zero_array ( int arry [] , int arry_size ) ; void print_array ( int arry [] , int arry_size ) ;
[Code] .....
The output seems like a memory address but is just a very large number, what have I done incorrectly to cause this?
One can initialize a dynamically created array in the following way:
unsigned int * vec; // ... do something to vec double * a = (double *) malloc(4*sizeof(double)); a = (double[3]){(double[3]){0.0,10.0,20.0}[vec[0]],
[Code] ....
While there is no compilation error for the first assignment, the memory a is pointing to seems to change, surprisingly to me. This seems to solve the problem though:
memcpy(a, (double[3]){(double[3]){0.0,10.0,20.0}[vec[0]], (double[3]){1.0,2.0,3.0}[vec[1]], (double[6]){-2.0,-1.0,0.0,1.0,2.0,3.0}[vec[2]] }, 3*sizeof(double)); // NO C COMPILER ERROR
What does the first assignment do and why does it cause memory to change later in the program?
When I run the program this is the output: Amy Adams 10111 97 86 78 95
-842150451 -6.27744e+066 -6.27744e+066 -6.27744e+066 -6.27744e+066 and so on .....
Press any key to continue . . .
As you can see the program is reading the first students information and outputting that fine, but the rest of the students have bad values for output. I'm guessing it's something to do with the pointer, but I really can't figure it out, why it won't read all of the students info?