Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
I have a structure like this
typedef struct {
string aPath;
string dPath;
string tmpPath;
int cSet;
int socket;
} threadParams;
And some variables like this
string dirPath = "./Maildir/";
string authPath;
string tmpPath = "~/tmpPath/";
Im trying to initialize structure and add some data like this
threadParams *tP = (threadParams*)malloc(sizeof(threadParams));
tP->aPath = authPath;
tP->cSet = cParam;
tP->dPath = dirPath;
tP->socket = commSocket;
tP->tmpPath = tmpPath;
When I run program with this code there is SIGSEGV signal while trying to execute this: tP->aPath = authPath; when i delete these string members of structure and keep just int variable its okay, everything works properly.
Can anyone tell me why is this happening and how to fix it? Thank you
when you allocate the structure, you are only allocating the size of the threadparams struct. The underlying strings are not being properly constructed. Internally, they are pointers to unallocated memory.
As stated previously, when creating a dynamic object, use 'new' this will allocate and properly construct the object and all object elements it contains.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I'm trying to make a login/register project and I have difficulties in declaring the char* tempUsername from this code (SIGSEVG segmentation fault)
char *tempUsername, *tempPassword, *tempPasswordConfirm, *tempSecurityQuestion;
/*
no other declaration for tempUsername here
*/
std::cout<<"Enter your new username:\n";
std::cin>>tempUsername;
//process stops here
if(fileSearch(newFilename(tempUsername))) {
std::cout<<"Username already exists! Choose another username!\n";
}
else {
std::cout<<"Enter your password:\n";
std::cin>>tempPassword;
std::cout<<"Confirm your password:\n";
I'm having a hard time understanding anything about pointers, so any advice is more than helpful!
char *tempUsername
std::cin>>tempUsername;
The problem here is that your pointer is uninitialised. When you try to extract from the input stream into the uninitialised pointer, the behaviour of the program will be undefined. Don't do this.
Your goal seems to be to read a string of user input. A solution that I can recommend is to use the std::string class:
std::string tempUsername;
std::cin >> tempUsername;
No need to use a pointer to achieve this.
I can use a char* as an array of chars, is that true?
It is not true in general. If you have a char* that points to an element of an array of char, then you can use that char* as an iterator to access the elements of the array.
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I am relatively new to C++, and I am attempting to read sequenceofchars from a text file into a char array that is dynamically allocated. My code is as follows:
while (file.get(c))
{
if (c =='\n')
continue;
char *temp = new char[i++];
arrayA = new char[i++];
arrayA[i] = c;
delete [] arrayA;
arrayA = temp;
}
And the text file format is as follows:
>NameOfChars
sequenceofchars
This is obviously horribly broken, but I've struggled to figure out the exact methodology one would use to go through this. I know about the Vector class, but I am unsure about how to go about using that if that is the preferred method for reallocating arrays on the heap. Any help would be greatly appreciated. Thank you.
I think you should definitely take a look at the vector class since it would make your code a lot cleaner. Here is a small (untested) code sample of how to use it:
#include <vector>
std::vector<char> my_vector;
while (file.get(c))
{
if (c =='\n')
continue;
my_vector.push_back(c);
}
For more information please check http://www.cplusplus.com/reference/vector/vector/push_back/
A raw array isn't dynamically allocated; hence using an STL container like vector would be better.
ifstream inf;
char c;
vector<char> charVec;
while (inf >> c)
{
charVec.push_back(c);
}
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 8 years ago.
Improve this question
I wanted to create an array of a specific size using a variable, but allegedly the only way to do that is to use pointers.
int size = 5;
string* myArray = new string[size];
I now want to assign an element from this array to another variable, which I believe will only work through dereferencing. The following line of code doesn't work. How do I fix it?
string line;
myArray[0] = "Hello";
line = *myArray[0];
Edit:
I just want to clarify something: Using the normal "myArray[0]" code doesn't work either. It compiles, but causes a crash. Here's some more specific code regarding what I want to do.
void MyClass::setLine(string line)
{
myLine = line; /*This is a private variable in MyClass*/
}
///////////////
MyClass* elements = new MyClass[size];
elements[0].setLine(myArray[0]);
I want to assign the array element to a private variable from a class, but the program crashes when I try to assign the value to the private variable.
If you know the size at compile time, you can use a normal array. If you only know the size at runtime, you could still use a std::vector, which is far easier to use than manual allocation.
Anyway, if you really want to learn about pointers for array managing, keep in mind that the index operator is equivalent to addition and dereference, i.e. ar[i] is the same as *(ar + i). In other words, indexing is just dereferencing at an offset.
As such, no extra dereference is needed. Just drop the asterisk in the failing line.
Valid code will look like
string line;
myArray[0] = "Hello";
line = myArray[0];
By the way you could use class std::vector instead of the array if you are going to add or remove elements from the collection.
For example
std::vector<std::string> myArray;
myArrray.reserve( 5 );
myArray.push_back( "Hello" );
line = myArray[0];
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
I'm sure this is a novice question but the book I'm studying from shows how to encode structurs into a buffer. However the examples are for simple types only. I'm trying to take this one step further by using a dynamic array.
My problem is that after calling encode(), the calling function can see the value of bodyLen but the value of "body" is lost. What am I doing wrong?
The pseudo code:
struct outData
{
uint32_t bodyLen; // Length of body
uint32_t *body; // Array of body elements
};
int encode(uint8_t *buffer, int size)
{
// Set the buffer to the data structure layout
outData *myData = (outData *)buffer;
// Junk data (a nice 10101010...)
uint32_t junk = 2863311530;
// Populate body
uint32_t bodyData[size];
for (int i=0; i<size; i++)
{
bodyData[i] = htonl(junk);
}
// Set the buffer
myData->bodyLen = size; // This works perfectly
myData->body = bodyData; // This is blank to calling function
...
}
Thanks!
bodyData is a local variable, but you are setting myData->body to point to it. After encode returns, bodyData is destroyed and *myData->body is now undefined.
To fix, you need to do a deep copy of the contents of bodyData into myData->body after properly allocating the space (with new[]). And then you need to be sure to delete[] it later.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
How to cast a Pointer in C++ from class name in String?
Psuedocode:
int * ptr = something;
myStruct ptrstruct = (ClassFromString("myStruct") ptr);
// The class/struct name is passed in as String
Thank you
I'm not a c++ guru but I have two ideas that may help with brainstorming:
May the use of the registry pattern as described here would be of help: Instantiate class from name?
Secondly, following the registry pattern idea you could crate a function for casting e.g. MyClass something = registry.cast("MyClass", ptr);
I am not sure but this must work
Only Void pointer or boost can help, if it happens
thing * p = something; // pointer to object
void * pv = p; // pointer to void
thing * p2 = static_cast<thing *>(pv); // pointer to the same object
Maybe same kind of situation is while returning values from Threads
Overall reflection is not possible in c++.
Its just brainstroming.