sepp2k's Profile
Reputation: 1690
Grandmaster
- Group:
- Mentors
- Active Posts:
- 2,562 (3.64 per day)
- Joined:
- 21-June 11
- Profile Views:
- 16,752
- Last Active:
A minute ago- Currently:
- Viewing Board Index
Previous Fields
- Country:
- DE
- OS Preference:
- Linux
- Favorite Browser:
- Who Cares
- Favorite Processor:
- Who Cares
- Favorite Gaming Platform:
- Who Cares
- Your Car:
- Who Cares
- Dream Kudos:
- 0
- Expert In:
- C/C++, Functional Programming, Java, Python, Computer Science
Posts I've Made
-
In Topic: How to handle “ *** glibc detected *** ./a: double free or corruption
Posted 25 May 2013
To start with you should compile with debugging symbols enabled, so that the backtrace tells you where the error happens (or at least where the error is first detected). The next step would be to run your code in a debugger to see what's going on when (and before) the error occurs. -
In Topic: Problem with structs
Posted 25 May 2013
shurd, on 25 May 2013 - 12:57 PM, said:char* aux; strcpy(aux,reg.chavePrimaria); return aux;
This won't work. aux is uninitialized and passing it to strcpy will invoke undefined behavior. In practicaly terms, this will cause strcpy to try to copy the string into "random" memory, which might cause a segmentation fault or other undesired consequences.
You could make this work if you allocate the memory for aux on the heap using malloc, but then you'd need to free it when you're done with it. This will make your code more complicated (instead of just doing printf("%s", get_bla(fix)); you'd need char * bla = get_bla(fix); printf("%s", bla); free(bla);), will introduce the possibility of a memory leak if the user of the get_ function isn't careful and it would just be a waste of time and space since you don't actually need or want to create a copy of the string.
Quote
static char aux[5]; strcpy(aux,reg.chavePrimaria); return aux;
Consider this:
int main() { struct tipo_Fix fix1; struct tipo_Fix fix2; set_Fix_chavePrimaria(&fix1,"ID28"); set_Fix_chavePrimaria(&fix2,"ID29"); char * cp1 = get_Fix_chavePrimaria(fix1); char * cp2 = get_Fix_chavePrimaria(fix2); printf("%s", cp1); printf("%s", cp2); return 0; }
What will the output of this code be using the above definition of get_Fix_chavePrimaria? Is that the output you'd want to get?
The only way to make a get function work sensibly in this case would be to have it take a pointer to the struct.
Quote
Or is it simply better to not use a get function on C?
If the definition of the struct is visible to the user, I see no point in having a get function as the user can just access the field's directly anyway. If you want the definition of your struct to be hidden from the user, it would make sense to have a get function. And since, in that case, the get function would have to take a pointer anyway, that'd work out fine. -
In Topic: Variable not being passed between functions.
Posted 16 May 2013
ccubed, on 16 May 2013 - 01:55 PM, said:Also, based on your above code this shouldn't compile. You pass 0 to functions that require a pointer to a float variable.
The code does (and should) compile just fine. It is perfectly valid to pass 0 to a function expecting a pointer - that will simply call the function with a null pointer. Of course those functions shouldn't be called with null pointers as arguments, but the compiler doesn't know that. -
In Topic: Variable not being passed between functions.
Posted 16 May 2013
You're calling scanf with a null pointer as an argument, which invokes undefined behavior. You're also passing a null pointer to CalculateVolume and then dereferencing it, again invoking undefined behavior. Further it doesn't make sense that a function that's supposed to calculate a volume would take that volume as a non-pointer argument. Assigning to Volume within CalculateVolume won't have any effect. You're also not using the return value of CalculateVolume. -
In Topic: type element is a subtype of type elements in 2nd arrlist
Posted 16 May 2013
If you make the arguments ArrayList<Object>, you won't be able to pass ArrayList<String> or anything else to the method (generics in Java are not covariant if used without wild cards). Making both arguments ArrayList<?> would work, but so would the OP's solution as far as I can tell (though the body of the method could definitely do with some cleaning up -- like removing the pointless inner loop).
My Information
- Member Title:
- D.I.C Lover
- Age:
- Age Unknown
- Birthday:
- Birthday Unknown
- Gender:
-
- Years Programming:
- 15
- Programming Languages:
- Haskell, Ruby, OCaml, SML, F#, Scala, Racket/Scheme, Vala, C#, C++, Java
Contact Information
- E-mail:
- Private
- ICQ:
-
205544826
- Jabber:
-
sexykane86@jabber.ccc.de
|
|


Find Topics
Find Posts
View Reputation Given
|