pointers


 
Thread Tools Search this Thread
Top Forums Programming pointers
# 1  
Old 11-02-2007
pointers

Hi I mash with pointers in C. I solve this problem about 5 hours and I don't know how I should continue.


Code:
void InsertFirst (tList *L, int val)   {
   tElemPtr new;
        if((new = malloc(sizeof(tElemPtr))) == NULL) Error();

   new->data = val;
   new->ptr = L->frst;
   L->frst = new;
   return;

}


and Valgrind writes this:

Code:
Invalid write of size 4
==3742==    at 0x804847C: InsertFirst (in /home/Digimon/c201/prog)
==3742==    by 0x804865E: main (in /home/Digimon/c201/prog)
==3742==  Address 0x402302C is 0 bytes after a block of size 4 alloc'd
==3742==    at 0x4005400: malloc (vg_replace_malloc.c:149)
==3742==    by 0x8048467: InsertFirst (in /home/Digimon/c201/prog)
==3742==    by 0x804865E: main (in /home/Digimon/c201/prog)
==3742==
==3742== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 13 from 1)
==3742== malloc/free: in use at exit: 0 bytes in 0 blocks.
==3742== malloc/free: 1 allocs, 1 frees, 4 bytes allocated.
==3742== For counts of detected errors, rerun with: -v
==3742== All heap blocks were freed -- no leaks are possible.


I think, that I should alocated in structure space for each element
# 2  
Old 11-02-2007
Quote:
Originally Posted by Milla
tElemPtr new;
if((new = malloc(sizeof(tElemPtr))) == NULL) Error();
1. don't use the word "new", it's a reserved word in C++ so a bit of a bad habit and may trip up some C/C++ compilers.

2. You have allocated space for the size of the pointer, not to what it points to.

Code:
   tElemPtr newElem;
        if((newElem = malloc(sizeof(newElem[0]))) == NULL) Error();
....

# 3  
Old 11-03-2007
Thanks a lot Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Pointers and array

Hello, I read from a book exercise for a challenge. How to print out each letter of char array a by two different pointers pa and ppa in the example? I have tried my code for letter "r" by testing without full understanding as only the first one worked. #include<stdio.h> int main() { char... (17 Replies)
Discussion started by: yifangt
17 Replies

2. Programming

Pointer to pointers

Hi guys, I'm trying to understand pointers in C and made a simple example and I've problems with It. Can someone help? #include <stdio.h> #include <stdlib.h> #include <assert.h> int f1(char **str_); int main(int argc, char **argv) { char *str = NULL; f1(&str); ... (3 Replies)
Discussion started by: pharaoh
3 Replies

3. Programming

Problem With Pointers

Hi guys. What is the difference between these: 1. int *a; 2. int (*a); (2 Replies)
Discussion started by: majid.merkava
2 Replies

4. Programming

Need help with the Pointers in C

I have a special character called ô. When it is declared as a character variable its showing it can be printed. But when it is declared as a character pointer variable its showing it cannot be printed. I am just wondering why its happening like this.. c1 = '@'; c2 = 'ô'; char *fp; fp="XXô"; if... (1 Reply)
Discussion started by: sivakumar.rj
1 Replies

5. UNIX for Advanced & Expert Users

shared pointers

I am new to shared pointer conceot in C++ and hence require some clarification: For example: class A { public: virtual ~A() { } int x; }; typedef boost::shared_ptr<A>... (1 Reply)
Discussion started by: uunniixx
1 Replies

6. Programming

restricted pointers

Hi all. I am trying to use restricted pointers to allow the gcc compiler optimize the code, but I have not been able to make it work so far. I am testing with this code: #include <stdlib.h> #include <stdio.h> #include <time.h> #include <sys/time.h> void vecmult(int n, int * restrict a, int... (0 Replies)
Discussion started by: carl.alv
0 Replies

7. Programming

pointers

is this a valid c declaration int (*ptr(int *b)); plz explain... (4 Replies)
Discussion started by: areef4u
4 Replies

8. Shell Programming and Scripting

functiom pointers

Hi all i wonder about function pointers as i never used them in my C code . could any tell me why and where exactly function pointers come into picture . thanq (1 Reply)
Discussion started by: Raom
1 Replies

9. Programming

Regarding Function and Pointers.

HI, Here is some thing that is puzzling me from a long time. Can some body explain me this with example. The question is :- What is the difference between function pointer and pointer to a function. Where do we actually use the function pointers and pointer to functions. Thanks in... (0 Replies)
Discussion started by: S.Vishwanath
0 Replies

10. Programming

Pointers to Arrays

Below is the program i tried to execute...... main() { static int a = {0,1,2,3,4}; static int *p = {a, a+1, a+2, a+3, a+4}; printf (“\n %u %u %d”, p, *p, *(*p) ); } This works, but i wanted to know why both a and *p are declared as "static". If we dont declare a as static... (2 Replies)
Discussion started by: Jayathirtha
2 Replies
Login or Register to Ask a Question