C/C++ :: Initializing Pointer To NULL?
May 17, 2014
So I'm writing a small program for class, and for some reason I keep getting an error when trying to initialize head to NULL. Even threw in the namespace just to see, nothin'.
#ifndef NUMBERLIST_H
#define NUMBERLIST_H
using namespace std;
[Code].....
There's my header file. Not sure what I'm doing wrong with the constructor.
EDIT: Got it to work with nullptr, but still curious why that isn't working
View 2 Replies
ADVERTISEMENT
Feb 27, 2014
class A; //forward defined in class B.h
class B
{
private:
[Code]....
I get error message forward Declaration of class A;
What is going on here. Is there a way I could initialize all the vectors of type B to be set to NULL in the constructor?
View 6 Replies
View Related
Feb 4, 2014
I came across a piece of c++ code that seems to be initializing a class object like this:
ImapMailbox *mailbox;
Now the class looks like this:
class ImapMailbox {
public:
ImapMailbox();
ImapMailbox (const QString& mailbox);
~ImapMailbox();
// Methods
void addMessage (ImapMessage *message);
[Code]...
And the constructor looks like this:
ImapMailbox::ImapMailbox()
: d(new ImapMailboxPrivate)
{
d->unseen = d->exists = d->recent = 0;
d->readWrite = false;
d->flags = 0;
}
But this does not seem to be an array like object. So what exactly could this be pointing to?
View 2 Replies
View Related
Apr 9, 2014
In my raytracer-project I have the following function:
bool hitObjects(Ray &ray, std::vector<Object *> &objects, Vector3& normal, Material *mat) {
double tmin = K_HUGE_VALUE;
double t;
[Code]....
When I try to run this, I only get the message "Material pointer is null". So, somehow the pointer is null after the hitObjects-function is called, even though it isn't inside that function.
View 6 Replies
View Related
Aug 1, 2013
How to initialize double pointer array..?
int (**aLines)[2];
View 3 Replies
View Related
Aug 7, 2014
In jumping into C++ it says something like this: It's not necessary but when you delete a pointer it's a good idea to reset it as a null pointer. That if your code try's to dereference the pointer after being freed, your program will crash. This happens to a lot of experienced programmers.
This could corrupt users data. delete p_int;
p_int = NULL;
1. If you can deference a pointer after the memory is freed, why can't you just delete the pointer?
2. If you can do 1, how do you delete the pointer using code?
3. Every thing I've read says that free memory is handed out in a sequenced order. I don't believe that is true at all. I may be wrong. Why can't you put the data in any number of places if it will fit. Isn't the compiler smart enough to know where bytes (bits)and pieces are stored?
4. If you storing anything in free memory must use a pointer to it?
5. Can a pointer or something similar be used with stack memory?
View 13 Replies
View Related
Dec 9, 2013
in the below program for both class pointers pointing to null. Class contains normal function executing but class contains virtual function getting segmentation fault.
#include <cstdio>
using namespace std;
class A
{
[Code].....
View 1 Replies
View Related
Aug 26, 2014
Here when NonlinearSolver called the constructor,
m_gradient() constructor is called to construct m_gradient, which in turns sets a variable called m_S to NULL(0), then in the assignment operator, because the m_S is NULL, Visual Studio throws an exception saying that destination pointer is null, but it's not fatal. I don't like that to happen because I have to press ignore many many times when the program is debugged, how can I fix this bug (not mine) to avoid the null destination pointer exception?
As an intermediate layer, the function calls boost unbounded array operator = which in turns calls std::copy which results in this error
Code:
NonlinearSolver(solver_type const & solver)
: m_gradient()
, m_function()
, m_projection() {
*this = solver;
[Code] .....
View 3 Replies
View Related
Feb 14, 2013
lets say I have a pointer p_unit of type c_unit* (c_unit is an a.b.c.)
I have a function that returns a pointer to a new c_unit object:
c_unit * man_add_unit() {
c_unit * local_p_unit;
unsigned short int local_run_code;
print_man_add_menu();
local_run_code = get_a_run_code(); // this bit just gets user input
switch (local_run_code)
[code]....
I assign that to p_unit, then add it to a vector v_units:
p_unit = man_add_unit();
v_units.push_back(p_unit);
cout << "New unit added.
";
The whole program runs on a loop, and another thing the user can do is to print out data on c_unit objects pointed to by v_units. The problem is, in that function up there ^ I give the user the option to go back to main menu without creating a unit.
Since "local_p_unit" is declared but not assigned an initial value, I'm guessing the function would return a "null" pointer (which is what's hanging me up). If I just let this run with the above code, and go to print out the unit data, the program crashes.
I tried to make an if thing with p_unit == 0 but this always returns false and doesn't catch the "bad" unit that will subsequently cause a crash.
Btw, I have considered assigning a reference to a generic c_unit object to that there local_p_unit so it won't return null, then remove pointers to that object from v_units at the end of the loop.. But I know there's got to be a better way.
View 4 Replies
View Related
Mar 14, 2014
struct mystruct{
int n;
};
class mscope{
public:
std::vector<mystruct> mv;
[Code] .....
So I have a vector of structs and I want to traverse it, find a struct that matches a constraint and obtain a pointer to that struct. I made a function for this purpose which takes a number and an empty pointer that will store the reference. However, after function returns the pointer becomes null. What could be causing this?
View 2 Replies
View Related
Jan 22, 2013
getting an invalid null pointer error message after a successful build. This program is supposed to ask for firstName, lastName, age and maJor and keep doing so until the age that is input is 0 and then the program closes. Here is what I have.
#include <iostream>
#include <string>
using namespace std;
[Code]....
View 3 Replies
View Related
Apr 12, 2013
[URL] ....
Which object's draw is being called?
View 6 Replies
View Related
Jan 25, 2014
VS 2012 / Windows 7 64 bit
class
class myclass {
public:
myclass(){/*stuff here*/}
myclass(int* p) {MyPointer = p; myclass()}
[Code] ....
it works until the myclass(int* p) calls myclass()
then the MyPointer will become CCCCCCCC (NULL value)!
is there a way to stop the second constructor from resetting the pointer value to null?
View 3 Replies
View Related
Feb 18, 2014
I am looking at one of the functions of an exercise:
void escape(char * s, char * t) {
int i, j;
i = j = 0;
while ( t[i] ) {
/* Translate the special character, if we have one */
switch( t[i] ) {
[code]...
Notice the while loop evaluates the current value in t to true or false. When we hit the null terminator, does that get evaluated as 0 and hence evaluates as a falsy value so the while loop exits?
View 1 Replies
View Related
Mar 31, 2015
When you login to my site my loginservice which is done by ajax and json make a session called context.Session["Name"]. With BreakPoints it shows that everything is good and the variables are in place. However when I use Session["Name"] it comes out as null.
I will add my code at the bottem not
using System;
using System.Collections.Generic;
using System.Linq;
[Code].....
View 2 Replies
View Related
Mar 6, 2015
I am making a tictactoe program that requires me to have a 3 by 3 two dimensional array of integers, in which the constructor should initialize the empty board to all zeroes. The program complies, but it keeps outputting garbage values and i'm not entirely sure why, My print function isn't complete yet, since I want to print out the array in a tic tac toe format, but i'm more concerned with why it's printing garbage values, here is my code:
Code:
// header file
#include <iostream>
#include <array>
[Code] ....
View 7 Replies
View Related
Sep 26, 2014
I am working on a homework assignment and have most of the program working, but when I try to compile it keeps telling me to initialize the coin variables in each class. However, they are supposed to be added then removed so I don't want to set them back to zero.
Rewrite the Purse program given in Page 35 with functions to perform insert and remove operations. The function insert (int p, int n, int d, int q) will initialize pennies, nickels, dimes and quarters. The function dollars() will return the dollars. The function remove (int p, int n, int d, int q) will subtract pennies, nickels, dimes and quarters. The function display() returns a new String to print the content of the purse with remaining pennies, nickels, dimes and quarters.
Code:
usingnamespace std;
int insert_money (int *p, int *n, int *d, int *q);
int remove_money (int *p, int *n, int *d, int *q);
int dollars();
int main()
[Code] ....
View 2 Replies
View Related
Oct 7, 2014
I am having issues with trying to implement JameM's kernel. I page fault attempting to assign the return value of place_ordered_array into the kheap.The malloc function should remain between 10000000-20000000 yet I exceed this. I have a hunch it's due that I am using grub2 (he uses legacy) and it loads modules at a higher address. I tested this by debugging at alloc and found that the function cannot handle above about 20000000. Am I on track to say I must attempt to move the module or code to prevent trampling or that I should switch to legacy.
View 1 Replies
View Related
Jul 24, 2014
Here I'm trying to initialize PersonFactory::ethnicSurnames just once for the entire run of the program:
#include <iostream>
#include <string>
#include <vector>
#include <array>
enum Country {India, China, France, NumCountries}; // plus many other countries
struct School {}; struct Mall {}; struct HockeyArena {};
[Code] ....
Output:
PersonFactory::initializeEthnicNames() called
Carrying out the initialization...
PersonFactory::initializeEthnicNames() called
PersonFactory::initializeEthnicNames() called
PersonFactory::initializeEthnicNames() called
PersonFactory::initializeEthnicNames() called
numberOfTimesInitialized = 1
As you can see, even though five PersonFactory objects were constructed, the ethnicNames initialization only occurred once, as desired. However, there are some issues with my method. First of all, the use of the comma operator is ugly in my opinion. But fashion statements aside, PersonFactory::initializeEthnicNames() is still called multiple times, which is not good, even though it correctly avoids reinitializing ethnicNames after the first call. Also, I now forever get the annoying compiler warnings that the bool namesInitialized is never used, which is true, thus wasting a small bit of memory. And finally, I cannot declare ethnicNames const now, and it is supposed to be const. Any better way to accomplish what I'm trying to do?
By the way, the reason why I don't initialize ethnic names outside the class as is normally done for static data members (and that would indeed allow me to declare it const) is because it would get messed up if I later change the order of the elements in enum Country. Hence actual lines of initializations I think are needed. And I do want ethnicSurnames inside PersonFactory, because I feel it really does belong there. Also, PersonFactory is not to be Singleton, because it has data members that depend on some parameters in its constructor (e.g. geographic location).
View 5 Replies
View Related
Oct 24, 2013
I want to know how to make variables during the program. for example in a program i want to have the player be able to create their own spells or add their own weapons to the game without having to edit the source code. i use sfml 2.0
View 4 Replies
View Related
Oct 15, 2013
sth is in my mind.
fast way of initializing an object through constructor is
Fred::Fred() : x_(whatever) { }
slow way
Fred::Fred() { x_ = whatever; }
ok. i accept it is faster but why?? what is the difference compiler does.
View 6 Replies
View Related
Mar 31, 2014
I'm working on this program, and when i run it for 'p', 'P', or for a incorrect service code a error message pops up saying that "totalCost is being used without being initialized". I don't want to change it to switches, case, and breaks now because I've come too far to change it all. I have that variable right here just below.
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
int acctCode;
double nightCost;
double totalCost;
[Code] ....
View 4 Replies
View Related
Dec 20, 2013
Code:
struct foo {
char label[64];
int state;
} bar[3] = { };
Will the above code ALWAYS initialize to 0 all objects:
bar[0].label
bar[0].state
bar[1].label
bar[1].state
bar[2].label
bar[2].state
Using gcc and g++, it does in my testing But was wondering if it can be unsafe under some circumstances.
I even tried without = { } and it is still initialized to 0 for all objects!
View 5 Replies
View Related
Feb 25, 2014
How to initialize this array of structures in my code
struct CustomerAddress; {
string street;
string city;
string state;
int zip;
[Code] ....
I am going to be using a boolean variable to mark whether or not a specific field has had data entered into it. I figure the best way to do that is to initialize all the elements of the structures to 0. However, with strings and with the nested structure, I'm not sure how to do this.
View 6 Replies
View Related
Aug 21, 2013
Here is a declaration of property class
class CMyPropertyPage11 : public CPropertyPage
Here is the definition
CMyPropertyPage11::CMyPropertyPage11() : CPropertyPage(CMyPropertyPage11::IDD)
I really do not understand the role of the
CPropertyPage(CMyPropertyPage11::IDD)
But I need to derive CMyPropertyPage11 from another CPropertyPage, say CBasePropertyPage adn really need to know how to implement the CPropertyPage(CMyPropertyPage11::IDD) initialization again.
View 3 Replies
View Related
Apr 14, 2014
I'm wondering what is the "best" way to initialize a bitfield struct. I have this bitfield, defined as:
Code:
struct S
{
unsigned int a : 1;
unsigned int b : 1;
};
If I'm "using" the bitfield, I can initialize it easily when declaring it, as so:
Code:
int main()
{
S s = {0};
}
Now, the issue I'm facing is that I want to embed S inside another struct, which I'll name "outer". EG:
Code:
struct Outer
{
S s;
};
I'm wondering what the "best" way to have Outer initialize S is? I've seen a lot of people use the "union" approach:
Code:
struct Outer
{
Outer()
{
u.all = 0;
}
union
{
unsigned char all;
S s;
} u;
};
but:This adds an extra field depth (the union's u)Does bit hacking, in a way (is the bitfield as large as my field?) I'd have wanted to initialize the field in my constructor, as so:
Code:
Outer::Outer() : s({0})
However, this would appear to be a C++11 feature only.
I have, however, "observed" that by simply "empty constructing" s, eg:
Code:
Outer::Outer() : s(){} //Initialize s ?
vs
Outer::Outer(){}
Then my bitfield "appears" initialized.
View 5 Replies
View Related