Hello, I have a C++ memory management doubt, that's (obviously) related to references and pointers. Suppose I have a class Class with a method my_method:
OtherClass& Class::my_method( ... ) {
OtherClass* other_object = new OtherClass( ... );
return *other_object;
}
Meanwhile in a nearby piece of code:
{
Class m( ... );
...
I want to write a function that reverses the given string passed into it.
But, I can not. If I supply the doReverse function (see code below) with a character array, my code works well.
I can't figure out why this does not work.
I am able to access str[0] in doReverse, but I can't change any value of the array by using a char pointer. A...
My current code to the effect of:
if( objectPointer != NULL){
delete objectPointer;
}
doesn't work because the pointers are getting set to invalid hex numbers by the compiler such as:
0xbaadf00d
0xdeadbeef
etc....
So what's the best way to check for an invalid pointer before trying to delete the object?
...
I want a simple function that receives a string and returns an array of strings after some parsing. So, this is my function signature:
int parse(const char *foo, char **sep_foo, int *sep_foo_qty) {
int i;
char *token;
...
strcpy(sep_foo[i], token); /* sf here */
...
}
Then I call it like this:
char sep_foo[MAX_QTY...
What is the difference between
void func(const Class *myClass)
and
void func(Class *const myClass)
See also:
http://stackoverflow.com/questions/269882/c-const-question
http://stackoverflow.com/questions/455518/how-many-and-which-are-the-uses-of-const-in-c
and probably others...
...
When i try to compile this as part of an objective-c program it gives the warning:
warning: passing argument 1 of 'sqlite3_close' from incompatible pointer type
sqlite3 *db;
sqlite3_open("~/Documents/testdb.sqlite", &db);
/*stuff*/
sqlite3_close(&db);
An almost identical error is given with nearly any other function call that uses &d...
First off, here is some code:
int main()
{
int days[] = {1,2,3,4,5};
int *ptr = days;
printf("%u\n", sizeof(days));
printf("%u\n", sizeof(ptr));
return 0;
}
Is there a any way to find out the size of the array that ptr is pointing to? Instead of just giving it's size, which is 4 bytes.
Thanks.
...
I recently embarrassed myself while explaining to a colleague why
char a[100];
scanf("%s", &a); // notice a & in front of 'a'
is very bad and that the slightly better way to do it is:
char a[100];
scanf("%s", a); // notice no & in front of 'a'
Ok. For everybody getting ready to tell me why scanf should not be used anyway for securi...
I am very new to C and I have some problems learning about pointers. I experimented swapping and that's all what I can do with them :) I know that every variable has its own address in memory cells (this is what my lecturer told us) and every variable's value can be obtained by going to its associated address and then fetching the value ...
I'm using C++ to understand how exactly pointers work. I have this piece of code using arrays, which I'm using just to understand how the equivalent works with pointers.
int main() {
int arr[10] = {1,2,3};
char arr2[10] = {'c','i','a','o','\0'};
cout << arr << endl;
cout << arr2 << endl;
}
However when I run th...
So I have some code that looks like this:
int a[10];
a = arrayGen(a,9);
and the arrayGen function looks like this:
int* arrayGen(int arrAddr[], int maxNum)
{
int counter=0;
while(arrAddr[counter] != '\0') {
arrAddr[counter] = gen(maxNum);
counter++;
}
return arrAddr;
}
Right now the compilier tells me "warning: passing argume...
Is there an integer type with the same size as pointer? Guaranteed on all microarchitectures?
...
Is it safe and predictable to reuse pointers after freeing the data they point to?
For example:
char* fileNames[] = { "words.txt", "moreWords.txt" };
char** words = NULL;
int* wordsCount = NULL;
for ( i = 0; i < 2; ++i ) {
data = fopen( fileNames[i], "r" );
words = readWords( data );
wordsCount = countWords( words );
f...
How can i have a pointer to the next struct in the definition of this struct? :
typedef struct A {
int a;
int b;
A* next;
} A;
this is how i first wrote it but it does not work.
...
I have a Linux C++ application and I'd like to test an object pointer for validity before dereferencing it. However try/catch doesn't work for this on Linux because of the segmentation fault. How can this be done?
...
std::map<std::string, std::string> myMap;
std::map<std::string, std::string>::iterator i = m_myMap.find(some_key_string);
if(i == m_imagesMap.end())
return NULL;
string *p = &i->first;
Is the last line valid?
I want to store this pointer p somewhere else, will it be valid for the whole program life?
But what will happen if I ad...
I'm making this a community wiki in order to better understand the semantic differences between these errors and their runtime or compiled consequences. Also, I've coded on Java far too long and I want to learn pointers better in C++ -- so I need other people to do it.
Edit2: I am refactoring this question. The distinction I am trying...
This is tricky for me.
const int * const buffer[]
Currently, I have it translated as follows:
byte[] buffer
Problem is that I'm getting AccessViolation exceptions, when DLL is calling function with that is using above parameter.
Thanks for help.
...
Hi, I am new to coding and trying to get up to speed with Objective-C.
Came across some code I did not understand. I was hoping someone could
clarify it for me. In the case below, I am not sure how *foo2 is working and why it is not being released?
ClassOne *pointer = [[ClassOne alloc]init];
ClassTwo *foo = [[ClassTwo alloc]init], *fo...
how do I make a pointer to a multidimensional array, which have a unknown size? I've tried this:
int **triangles;
triangles = new int[numTriangles][3];
But i get this error:
cannot convert 'int (*)[3]' to 'int**' in assignment
...