I want to insert an element into binary tree using pointer passing through functions. In my program i have used three structure which are follows :-
Code:
struct tree{ int data1; struct tree *leftptr; struct tree *rightptr; }; struct list{ struct tree **data; struct list *node;
[Code]...
Here i think problem is in allocating memory to n2 . why i do this because i want to store the address of address of left and right pointers of tree and extract that address to get the address of left and right pointers. Is my method correct ?
I'm new with working with random binary files. I have a class with a char* pointer stored inside of it, I also have a constructor that takes in a string (of any size) from the user. I then simply store this string into the char *. Once the string is stored in the char *. I reinterpret the instance, and I store the information into the random binary file. Everything works.
Question: Random files must know the size of the object that is being stored inside of it, so why when I enter strings of different sizes into the file, it appears to still be working. for example this is an example of the code:
class info { private: char *phrase; public: info(string n ="unknown"){ phrase = new char[n.size()+1];
[Code] ....
My point is, lets just say for example the object ETC, was some long string, this would still work for me. My question is, I don't believe each object is the same size because I allocate memory for the char pointer in the constructor.
Should I not do this just to be safe, and just use a char array instead of a pointer? (Even tho I would have set a pre-defined size for the string) or is something happening in the back to prevent this from not working?
I am writing a program where I need to read a byte of char data and convert it into a text string of binary data that represents the hex value...
i.e. The char byte is 0x42 so I need a string that has 01000010 in it. I've written the following subroutine....
------------- My Subroutine ---------------------------------------------------------------------- void charbytetostring(char input, char *output){ int i, remainder; char BASE=0x2; int DIGITS=8; char digitsArray[3] = "01";
[Code] ....
When I submitted the byte 0x42 to the subroutine, the subroutine returned to the output variable 01000010... Life is good.
The next byte that came in was 0x91. When I submit this to the subroutine I get garbage out.
I am using a debugger and stepped through the subroutine a line at a time. When I feed it 0x42 I get what I expect for all variables at all points in the execution.
When I submit 0x91 When the line remainder = input % BASE; gets executed the remainder variable gets set to 0xFFFF (I expected 1). Also, when the next line gets executed..
input = input / BASE; I get C9 where I expected to get 48.
My question is, are there data limits on what can be used with the mod (%) operator? Or am I doing something more fundamentally incorrect?
Following is the program I wrote it basically takes 9 inputs and then save them into binary file. then print out the data stored in binary data and find inverse of it then print the inverse out. but its stuck in a loop somewhere.
Code: #include <stdio.h> int main() { int a[3][3],i,j; float determinant=0; int x; FILE *fp = fopen ("file.bin", "wb");
I'm attempting to make a cache simulator in C++. But I need to access individual bits in an integer to figure out where in my "cache" the writing actually gets done. I'm pretty new to bit shifting. Say I'm trying to access the the bits of the int 5, which are its "address". I'm simulating a direct mapped cache. I need to find its tag, the set it goes into, and which line. How do I use bit shifting to access the bits to acquire the tag, the index bits, offset bits, block number...all these pieces in order to actually find where I store it in the cache.
I'm attempting to make a cache simulator in C++. But I need to access individual bits in an integer to figure out where in my "cache" the writing actually gets done. I'm pretty new to bit shifting. Say I'm trying to access the the bits of the int 5, which are its "address". I'm simulating a direct mapped cache. I need to find its tag, the set it goes into, and which line. How do I use bit shifting to access the bits to aquire the tag, the index bits, offset bits, block number...all these pieces in order to actually find where I store it in the cache. I need to break the bits up into 3 sections: tag, set index, and block index. I think I can figure out the set and block index sizes based on the values passed in. The tag bits are just the remaining ones. And I'm hard coding values such as cache size (C) - 1024, number of physical address bits (m) - 32, block size (B) - 2, number of lines per set (E) - 1 (again, directly mapped cache). How would this look? I'll be using unsigned longs, so it can handle up to 64 bits.
If we use bitwise-shift to shift all bits to the right by 2, x is 0:
00000000000000000000000000000000
If we then do a bitwise leftshift on x by 30, do we end up with:
11000000000000000000000000000000 or 00000000000000000000000000000000
In other words, when we perform right shift which clips away the least most significant bits, and then do a left shift, is it possible for those bits to reappear?
Let's say I have an array of 10 elements. I want user to enter 9 numbers so that they fill arrays 0 to 8 (9 numbers). Now I shift the arrays +1. so array[0] is now array[1] and so on. Now I ask user to enter 10th number (fills array 0).
Here's my code(it doesn't shift arrays and doesn't ask for 10th num)
I am inserting elements from two files into 2-D arrays.Suppose I have generated this kind of code to create 2-D array:
Code:
main() { int counter; int divide=5, m1=0, l1=20, window=20; for(counter=0;counter<divide;counter++){ for(i=m1,j=0;i<l1;i++,j++){ }
[code]....
Now after generating 2D array, if I want to shift last 2 elements from windata[counter] or winquery[counter] where counter=0 to the beginning of counter 1 and subsequently last two from counter 1 to counter 2 in this fashion, how can I do that.
This can be done very easy, but I assume there is a better way to do it.
assume that I have a vector or a signal like
x=[1 1 1 1 1]
want to shift it by one unit to the right I have
x[n+1] gives
xn=[0 1 1 1 1 1]
and x[n-1] vies
xn=[1 1 1 1 0]
as you can see the length of the original vector or array does not change.
How can I solve this problem without specify the length of the output vector. The size of the output array should be the same as the input array, but I couldn't find a way to do it without adjusting the size.
I want to do simple program to capture some 5 surnames of people(employees) - using array and pointers. Well I understand a bit of arrays and I know how to loop through.
How can achieve this? This holds the key to my understanding how pointers work and can be used with char or strings.
how to correctly use pointers within relation to function parameters and main source file.
I noticed that char types, for example char myVariable[50]; which is an array, does not seem to require a pointer as if it already has one built in? as opposed to char *myVariable; which seems to need one - i assume this is because char has different ways to store memory in relation to pointers, because of there being multiple ways to store a string, and memory allocation as a part of that. - i stared C a few weeks ago and feel that it is difficult to progress without nailing down pointers. Also address operators provide confusion for me and written tutorials are not so clear because there are different ways to use these operators.
Assume the user has already put in the number of students (hence my variables numStuds, which will most likely be irrelevant to my problem).
So suppose I have this:
void inputStudentInfo(string *names, int *movies, const int numStuds) { for(int i =0; i < numStuds; i++) { cout << "Enter student name: "; getline(cin, names[i]); read_string(names[i]);
[Code] ....
Then I have my data type checking function:
//Data-Type Checking for strings string read_string(string Sname) { while(!cin.good())
[Code] ....
I am getting errors. I know the problem I think is that I am trying to data type check for a string made up of pointers* with just a string but I don't know how I am supposed to check this?
I have to make a function that i'll later be able to use for a ceasar cypher. The letters should shift a user inputted number. This is what I have so far:
char shiftChar(char c, int s) { char ch = c; int shift = s; int newC; newC = int(ch) + shift; return newC; }
The problem with this, is that it doesn't loop back to the start of the alphabet once i get past z.
I am trying to use smart pointers to sort and re-link potentially large data elements. I have defined a class in my code for smart pointers, as listed below:
The object I'm trying to sort is a singly-linked list containing class objects with personal records (i.e., names, phone numbers, etc.). The singly-linked list class has an iterator class within it, as listed below.
The following is my function within my list class for "sorting" using the smart pointers.
template <typename T> void slist<T>::sort(){ vector< sptr<node> > Ap(N); // set up smart point array for list //slist<T>::iterator iter = begin(); node *ptrtemp = head->next;
[Code] .....
I must have a bad smart pointer assignment somewhere because this code compiles, but when I run it, std::bad_alloc happens along with a core dump. Where am I leaking memory?
I am writing a client-server program in C++ using C libraries. The server listens for a connection and when one is found, it is supposed to send a binary file(an image in my case) to the client. The server is sending the binary file perfectly after opening it and reading its contents into a buffer that is dynamically allocated in the SERVER file.
The problem begins when the client file is supposed to recv() the file, I do not know how I can allocate a buffer big enough for the file to be received. I know how to use malloc() and new, I prefer malloc() for executable size customization. Assuming the file being sent is 11000 bytes(note that this is an assumption and the client can not know the file size because the file size is dynamically generated by server).
How can the client generate a dynamically allocated buffer big enough to hold 11000 bytes? OR How can the client store the data in a buffer using recv() without knowing the file size.
Below is part of the code that does the send()'ing and recv()'ing.
SERVER
FILE *img = fopen("img.jpg", "rb"); fseek(img, 0, SEEK_END); unsigned long filesize = ftell(img); char *buffer = (char*)malloc(sizeof(char)*filesize); rewind(img); // store read data into buffer
[Code] ....
This outputs all the binary code perfectly but it is a bug because it can't be written to a file. Is there a way to dynamically increase the buffer until all filesize is received? Certainly, buffer needs to be dynamically allocated or program crashes ...
Also one more thing, when I telnet the server from command line, telnet displays all the output perfectly as well
Maybe telnet is storing data into a buffer behind the scenes, if so, how?
I'm not the best at C but I'm trying to write a C function that basically opens a text file with assembler language does a syntax error check on it and then converts the binary data into hex.
This is my code so far:
Code:
#include <stdio.h> #include <string.h> int main(void) { FILE*fname; char prompt; char filename[15]; char text[100]; printf( "Please enter the name of the file you wish to open: " );
I a want to write a code to convert a string into binary data for that i wrote a code its working perfectly but there is one problem , some of the binary data is written in 7bit and i want to convert it to 8 bit by adding 0 to the last.
#include <iostream> #include <fstream> #include <string> using namespace std;