pointers

Will this lead to a memory leak in C++?

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( ... ); ...

Modifying C string constants?

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...

How do you check for an invalid pointer?

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? ...

Passing an array of strings as parameter to a function in C

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...

Difference between const declarations in C++

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... ...

Pointer issues when using sqlite in an objective-c program.

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...

How to find the sizeof( a pointer pointing to an array )

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. ...

Semantics of char a[]

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...

How to Approach Pointers in C?

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 ...

Why does cout print char arrays differently from other arrays?

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...

Passing pointers of arrays in C

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...

sizeof (int) == sizeof (void*) ?

Is there an integer type with the same size as pointer? Guaranteed on all microarchitectures? ...

Is it safe to reuse pointers variables after freeing what they point to?

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...

C : pointer to struct in the struct definition

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. ...

Try/Catch a segmentation fault on Linux

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, pointer to map key value, is this possible?

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...

Consequences of these Pointer Errors in C++ vs Managed

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...

How to import const int * const buffer[] into C# ?

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. ...

Objective-C pointers?

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 has an unknown size?

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 ...