New answers tagged pointers
4
votes
what are "const-correctness" rules for initializing/binding pointers and references to pointers?
Indeed there is a lot going on.
Initialization is covered in the C++ spec in dcl.init.
The section dcl.init.1 confirms that the rules are the same for all 3 syntactic contexts (variable initialization,...
8
votes
delete[] isn't deleting the arrays
Your understanding of new/new[] and delete/delete[] is incorrect, and all the assert calls in your code should be removed.
Firstly new and new[] never return NULL on failure. If they fail they throw a ...
1
vote
Does the static type of an object coincide with the dynamic type, if the compiler can infer the dynamic type at compile type?
"Static type" just means the type of an expression. Various rules in the standard tell you how the type of an expression is determined. "Dynamic type" means the type of an object ...
4
votes
Does the static type of an object coincide with the dynamic type, if the compiler can infer the dynamic type at compile type?
After the shown declaration
the static type of the expression *d is B const and its value category is glvalue;
the dynamic type of the object referenced by the glvalue expression *d is D const.
The ...
0
votes
Can the addresses of two distinct variables of different types compare equal in C?
No cites provided, just a community wiki starting point.
IMO, a detailed answer involves quite a bit and better if OP narrowed the question.
Keep in mind that in C, there are special issues ...
Community wiki
0
votes
c++: Is object pointed to by a "pointer to const" considered unchanging or just unmodifiable?
As this post explores in more detail, having a pointer to const does not mean that the compiler can actually assume the pointed to object to be const [in a multithreaded/interruptable environment] in ...
0
votes
GDAL/ogr2ogr/ogrinfo produces an invalid pointer error each time I run it
I have Ubuntu 20.04.6 LTS (WSL2).
Same problem.
The challenge is finding the right libproj version that works with your both Gdal and Pdal. I tried many different variations. Here is my own package ...
-2
votes
Can the addresses of two distinct variables of different types compare equal in C?
Once you went that far, it is highly advisable that you learn and have a real life practice with assembler. This would give you an understanding of what memory and pointers really are.
High level ...
1
vote
Can the addresses of two distinct variables of different types compare equal in C?
It's practically impossible for an implementation to have "int memory" and "float memory" because any suitably aligned memory (such as that returned by malloc) can be used to hold ...
4
votes
Can the addresses of two distinct variables of different types compare equal in C?
As far as standard C is concerned, it avoids the term addresses when it can, because the nature of addresses is implementation-defined. The C standard does assume that addresses are constant through ...
3
votes
How to access the metadata that malloc stores for each block?
There is no standard function to get the metadata for a malloc block, because as you correctly note, the location and contents of the metadata block are heavily implementation-dependent.
There are ...
0
votes
Are there pointers in javascript?
JavaScript doesn't have traditional pointers like C or C++, but it does utilize references. Understand the difference:
Primitive Values (No Pointers)
let num1 = 89;
let num2 = num1;
console.log(num1); ...
0
votes
How can I use/modify a class in different functions of another class?
Technical comment 1: For delayed initialization, prefer optionals to pointers
Your original intent was to have plain members, within the Game object; you did not intend to switch between multiple ...
1
vote
Overload resolution of a pointer and a container with pointers
Let us first consider what happens when function overloading is specified. The compiler will search for an ambigous call during compilation so we straight off the bat can eliminate run time errors and ...
18
votes
Accepted
Overload resolution of a pointer and a container with pointers
Because the conversion from braced-list to std::vector is classified as user-defined conversion sequence, which has lower rank.
You can add another overload taking std::initializer_list which will be ...
8
votes
Overload resolution of a pointer and a container with pointers
The compiler prefers to choose an overload that requires less conversions (preferably none).
In this case a pointer can be directly initialized from {} in order to invoke the second overload.
You can ...
3
votes
Accepted
Seg fault on string array items after tokenizing in external function
A few style notes.
Using strdup
To me this is suspect:
tokens[*tokens_count - 1] =
strcpy((char *)calloc(strlen(token) + 1, sizeof(char)), token);
You don't need to cast the result of ...
2
votes
Seg fault on string array items after tokenizing in external function
Thank to @Someprogrammerdude and @AndersK, I could reajust my thoughts and correct the code. Please refer to the comments above.
Thanks again!
Here the corrected code with corrections in comments on ...
-1
votes
Difference between &IntPtr and .ToPointer() in an unsafe context - P/Invoke
The behavior with IntPtr and out when using P/Invoke can be understood through how the interop marshalling works in .NET. Essentially, interop marshalling is a runtime mechanism that translates ...
5
votes
String to char* conversion in struct or class gives undefined first chars
Your constructor stores a pointer to the data of the string passed in.
Data(std::string string) : m_data(string.data()), ...
but this string is destroyed when the constructor is exited. So you are ...
3
votes
String to char* conversion in struct or class gives undefined first chars
In your constructor here:
Data(std::string string) : m_data(string.data()), m_size(string.size())
You make a copy object string whose lifecycle ends at the end of the constructor body. Thus m_data ...
3
votes
Why the structure pointer "p" in the following code is not updating with the "temp" value assigned to it?
You are passing the pointer p by value
AddatBeginning (p, 100);
It means that the function deals with a copy of the original passed pointer. Any changes of the copy like that
p=temp;
leave the ...
4
votes
Accepted
During the second call of strtok(), the code raises the following error: Invalid read of size 1
strtok() is not reentrant. It can tokenize only 1 string at a time. When you call it with NULL, it returns the next token for the previous non-NULL string that you passed in.
However, removeDuplicates(...
2
votes
Accepted
Is it safe and defined behaviour to cast a pointer to another level of indirection?
Is it safe and defined behaviour to cast a pointer to another level of indirection?
It's unclear why you think it might not be safe. In particular, "Level of indirection" is not a property ...
-1
votes
Defining array with dimensions without allocation in C
You are allowed to do this:
int (*my_matrix)[5][10] = malloc(sizeof *my_matrix);
if you want to dynamically allocate a matrix, this will allow you to access your elements as (*my_matrix)[2][3] for ...
0
votes
How to remove odd numbers from a stack with C?
thank you guys I finished, I did what you guys said.
I created a new stack and if the number was even, I pushed it to the new stack, and the odd ones I just popped them, after all that I pushed the ...
0
votes
How to remove odd numbers from a stack with C?
Ohh, okay guys I got it. Probably I'm going to pop ALL tem and push the odds after
1
vote
Accepted
unique_ptr is not changing object it's pointing to
I expected unique_ptr to behave like a raw pointer.
That was your mistake. If you want the behavior of a raw pointer, you should use a raw pointer.
You can fix this code by replacing std::unique_ptr ...
1
vote
How do you convert void pointer to char pointer in C
I faced the same problem as you, and the way I got it to work was by encompassing the whole thing in parentheses.
Example:
pChar = ((char*)pVoid);
0
votes
How to remove odd numbers from a stack with C?
Although your code identifies the nodes that have odd values -- using aux -- this doesn't have anything to do with what pop does. pop will always remove the top element from the stack, no matter ...
0
votes
How to remove odd numbers from a stack with C?
In a stack one begins empty, pushes consecutively, and pops in the reversed order from the top. Only the top is available.
Hence you cannot walk all elements from the stack, all even numbers must be ...
0
votes
How to remove odd numbers from a stack with C?
The problem with your approach is that you use pop for removing nodes in the middle of the stack. That's not what popdo - it removes the front element.
So you need another function that can remove ...
0
votes
C convert 2 sequential uint8_t to one uint16_t
There are three main concepts that you need to take care when you play around with pointers
Padding (does not apply in your case)
Little Endian verses Big Endian
Signed verses Unsigned datatypes (...
-1
votes
Is NULL always zero in C?
#define NULL (void *)0
printf("NULL %d\n", NULL);
results == 0
#define NULL (void *)1
printf("NULL %d\n", NULL);
results == 1
From the above, when we say NULL. We are telling ...
0
votes
C convert 2 sequential uint8_t to one uint16_t
As the other colleagues already mentioned, it depends on the endianness of the targeted systems.
A work-around if I understood what you are trying to do is:
#include <stdio.h>
#include <...
3
votes
Accepted
C code reading only part of a matrix correctly
You have minus signs in the data, Unicode character 8722. The %lf conversion of fscanf expects a hyphen to represent a minus sign. Edit the Gauss1.dad file to change the minus signs to hyphens. Or ...
-2
votes
C convert 2 sequential uint8_t to one uint16_t
Although it is not a direct answer to your question, the code below contains a more C Standards compatible version of what you want to do (as @Lundin pointed in comment).
#include <stdio.h>
#...
1
vote
Accepted
C convert 2 sequential uint8_t to one uint16_t
shouldn't the bytes array look like this in memory 0x00 0x7e ?
The way the order of bytes in memory are interpreted as a uint16_t depends on the Endianness of your system.
Also *(uint16_t*)pointer) ...
3
votes
Accepted
Can not convert a generic struct type to a generic interface type in golang
You are assuming covariance in type parameters, which Go does not have - types must match exactly. You would need variance annotations like Kotlins in and out or Javas wildcards for that.
As the FAQ ...
2
votes
How to free memory when casting from a void pointer in C
How does free keep track of the allocation sizes?
Implementations of the memory management routines keep track of memory in various ways. One method is that, when memory is allocated, the routines ...
0
votes
What is the difference between a , &a and *a?
Actually, 'a' is not just any variable but a pointer variable which stores the address of an integer type variable 'b'. So, when you print 'a', then according to the data given, it will represent the ...
2
votes
Problems with constant int and pointer indicating it
You cast away const in order to change the value of a variable declared const. This makes your program have undefined behavior and you should not expect any particular output from that program, be it ...
2
votes
Can not convert a generic struct type to a generic interface type in golang
You can fix the compile error by dereferencing the *Packet (pointer to the Packet interface) from r3.Get():
r := &BaseRecycler[packet]{}
var r1 interface{} = r
r3 := r1.(Recycler[Packet])
fmt....
1
vote
How to free memory when casting from a void pointer in C
A further couple of notes on style and best practices, since wohlstad has directly answered the question.
You should check the return value from malloc and handle it if it doesn't succeed.
p is both ...
4
votes
Accepted
How to free memory when casting from a void pointer in C
I'm not allowed to free it anymore?
Yes you are allowed to free it. In fact you must (eventually) free it in order to avoid a memory leak.
free does not care about the type the pointer points to - it ...
1
vote
create a function like strlcpy in c
I'd expect ft_strlcpy() to use restrict pointers to indicate overlapped memories risk undefined behavior (UB).
size_t ft_strlcpy(char *restrict dst, const char *restrict src, size_t dstsize);
If code ...
3
votes
create a function like strlcpy in c
Here is the specification for strlcpy (and strlcat) from the original BSD man pages:
Synopsis
#include <bsd/string.h>
size_t strlcpy(char *dst, const char *src, size_t size);
size_t strlcat(...
0
votes
Accepted
Is it allowed to return a dereferenced pointer of a blittable struct in C#
The answer is no it is not save to return the struct. The struct itself is a copy so it it safe to return but in the struct is a PWSTR aka a char* and that pointer points to an area in the byte* (just ...
1
vote
C++ array pointer to pointer
How and by what rule is my array masquerading as an array of arrays?
The code your provided declare buf as a pointer to a dynamically allocated array of ints with a size of max_size. So, buf pointers ...
-1
votes
c++ passing ownership to composite class
This function does not appear to be clear and complete. Looks like this has other issues also.
void SomeLongLivedClass::someFunction()
{
B m = createB();
A a = new A{ m }
// m goes out of ...
Top 50 recent answers are included
Related Tags
pointers × 57342c × 28405
c++ × 23014
arrays × 10976
struct × 4646
function × 3032
string × 2725
reference × 1954
memory × 1844
linked-list × 1810
malloc × 1727
char × 1374
vector × 1367
class × 1346
segmentation-fault × 1335
memory-management × 1331
go × 1062
casting × 1058
c# × 1043
multidimensional-array × 1033
c++11 × 905
constants × 813
structure × 795
dynamic-memory-allocation × 767
function-pointers × 746