pointers

Can I delete a memory previously allocated dynamically, but with a different pointer?

纵然是瞬间 提交于 2021-02-08 07:22:08
问题 I was making a program for linked list in C++. To implement the concept, I created a pointer 'start' globally, pointing to the first element of the list. After completion of the program I tried to delete all memory allocated dynamically to prevent memory leaks, by accessing successive nodes using the start and another locally declared pointer 'p'. Here, I used a pointer pointing to the same correct addresses, but this pointer was not the one used for memory allocation, but was declared

Delete a dynamic array but keep a pointer

旧巷老猫 提交于 2021-02-08 06:34:27
问题 I have made a function for expanding array, and this function is inside a class. Because this function creates new_arr and copies all the numbers of array into the new_arr and at the end sets pointer of array with new_arr , I wold like to know how to delete numbers in array becuse I dont need it any more void Array::bigger() { int new_size = size * 2; int *new_arr = new int[new_size]; for (int f1=0; f1<last; f1++) { new_arr[f1] = array[f1]; } this->size = new_size; array = new_arr; } Thanks

PHP dynamic string update with reference

99封情书 提交于 2021-02-08 06:22:43
问题 Is there any way to do this: $myVar = 2; $str = "I'm number:".$myVar; $myVar = 3; echo $str; output would be: "I'm number: 3"; I'd like to have a string where part of it would be like a pointer and its value would be set by the last modification to the referenced variable. For instance even if I do this: $myStr = "hi"; $myStrReference = &$myStr; $dynamicStr = "bye ".$myStrReference; $myStr = "bye"; echo $dynamicStr; This will output " bye hi " but I'd like it to be " bye bye " due to the last

Using pointer to character as the argument of strtok

僤鯓⒐⒋嵵緔 提交于 2021-02-08 05:28:15
问题 I try to split the string by using strtok function. But the program become failed if i use the pointer to character as the argument of this function. If i initialize the string as s2 or s3 the program works well. But if i use pointer to character as s1 the program get Segmentation fault (core dumped) . char *s1 = "1A 2B 3C 4D"; char s2[] = "1A 2B 3C 4D"; char s3[20] = "1A 2B 3C 4D"; The problem is the other functions, printf and strlen work without failure, but only strtok get error. The

Difference between array of pointers and pointer to array?

前提是你 提交于 2021-02-08 03:29:28
问题 char string1[3][4]={"koo","kid","kav"}; //This is a 2D array char * string[3]={"koo","kid","kav"}; //This is an array of 3 pointers pointing to 1D array as strings are stored as arrays in memory char (*string1Ptr)[4]=string1; //This is a pointer to a 1D array of 4 characters //I want to know differences between string1Ptr(pointer to array mentioned in question) and string(array of pointers mentioned in question). I only typed string1 here to give string1Ptr an address to strings Besides the

Assigning pointer to uninitialized variable changes it value?

人盡茶涼 提交于 2021-02-07 21:13:55
问题 I'm playing with c++ in VisualStudio2010 Please explain why IT happens: int a, b; int *p, *q; cout << a << " " << b; prints out "0 0". Well it's understandable, uninitialized integer should be 0; but int a, b; int *p, *q; p = &a; cout << a << " " << b; output is "1792816880 0" So if I assign pointer to uninitialized variable it change value from default. Why? Edit clarification: the question was not about value of uninitialized variable int a; int *p; cout << a; // would be 0, because it's

Assigning pointer to uninitialized variable changes it value?

前提是你 提交于 2021-02-07 21:10:18
问题 I'm playing with c++ in VisualStudio2010 Please explain why IT happens: int a, b; int *p, *q; cout << a << " " << b; prints out "0 0". Well it's understandable, uninitialized integer should be 0; but int a, b; int *p, *q; p = &a; cout << a << " " << b; output is "1792816880 0" So if I assign pointer to uninitialized variable it change value from default. Why? Edit clarification: the question was not about value of uninitialized variable int a; int *p; cout << a; // would be 0, because it's

Assigning pointer to uninitialized variable changes it value?

不羁岁月 提交于 2021-02-07 21:07:28
问题 I'm playing with c++ in VisualStudio2010 Please explain why IT happens: int a, b; int *p, *q; cout << a << " " << b; prints out "0 0". Well it's understandable, uninitialized integer should be 0; but int a, b; int *p, *q; p = &a; cout << a << " " << b; output is "1792816880 0" So if I assign pointer to uninitialized variable it change value from default. Why? Edit clarification: the question was not about value of uninitialized variable int a; int *p; cout << a; // would be 0, because it's

Is char pointer address initialization necessary in C?

百般思念 提交于 2021-02-07 20:21:05
问题 I'm learning C programming in a self-taught fashion. I know that numeric pointer addresses must always be initialized, either statically or dynamically. However, I haven't read about the compulsory need of initializing char pointer addresses yet. For example, would this code be correct, or is a pointer address initialization needed? char *p_message; *p_message = "Pointer"; 回答1: I'm not entirely sure what you mean by "numeric pointer" as opposed to "char pointer". In C, a char is an integer

Does casting pointers to integers define a total order on pointers?

荒凉一梦 提交于 2021-02-07 18:42:46
问题 (related to my previous question) In QT, the QMap documentation says: The key type of a QMap must provide operator<() specifying a total order . However, in qmap.h , they seem to use something similar to std::less to compare pointers: /* QMap uses qMapLessThanKey() to compare keys. The default implementation uses operator<(). For pointer types, qMapLessThanKey() casts the pointers to integers before it compares them, because operator<() is undefined on pointers that come from different memory