pointers

valgrind shows memory leak even after memory free

我与影子孤独终老i 提交于 2021-01-29 06:23:40
问题 so I have the file Countries.c which contains: typedef struct City* pCity; typedef struct Country* pCountry; typedef struct Territory* pTerritory; struct City{ char* name; char* food; int population; }; struct Country{ char *name; int numCities; pCity cities; pTerritory countryTerr; }; struct Territory{ int x1; int x2; int y1; int y2; }; void deleteCountry(pCountry country){ if(country != NULL){ int num_of_cities = country->numCities; for(int i = 0 ; i<num_of_cities; i++){ if (country->cities

Two names for the same function

僤鯓⒐⒋嵵緔 提交于 2021-01-29 06:04:13
问题 I need two instances of the same function (not just alias). One thing that definitely works is void writedata(union chip *tempchip, unsigned char *datapos, int datanum) { blahblah } void writestring(union chip *tempchip, unsigned char *datapos, int datanum) { writedata(tempchip, datapos, datanum); } This is kind of silly, because the second just passes parameters to the first. So I tried to be "smart" and make a pointer void writedata(union chip *tempchip, unsigned char *datapos, int datanum)

basename on buffer goes into segmentation fault

时间秒杀一切 提交于 2021-01-29 04:26:43
问题 I'm tweaking with basename right now and I encounter a case really weird (at least for me). Here's the code: char buffer[300]; char* p; strcpy(buffer, "../src/test/resources/constraints_0020_000"); printf("%d\n", strcmp(basename("../src/test/resources/constraints_0020_000"), "constraints_0020_000")); //works as expected printf("assert testBasename02"); printf("%d\n", strcmp(basename(buffer), "constraints_0020_000") == 0); printf("done 1\n"); //goes in segmentation fault printf("%d\n", strcmp

segment fault, assigning to double pointer in c

邮差的信 提交于 2021-01-29 04:10:04
问题 I am getting a little confused as to how to properly allocate memory for double pointers. My code causes a segment fault the second time it attempts to store a value at the 2nd index of the first UCHAR pointer array. Any assistance would be appreciated. assigning my double pointer: width = BMP_GetWidth (bmp); height = BMP_GetHeight (bmp); depth = BMP_GetDepth (bmp); r = (UCHAR **) malloc(sizeof(UCHAR *) * height); g = (UCHAR **) malloc(sizeof(UCHAR *) * height); b = (UCHAR **) malloc(sizeof

What will be the size of pointer on a 8 bit microcontroller like 8051?

陌路散爱 提交于 2021-01-29 02:47:26
问题 We know that size of the pointer depends on address bus,so what will be the size of pointer on 8 bit microcontroller like 8051? 回答1: The 8051 is not a C friendly processor. It has several address spaces. I used the Keil 8051 compiler extensively and it had several pointer types. An 8 bit pointer to point at the internal memory space or internal indirect space. A 16 bit pointer to point to either external ram or code space. A "smart" 24 bit pointer that could point anywhere. Basically a tag

copying pointers and memory allocation confusion

可紊 提交于 2021-01-28 21:17:19
问题 What if I want to make a function that takes in an array and modifies it. void mod_Arr(int* arr) { int*arr = int[3][3]; //arr is now an array int*arrCpy = arr; //some modification to arrCpy } `` 1) will arr be modified as well? i.e must I include the line: arr = arrCpy; or should I instead use: arr = mod_Arr(arr) //and get mod_Arr to return an array? 2) Must I free(arr) to prevent old values of arr taking up space? In the case that the pointer now points to a new memory instead of modifying

Initializing array of subclasses of abstract base class in C++

青春壹個敷衍的年華 提交于 2021-01-28 20:14:00
问题 I have an abstract base class in C++ and need to create an array to store objects which are subclasses of it. I use pointers to the subclasses since each member of the array needs to be of the same size. Currently I am declaring and initializing my array like this: BaseClass *array[]; ... array = { &SubClass1(...), &SubClass2(...), ... &SubClass3(...) }; This is giving me the following when I try to compile: warning: taking address of temporary error: too many initializers for ‘BaseClass* [0]

Accessing struct elements using single pointer?

泄露秘密 提交于 2021-01-28 19:51:06
问题 I have defined 5 floats within a struct: struct as {float a1, a2, a3, a4, a5;}; I'd like to access those 5 using a single pointer float* p_a = &a1; , and then just p_a++ , etc. Will it work? Converting to array float a[5] requires a big change I would like to avoid. 回答1: I would rather typedef struct { union { float fa[5]; struct { float f1,f2,f3,f4,f5; }; }; /* .... */ }MY_T; void foo() { MY_T s; float *p = s.fa; p++ *p = something; } 回答2: You can do ++ on array-element pointers and as long

How to modify the value of a simple type through pointer receiver method in Go?

ぐ巨炮叔叔 提交于 2021-01-28 19:20:29
问题 I wanted to have a custom type based on a basic type and be able to set its value by calling a pointer receiver. When I run the following program: package main import ( "fmt" "strconv" ) type FooInt int func (fi *FooInt) FromString(i string) { num, _ := strconv.Atoi(i) tmp := FooInt(num) fi = &tmp } func main() { var fi *FooInt fi.FromString("5") fmt.Printf("%v\n", fi) } I receive <nil> . Why doesn't the pointer declared in main() change its value to the address of tmp ? Here is a Go

Extending and shrinking array using realloc

怎甘沉沦 提交于 2021-01-28 15:52:22
问题 I'm trying to write a program which first dynamically initialises a queue array for 100 int elements. Whenever the queue is full and another element is supposed to be queued, the original array is supposed to double it's size so new elements can be inserted. In case elements are dequeued, and the amount of elements the queue consists of falls below half of its actual size, the queue size is supposed to be cut in half. However, its size should never fall below 10. I'm trying to expand and