C++ :: Memory Addresses Of Pointers To Structs
Apr 19, 2014
I have this struct declaration to create a linked list
struct node {
string y;
node* next;
};
If I create a linked list of 3 nodes
A->B->C->NULL
A->y is "a"
B->y is "b"
C->y is "c"
Since they are consecutive in memory. How far apart are the strings in memory?
I tried doing something like
int n = &(head->next->x)-&(head->x);
The problem is that I got 6 while debugging and 2 otherwise.
View 1 Replies
ADVERTISEMENT
Aug 31, 2014
What is the difference between initializing pointers to a memory address using the address operator like int *pointer = &x; or initializing it to a value like int *pointer = x; .. in other words how does p and q point to the same memory address in:
const int ARRAY_LEN = 100;
int arr [ ARRAY_LEN ];
int *p = arr;
int *q = &arr[0];
View 4 Replies
View Related
Dec 4, 2014
Jumping into C++, question 5 page 181:
"Write a program that compares the memory addresses of two different variables on the stack and prints out the order of the variables by numerical order of their addresses.
Does my solution look correct
Code:
#include <iostream>
using namespace std;
int main() {
int one = 1;
int two = 2;
if (&one < &two)
cout << one << " " << &one << " " << two << &two << endl;
else
cout << two << " " << &two << " " << one << " " << &one << endl;
}
View 6 Replies
View Related
Sep 1, 2013
My question is this: Is it possible to determine where functions are stored at compile time, so that at run time you can pass the memory address as a pointer to the interrupt handler so that it can directly call the function at memory location 'X'?
The newest project I'm working on would require to either somehow capture these addresses or to find a work-around so that instead of passing the pointer to the interrupt handler, the software would then need to be able to be non-interruptable.
View 2 Replies
View Related
Jun 21, 2013
the question is; Write a program that prints out the memory addresses of each element in a two-dimensional array. Check to see if the values printed out make sense to you based on the way I explained it before.
Below is the code I have done. I am having problems printing the "-" sign to keep formatting with the board when the user enter in different dimensions other than [4][4].
Code:
#include <iostream>
using namespace std;
void printTable (int x, int y) {
int **p_p_twoDimension = new int* [y];
for (int i = 0; i < y; i++) {
p_p_twoDimension[i] = new int [x];}
[Code]...
View 12 Replies
View Related
Dec 14, 2014
What the second print statement prints
main() {
struct s1{
char *str;
int i;
struct s1 *ptr;
[Code] .....
I executed the code and the output was some number(for second printf) but it should print a string.
View 3 Replies
View Related
Feb 14, 2013
I have an assignment where I need to use pointers to do a few things and I am a little confused on the syntax of it all. My question is how do you use a pointer to point to an array of structs.
For example
struct info{
char firstName[15];
char lastName[15];
};
main() {
info person[4];
cout << "The third letter of the second persons first name is: "; // ?????
}
how would I define a pointer to point to the third letter of first name the second person in the "person" array.
View 2 Replies
View Related
Feb 23, 2013
I have a struct and I want to implement in with pointers and functions.
What is the corect syntax? For example:
Code:
typedef struct XYZ
{
int x;
int y;
int z;
}XYZ_t;
int func( using the XYZ_t struct)
[Code] .....
View 5 Replies
View Related
Feb 24, 2013
Background: I'm writing a convolutional encoder (and decoder, eventually) for a microprocessor (PIC24), for which I'm using structs and pointers to move from state to state. So far as I'm aware, everything I'm using in the PIC involves nothing other than ANSI C.
I have a little experience with structures, having written a linked-list program for a class a couple years back, but nothing since and never used structure arrays. I have the feeling I'm missing something basic here, which is what's so frustrating. The most confusing error (and I suspect the root of most of them) is the 'state undeclared', which I just can't figure.
The errors I'm getting are:
encoder.c:11: warning: 'struct memstate' declared inside parameter list
encoder.c:11: warning: its scope is only this definition or declaration, which is probably not what you want
encoder.c: In function 'state_init':
encoder.c:22: error: two or more data types in declaration specifiers
encoder.c:25: error: 'state' undeclared (first use in this function)
encoder.c:25: error: (Each undeclared identifier is reported only once
[Code]....
Code:
Code: //Includes
#include <stdlib.h>
//------------------------------------------------------------------------------
//Creates state machine and passes back pointer to 00 state
void state_init(struct memstate* startpoint)
{
extern struct memstate
{
char output0; //output if next input is 0
[code]...
NB: I'm aware that at the moment, this code will do nothing except spin round that do-while loop. Once it's actually compiling I'll drop in some simple button-based test code so it'll check for the correct output.
View 9 Replies
View Related
Apr 15, 2013
I am having a lot of trouble being able to get data from a file and input it into given structs and arrays of structs and then outputting the file. We are given a file that contains 96 lines and looks like this:
Arzin, Neil
2.3 6.0 5.0 6.7 7.8 5.6 8.9 7.6
Babbage, Charles
2.3 5.6 6.5 7.6 8.7 7.8 5.4 4.5
This file continues for 24 different people and then repeats with different scores (the second line).
The first number, in this case is 2.3 for both people is a difficulty rating. The next 6 numbers are scores.
We are given this data in order to set up our structs and arrays and my code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main () {
ifstream inFile;
inFile.open("C://diveData.txt);
[Code] .....
View 1 Replies
View Related
Sep 2, 2014
declare
int *p[3];
that gives an array with 3 pointers, but how 3 pointers can have a link in the memory?
View 10 Replies
View Related
Sep 30, 2013
Pointers point to an address in memory. What if I used 3 pointers: 2 to mark the first/last nodes, and the third to mark the current node being referenced? I would wrap it in a class (to make the memory management automatic, of course), but is this practical?? maybe some pseudo code will get the juices flowing:
template<class type>
class supercondensed_list{
public:
supercondensed_list();
~supercondensed_list();
[code].....
Any things I should take into consideration? I'm not exactly the most experienced with pointers, and manually managing memory, but I think it's worth trying. If this works, then my programs should, in theory, be 100% memory efficient.
View 7 Replies
View Related
Nov 16, 2013
we are currently covering double pointers and memory allocation. Currently getScrabbleWords is not working. when I compile with commented code (Main() works fine) I get a segmentation fault.
This is the purpose of getScrabbleWords:
char **getScrabbleWords(char **allWords, char letters[]):
This function takes an array of char* values (i.e. strings) representing all the words read from wordlist.txt. Each of these words is tested by callingcanWeMakeIt as a helper function, and pointers to the words that can be made are put into an array, myWords. Note, copies of the words are not made! In order to indicate the end of myWords, we terminate with a NULL pointer. Thus, if N words can be made from letters then myWords should have length N+1.
View 6 Replies
View Related
Nov 20, 2014
I get a memory access violation when I run the below code. void createNewImage(int width, int height); this function deletes any current image data and creates a new image with the specified width/height and allocates the needed number of pixels dynamically. The new image contains four blocks with different color, red, blue, green, and black.
Code:
#include <iostream>
#include "image.h"
image::image() {
pixels = NULL;
width = 0;
height = 0;
[Code] .....
View 2 Replies
View Related
May 23, 2013
Suppose I wished to reallocate memory (resize) an array of pointers. Why does the following not work?(The program runs, yet yields a faulty segmentation error message. Why?):
Code: char **ptrarr = (char**) malloc(sizeof(char*))
Code: ptrarr = (char**) realloc(ptrarr, (capacity) * sizeof(char*));
View 14 Replies
View Related
Dec 21, 2014
I need to use dynamic memory allocation and use pointers to iterate through the arrays that I have already in this program. I am lost, nothing I do works and where to use the pointers. I am just looking for a push in the right direction so I can finish this project and how I can implement pointers in my program.
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>
#include <stdio.h>
using namespace std;
[Code]...
View 1 Replies
View Related
Apr 26, 2012
My project is say that it has memory leaks. When I view it in the debugger, it shows the correct values being passed to the pointers.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
bool ConfigGetEntry( char *pcFile, char *pcNameMust, char* &pcResult ) {
char pcBuffer[ 512 ];
[Code] ....
Its saying that i have memory corrupting at..
Code:
if ( !ConfigGetEntry( pcFile, pcName, pcRet ) ) return _strdup( pcDefault );
Also is it normal to have a bad pointer then point data into it to then clear that of the bad pointer. To have valid data now in that pointer.
View 3 Replies
View Related
Feb 26, 2013
Having some frustrating issues trying to free memory from a dynamically allocated array of pointers to linked lists. I think the problem is in how I initialize the pointers to NULL. Is there a more elegant way to have the program recognize that the list is empty so it knows to create a head node for the linked list in the function 'add_end_stub_to_array'?
I ran the code through Valgrind and it says that memory is definitely lost from this array.
This is the structure definition.
Code: struct stub_edge {
int loc_id;
int anim_type;
int mkt;
struct stub_edge *next_node;
};
Here is the code snippet from main allocating and deallocating memory to the array.
Code:
struct stub_edge **stub_list = (struct stub_edge **)malloc( sizeof(struct stub_edge *) * 12);
for (i = 0; i < 12; i++)
{
stub_list[i] = (struct stub_edge *)malloc(sizeof(struct stub_edge));
stub_list[i] = NULL;
}
stub_list = add_end_stub_to_array(end_stubs, stub_list);
destroy_end_stub_array(stub_list);
Here the function for adding nodes to the lists by reading through a dynamically allocated 2D array. (The end_stubs array is ordered by month and each linked list represents events occuring within the month).
Code:
struct stub_edge **add_end_stub_to_array(int **end_stubs, struct stub_edge **list)
{
long int i = 0;
int mon = 0;
struct stub_edge *current_node1;
struct stub_edge *new_node1;
int break1 = 0;
while(i < num_edges && break1 == 0 && mon < 12)
[Code]...
Here is the function for freeing memory from the list.
Code:
void destroy_end_stub_array(struct stub_edge **list)
{
if(list != NULL)
{
int mon = 0;
struct stub_edge *current_node1;
struct stub_edge *new_node1;
for(mon = 0; mon < 12; mon++)
[Code]...
View 1 Replies
View Related
Sep 18, 2014
I was wondering if it is possible to check if two addresses, so pointers are equal.I was saving the address of an array, and later wanted to identify it by the address, so if my area has the address: int *my_array; // is equal to: 0x1e9aa3a2c ...Later when I go through a list of pointers like:
list=
0x1e9c7e060
0x1e9ba6640
0x1e9aa3a2c <== my address
0x1e9aa3a2c
I want the third one to be equal to my list, but with == it didn't work for me.
View 3 Replies
View Related
Feb 6, 2014
I want to take a starting IP on a local network, and loop through to an ending IP on a local network, pinging all the IP addresses in between. For instance, ping all IP addresses between 192.168.1.1 - 192.168.1.255 (user enters desired starting IP and ending IP in text boxes).
I have the ping functionality working, and i can make it all work with lots of messy string parsing.. but it seems sloppy to me.
I have to split the strings (start and end IP) to get the last octet, then subtract to get the range of IPs. Then loop through, adding 1 to the last octet, and converting back to a string each time.
The C# Ping class can use either a string or an IPaddress for its Send method. If I use IPAddress, I just have to convert it from the text box it originates in, but the adding 1 to the last octet in the loop is a hassle.
Anyway, I guess the only question I have is, if you had to loop through a range of IP addresses, how would YOU do it?
public Job(string ipStartIn, string ipEndIn) {
long ip1 = Convert.ToInt64(ipStartIn);
long ip2 = Convert.ToInt64(ipEndIn);
IPStart = new IPAddress(ip1);
IPEnd = new IPAddress (ip2);
this.deviceAlive = false;
[Code] ....
View 14 Replies
View Related
Feb 25, 2015
I'm taking a university course and one of our first projects dealing with C is to write a hash table (with chaining as a collision solution) that will hash loads of hexadecimal values into the table. I'm just brain storming right now but how practical is it to hash the values by converting them to decimal and working with that value in another function to organize the values? I'm thinking this might take a lot of time and memory because our code will be tested with text files that could have a few lines of hexadecimal addresses or millions of them.
View 2 Replies
View Related
Sep 10, 2013
I dont understand the pointers or the parenthesis.
#define PORTA *(unsigned char volatile *)(0x0000)
View 2 Replies
View Related
Mar 20, 2014
What this is, is a more recent assignment and my question is if my errors are directly related to passing structure addresses to functions. I've tried several syntax variations at the beginning of my loops such as this one:
while (choice != "Q" || "q")
But the loops will not run since I introduce polar to rectangular and the choice element. My last working code was rectangular to polar and all of it worked fine.
#include<iostream>
#include<cmath>
using namespace std;
//structure declarations
struct polar
{
double distance; //distance from origin
double angle; //direction from origin
[Code] ....
View 14 Replies
View Related
Sep 19, 2014
I used a heap viewer to check for memory leaks. I have many of them and its hard to find out where it is not being freed. Is their a way to use the debugger to log the addresses of the data it allocated on the heap. This way I can trace it back. Or is their any other way to fix memory leaks properly.
View 4 Replies
View Related
Dec 30, 2013
so i have two classes ( main and another one ask ) in main i have defined 3 arrays (char drivers[250] ,offences[250] , owners[250]) and also included their pointers ( char *drivers_ptr,*offences_ptr,8owners_ptr)my problem is that i need to set values (actually 1 string of 250 chars to each of these arrays BUT from the class ask without making these arrays global -as this is prohibited by my university exercise !- )
so my question is how will i manage to save data to the arrays that i have defined in main using their addresses(through the pointers of each one that i have passed to the ask class) from the ask class ?
View 4 Replies
View Related
Mar 12, 2013
I need to create a TCP/IP program using visual studios MFC that displays all client's IP addresses that are connected to the client.
The MFC application just has a list box and a button. The client can be a simple console application. When the button is clicked, the list box gets populated with all ip addresses of connected clients.
View 14 Replies
View Related