Hi.
I'm trying to figure out pointers and memory allocation in C. I've read quite a few explanations on the internet and have read in a few books that I've got, but I'm not so much smarter yet...
What I want do do is to create an array of five pointers to characters... I want to make it possible to assign a pointer to the params array tohrough the setParams function. I don't understand what I have not gotten, but there is certainly something. It seems like this works:
but this doesn't:
This seems logical to me, because the val1 variable is first instantiated with memory allocated and then set to "Hei og hå". I would assume the memory location returned from the malloc call would be lost, right?. The only walkaround for this problem as far as I have understood is to assign one char at the time and increase the pointer one position at the time and do
and then increase the pointer with val1++ for each letter. It sounds too complicated to be right.. Or is it like this? Another problem with that solution is that when I've moved the pointer a few positions I have lost the start position of the var and is no longer able to free the var because I don't have the right position.
If someone could straighten this out it would be awesome. I guess it's not so hard, but it is hard to get a grip on when you don't fully understand how the pointers work in C.
The code I'm trying to get to work is to large to post here I think, but if I get the idea on how to get the following piece of code right, much is done I think.
NOTICE! If your going to compile it, compile with "-DDEBUG -DSOLO" to get the main method and the includes in the compile.
Thanks in advance :)
K
I'm trying to figure out pointers and memory allocation in C. I've read quite a few explanations on the internet and have read in a few books that I've got, but I'm not so much smarter yet...
What I want do do is to create an array of five pointers to characters... I want to make it possible to assign a pointer to the params array tohrough the setParams function. I don't understand what I have not gotten, but there is certainly something. It seems like this works:
Code:
char * val1 = malloc(30); free(val1);
Code:
char * val1 = malloc(30); val1 = "Hei og hå!"; free(val1);
Code:
*val1 = 'H';
If someone could straighten this out it would be awesome. I guess it's not so hard, but it is hard to get a grip on when you don't fully understand how the pointers work in C.
The code I'm trying to get to work is to large to post here I think, but if I get the idea on how to get the following piece of code right, much is done I think.
NOTICE! If your going to compile it, compile with "-DDEBUG -DSOLO" to get the main method and the includes in the compile.
Code:
#ifdef DEBUG
#include <stdlib.h>
#include <stdio.h>
#endif
#include "bitwise.h"
int power(int num, int power) {
int i, res=1;
for(i=0; i<power; i++) {
res = res*num;
}
if(power==0)
return 1;
else
return res;
}
void setBit(unsigned char * map, int num, int val) {
if(val != getBit(map, num))
map[num/8] = (map[num/8]^power(2,7-(num%8)));
}
int getBit(unsigned char * map, int num) {
return ((map[num/8]>>(7-num)) & 1);
}
#ifdef SOLO
char * setParam(char * params[], unsigned char * map, int n, char * val) {
params[n] = val;
setBit(map, n, 1);
return params[n];
}
char * getParam(char * params[], unsigned char * map, int n) {
//if(getBit(map, n))
return params[n];
//else
// return NULL;
}
void removeParam(char * params[], unsigned char * map, int n) {
if(getBit(map, n)) {
printf("sletter: %x\n",params[n]);
free(params[n]);
setBit(map,n,0);
}
}
int paramUsed(char * params[], unsigned char * map, int n) {
return getBit(map, n);
}
int main() {
unsigned char status = 0;
char * params[5];
char * val1 = malloc(30);
val1 = "Hei og hå!";
printf("%s == %s\n",val1,setParam(¶ms[0], &status, 0, val1));
printf("%s\n",getParam(params, &status, 0));
printf("Used: %d\n",paramUsed(params, &status, 0));
removeParam(params, &status, 0);
printf("%s\n",getParam(params, &status, 0));
printf("Used: %d\n",paramUsed(params, &status, 0));
return 0;
}
#endif
K
Comment