Accessing struct member through unique_ptr gives segmentation fault [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
This is how I set pointer to struct. At runtime segmentation fault is thrown at second line. LoadedPDFInfo is struct in Canvas namespace
struct std::unique_ptr<Canvas::LoadedPDFInfo> pdfInfo;
pdfInfo->handle = ++currentPDFHandle;
pdfInfo->totalPageNum = FPDF_GetPageCount(doc);

First, std::unique_ptr is a class not a struct, so get rid of the struct prefix on the pdfInfo variable declaration. You were probably thinking of this instead:
std::unique_ptr<struct Canvas::LoadedPDFInfo> pdfInfo;
But even when declaring variables (or type-casting) using actual struct types, you still do not need the struct prefix. C needs that, C++ does not.
Second, your segfault is happening because you have merely declared the pdfInfo variable, but it is not actually pointing at a valid LoadedPDFInfo object, so using the -> operator is not a valid operation. Just like a regular pointer, std::unique_ptr (and std::auto_ptr, and std::shared_ptr) have to point at something in order to access that something's members. For example:
std::unique_ptr<Canvas::LoadedPDFInfo> pdfInfo(new Canvas::LoadedPDFInfo);

Related

Do friend functions add size to a C++ struct/class? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
What it looks like is happening is that memory is being used to store a function pointer inside every instance of my structs for every friend function declared inside of them. I had trouble finding info on this and needed confirmation before making major code changes.
The struct in question has about 10 million instances, so this becomes a significant amount of memory consumption.
Do friend functions add size to a C++ struct/class?
The language doesn't specify exhaustively what affect the size of a class. But in practice no, there is generally no reason why a friend function would need to affect the size of a class.
The friend functions were not adding size to the struct.
If you must know the source of my stupidity so it is not repeated, I had an array of char pointers instead of an array of chars by mistake:
char* OwnerName[20];
instead of
char OwnerName[20];

why do i keep getting vector cannot name type error in C++? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I keep getting the error "vector does not name type" from one of my classes each time I try to compile my code.
#ifndef DISK
#define DISK
#include "PageTableEntry.h"
#include <vector>
class disk{
private:
Vector <PageTableEntry*> frames;
public:
void addFrame(int Location, PageTableEntry* pte);
void removeFrame(int pteLocation);
Disk();
};
#endif
You should quote errors verbatim. I assume the error is actually more along the lines of Vector does not name type.
You have either not included the declaration for Vector in your code, doing so would provide the compiler with a type, or you have (more likely) mistakenly written Vector when it should be std::vector. Letter case and namespaces matter in C++.
Try:
std::vector<PageTableEntry*> frames;

Why is size() function of std::string[] array returning an incorrect number of parameters [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
This is a simple 2 line question that I just don't understand.
I just created an array with 3 members, and immediately afterwards, the size is reported as 6. It was working before, but all of sudden changed. I tried cleaning my project.
This is using Visual Studio 2015
std::string detectionMethods[3] = { "SQUARE", "CIRCLE", "CIRCLE-SSV" };
int k_size = detectionMethods->length();
Thanks,
Source code and debug screenshot
There's no such thing as a member function of a raw array.
The sub-expression detectionMethods refers to the raw array and decays to a pointer to the first item. The rest, ->length(), calls a member function of the first item.
One easy way to avoid these problems is to use std::vector instead of a raw array. Your book should be recommending that.
The size of the first string is been returned.
The reason is that a variable which is an array, decays to a pointer to the first element.
So calling detectionMethods->length() is the same as calling detectionMethods[0].length()

c++ return type pointer declaration [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
function:
name: make_shape
return: Shape*
parameters: const string &shape_name; const vector &data
The class is ShapeFactory. This is what I have for this function definition:
ShapeFactory::Shape* make_shape(const string &shape_name, const vector<double>&data)
I get an error that says:
"ShapeFactory.cpp:17:15: error: ‘Shape’ in ‘class ShapeFactory’ does not name a type"
I know Shape* isn't a return type, but I don't know how to declare the pointer. Any suggestions?
You've written ShapeFactory::Shape* make_shape, which the compiler thinks is the implementation of a function in ShapeFactory called Shape*. You need to have the return type before this, and the bit after the :: is the function name. So, the correct code is:
Shape* ShapeFactory::make_shape(const string
&shape_name, const vector<double>&data)

Is it dangerous to use the following in vector? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a member variable vector<Foo> m_list
In some methods, I create the auto variable a in Foo type
Foo a;
m_list.push_back(a)
Is it possible that m_list[0] becomes invalid?
Class Foo
{
vector<int> _colors;
vector<int> _flowers;
}
You seem to be worried about what will happen to m_list[0] once the local variable a goes out of scope. Don't be. push_back actually pushes a copy of a into m_list. Now; that may cause other problems if you were expecting a and m_list[0] to be the same thing, but as long as ClassA has a functioning copy constructor (and based on your edit, it does), trying to access a destroyed object won't be one of them.